利用反射动态调用类成员(泛型篇)

None.gif1.泛型类: 通过这个类中的invokeMethod动态调用InvokeClass中的method.
None.gif\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
None.gif 
public sealed class DynamicLoadMethod<T> where T : class
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private static object obj = new object();
InBlock.gif        
private static T invokeMethod;
InBlock.gif
InBlock.gif        
public static T InvokeMethod
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
lock (obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (invokeMethod == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        invokeMethod 
= typeof(T).InvokeMember(typeof(T).Name, BindingFlags.CreateInstance |
InBlock.gif                                                                          BindingFlags.Instance 
|
InBlock.gif                                                                          BindingFlags.NonPublic 
|
InBlock.gif                                                                          BindingFlags.Public, 
nullnullnullnullas T;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
return invokeMethod;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif
None.gif\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
None.gif
2.被动态调用的方法Class
None.gif 
public class InvokeClass
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private String Name;
InBlock.gif
InBlock.gif        
public String Names
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ Name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Name;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
private Object[] values = new Object[] dot.gif0123456789 };
InBlock.gif
InBlock.gif        
public Object this[int index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return values[index];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                values[index] 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Object Value
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "the value";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public InvokeClass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Name 
= "initialName";
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
int methodCalled = 0;
InBlock.gif
InBlock.gif        
public void AddUp()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            methodCalled
++;
InBlock.gif            Console.WriteLine(
"AddUp Called {0} times", methodCalled);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void PrintTime()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(DateTime.Now);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Swap(ref int a, ref int b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int x = a;
InBlock.gif            a 
= b;
InBlock.gif            b 
= x;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
None.gif
None.gif
3.调用方法
None.gif
None.gif        
static void Main(string[] args)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(DynamicLoadMethod
<InvokeClass>.InvokeMethod.Value as String);
InBlock.gif            DynamicLoadMethod
<InvokeClass>.InvokeMethod.AddUp();
InBlock.gif            DynamicLoadMethod
<InvokeClass>.InvokeMethod.Names = "BrianLei Build";
InBlock.gif            Console.WriteLine(DynamicLoadMethod
<InvokeClass>.InvokeMethod.ToString());
ExpandedBlockEnd.gif        }
None.gif最后的话,简单测试了一下,Loop100百万次,
None.gif          直接调用方法,只花了20毫秒,
None.gif         而动态调用方法的话,花了12秒
None.gif所以大家知道怎么这个思路,就可以了。
None.gif在web form 中最好不要使用。
None.gif在win form还是可以小试的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值