110,545
社区成员
发帖
与我相关
我的任务
分享
//委托调用:
//方法名GetPropertyValue,参数,1.类类型,2.需要反射的属性名(string)
//返回一个委托ReflectDelegate ,参数object,返回值 object
ReflectDelegate reflect = EasyReflect.GetPropertyValue(typeof(Test), "Text");
Test test = new Test();//测试类
object value = reflect(test);//获得测试类的Text属性的值(object类型)
//直接调用:
Test test = new Test();//测试类
object value = EasyReflect.GetPropertyValue(typeof(Test), "Text")(test);
//委托调用:
MethodInfo method = typeof(Test).GetProperty("Text", EasyIL.ALLATTR).GetGetMethod();//获得属性的Get方法
ReflectDelegate reflect2 = delegate(object test1) { return method.Invoke(test1, null); };//获得invoke方法委托
Test test = new Test();//测试类
object value = reflect(test);//获得测试类的Text属性的值(object类型)
//直接调用:
Test test = new Test();//测试类
object value = typeof(Test).GetProperty("Text", EasyIL.ALLATTR).GetGetMethod().Invoke(test, null);
//委托方式调用10个100W次,建立委托时间不算在内
ReflectDelegate reflect = EasyReflect.GetPropertyValue(typeof(Test), "Text");
MethodInfo method = typeof(Test).GetProperty("Text", EasyIL.ALLATTR).GetGetMethod();
ReflectDelegate reflect2 = delegate(object test1) { return method.Invoke(test1, null); };
Test test = new Test();//测试类
Stopwatch sw = new Stopwatch();//系统类:提供一组方法和属性,可用于准确地测量运行时间。
object value;
for (int i = 0; i < 10; i++)
{
sw.Reset();//重置测量时间
sw.Start();//开始计时
for (int j = 0; j < 100000; j++)
{
value = reflect(test);//使用EasyReflect建立的委托
}
sw.Stop();//停止即使
Console.Write(sw.ElapsedMilliseconds + " ms \t\t");//打印时间
sw.Reset();//重置测量时间
sw.Start();//开始计时
for (int j = 0; j < 100000; j++)
{
value = reflect2(test);//使用系统建立的委托
}
sw.Stop();//停止即使
Console.WriteLine(sw.ElapsedMilliseconds + " ms");//打印时间
}
Console.Read();
//直接使用反射调用10个100W次
Test test = new Test();//测试类
Stopwatch sw = new Stopwatch();//系统类:提供一组方法和属性,可用于准确地测量运行时间。
object value;
for (int i = 0; i < 10; i++)
{
sw.Reset();//重置测量时间
sw.Start();//开始计时
for (int j = 0; j < 100000; j++)
{//使用EasyReflect类直接反射
value = EasyReflect.GetPropertyValue(typeof(Test), "Text")(test);
}
sw.Stop();//停止即使
Console.Write(sw.ElapsedMilliseconds + " ms \t\t");//打印时间
sw.Reset();//重置测量时间
sw.Start();//开始计时
for (int j = 0; j < 100000; j++)
{//使用系统直接反射
value = typeof(Test).GetProperty("Text", EasyIL.ALLATTR).GetGetMethod().Invoke(test, null);
}
sw.Stop();//停止即使
Console.WriteLine(sw.ElapsedMilliseconds + " ms");//打印时间
}
Console.Read();
/// <summary>
/// 获取一个获取属性的值的委托
/// </summary>
/// <param name="classType">需要获取属性的类</param>
/// <param name="propertyName">属性名称</param>
public static ReflectGet GetPropertyValue(Type classType, string propertyName)
{
string cacheKey = classType.FullName + SPACE + propertyName;//缓存标识符key
ReflectGet returnDelegate; //声明返回委托对象
if (cacheGet.TryGetValue(cacheKey, out returnDelegate)) //取出缓存中的委托,如果没有返回false
{
return returnDelegate; //如果有,直接返回该委托
}
cacheGet.Add(cacheKey, returnDelegate); //添加到缓存
ReflectGet reflectGet; //声明实际引用的委托
reflectGet = delegate(object obj) //使用系统反射方法
{
return classType.GetProperty(propertyName, EasyIL.ALLATTR).GetGetMethod().Invoke(obj, null);
};
//开启新线程初始化动态方法
ThreadPool.QueueUserWorkItem(delegate(object o)
{
//Type returnType = classType.GetProperty(propertyName, EasyIL.ALLATTR).PropertyType;
Type returnType = typeof(object); //设置返回值类型
Type argType = typeof(object); //设置参数类型
EasyIL il = new EasyIL(returnType, classType.Module, argType); //建立简单EasyIL对象
//EasyIL:自定义类,用于简单操作IL生成器生成动态方法
//ILParameterBase obj = il.Convert(classType, il["0"]); //将参数0转换为
PropertyInfo propertyInfo =
classType.GetProperty(propertyName, EasyIL.ALLATTR); //获得属性对象PropertyInfo
ILProperty objProperty =
il.CreateProperty(propertyInfo, il["0"]); //根据对象和属性对象 创建IL参数
//il[0]表示第0个参数的ILProperty形式,ILProperty:自定义类,用于在EasyIL中代表一个参数
il.Return(objProperty); //return 属性的值
//Type delegateType = typeof(delegateGeneric<,>).MakeGenericType(returnType, classType);
reflectGet = (ReflectGet)il.CreateDelegate(typeof(ReflectGet)); //返回一个委托
});
returnDelegate = delegate(object obj) { return reflectGet(obj); }; //给返回委托赋值
return returnDelegate;
}