1.泛型类: 通过这个类中的invokeMethod动态调用InvokeClass中的method.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public sealed class DynamicLoadMethod<T> where T : class
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private static object obj = new object();
private static T invokeMethod;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static T InvokeMethod
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (obj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (invokeMethod == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
invokeMethod = typeof(T).InvokeMember(typeof(T).Name, BindingFlags.CreateInstance |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public, null, null, null, null) as T;
}
return invokeMethod;
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
2.被动态调用的方法Class
public class InvokeClass
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private String Name;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String Names
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ Name = value; }
}
public override string ToString()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Name;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private Object[] values = new Object[]
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Object this[int index]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return values[index];
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
values[index] = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Object Value
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "the value";
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public InvokeClass()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Name = "initialName";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int methodCalled = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void AddUp()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
methodCalled++;
Console.WriteLine("AddUp Called {0} times", methodCalled);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void PrintTime()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(DateTime.Now);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Swap(ref int a, ref int b)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int x = a;
a = b;
b = x;
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3.调用方法
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Console.WriteLine(DynamicLoadMethod<InvokeClass>.InvokeMethod.Value as String);
DynamicLoadMethod<InvokeClass>.InvokeMethod.AddUp();
DynamicLoadMethod<InvokeClass>.InvokeMethod.Names = "BrianLei Build";
Console.WriteLine(DynamicLoadMethod<InvokeClass>.InvokeMethod.ToString());
}
最后的话,简单测试了一下,Loop100百万次,
直接调用方法,只花了20毫秒,
而动态调用方法的话,花了12秒
所以大家知道怎么这个思路,就可以了。
在web form 中最好不要使用。
在win form还是可以小试的。
posted @
2007-03-15 16:48
RicoRui
阅读(
654)
评论()
编辑
收藏
举报