1.泛型类: 通过这个类中的invokeMethod动态调用InvokeClass中的method.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public sealed class DynamicLoadMethod<T> where T : class
{
private static object obj = new object();
private static T invokeMethod;
public static T InvokeMethod
{
get
{
lock (obj)
{
if (invokeMethod == null)
{
invokeMethod = typeof(T).InvokeMember(typeof(T).Name, BindingFlags.CreateInstance |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public, null, null, null, null) as T;
}
return invokeMethod;
}
}
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
2.被动态调用的方法Class
public class InvokeClass
{
private String Name;
public String Names
{
set { Name = value; }
}
public override string ToString()
{
return Name;
}
private Object[] values = new Object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
public Object this[int index]
{
get
{
return values[index];
}
set
{
values[index] = value;
}
}
public Object Value
{
get
{
return "the value";
}
}
public InvokeClass()
{
Name = "initialName";
}
int methodCalled = 0;
public void AddUp()
{
methodCalled++;
Console.WriteLine("AddUp Called {0} times", methodCalled);
}
public void PrintTime()
{
Console.WriteLine(DateTime.Now);
}
public void Swap(ref int a, ref int b)
{
int x = a;
a = b;
b = x;
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
3.调用方法
static void Main(string[] args)
{
Console.WriteLine(DynamicLoadMethod<InvokeClass>.InvokeMethod.Value as String);
DynamicLoadMethod<InvokeClass>.InvokeMethod.AddUp();
DynamicLoadMethod<InvokeClass>.InvokeMethod.Names = "BrianLei Build";
Console.WriteLine(DynamicLoadMethod<InvokeClass>.InvokeMethod.ToString());
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public sealed class DynamicLoadMethod<T> where T : class
{
private static object obj = new object();
private static T invokeMethod;
public static T InvokeMethod
{
get
{
lock (obj)
{
if (invokeMethod == null)
{
invokeMethod = typeof(T).InvokeMember(typeof(T).Name, BindingFlags.CreateInstance |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public, null, null, null, null) as T;
}
return invokeMethod;
}
}
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
2.被动态调用的方法Class
public class InvokeClass
{
private String Name;
public String Names
{
set { Name = value; }
}
public override string ToString()
{
return Name;
}
private Object[] values = new Object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
public Object this[int index]
{
get
{
return values[index];
}
set
{
values[index] = value;
}
}
public Object Value
{
get
{
return "the value";
}
}
public InvokeClass()
{
Name = "initialName";
}
int methodCalled = 0;
public void AddUp()
{
methodCalled++;
Console.WriteLine("AddUp Called {0} times", methodCalled);
}
public void PrintTime()
{
Console.WriteLine(DateTime.Now);
}
public void Swap(ref int a, ref int b)
{
int x = a;
a = b;
b = x;
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
3.调用方法
static void Main(string[] args)
{
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还是可以小试的。
直接调用方法,只花了20毫秒,
而动态调用方法的话,花了12秒
所以大家知道怎么这个思路,就可以了。
在web form 中最好不要使用。
在win form还是可以小试的。