|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
语言是不是不是最重要的?.NET反射供应了在运转时猎取工具范例元数据的路子,使程序能够静态地挪用工具的属性、办法。静态性带来的价值是反射挪用不像基于静态范例的间接挪用那样简便,且缺少范例反省机制,得到了IDE智能提醒,简单堕落;因而,很多伴侣实验对.NET反射举行封装。这个话题是仁者见仁,智者见智,这里我也谈谈本人对.NET反射封装的思绪,请先看上面的示例代码:
staticvoidMain(string[]args)
{
Personliu=newPerson("liu",26);
Reflectorreflector=newReflector(liu);
//猎取属性
stringname=reflector.Property<string>("Name");
intage=reflector.Property<int>("Age");
Console.WriteLine(name+""+age);
//修正属性
age=reflector.SetProperty<int>("Age",27);
Console.WriteLine(name+""+age);
//猎取历程
Proc<string>sayHello=reflector.Proc<string>("SayHello");
sayHello("Ling");
//猎取函数
Func<int>getAge=reflector.Func<int>("GetAge");
age=getAge();
Console.WriteLine(age);
Console.ReadLine();
}
publicclassPerson
{
privatestringname;
privateintage;
publicPerson(stringname,intage)
{
this.name=name;
this.age=age;
}
publicstringName
{
get{returnname;}
}
publicintAge
{
get{returnage;}
set{age=value;}
}
publicvoidSayHello(stringwho)
{
Console.WriteLine("SayHelloto"+who);
}
publicintGetAge()
{
returnage;
}
}
信任您已从代码看出了封装的思绪:使用泛型和泛型托付为静态的反射增加静态的范例束缚。上面我们就来复杂看一下Reflector完成的关头部分:
publicdelegatevoidProc();
publicdelegatevoidProc<T1>(T1arg1);
publicdelegatevoidProc<T1,T2>(T1arg1,T2args);
publicdelegatevoidProc<T1,T2,T3>(T1arg1,T2args,T3arg3);
publicdelegatevoidProc<T1,T2,T3,T4>(T1arg1,T2args,T3arg3,T4arg4);
publicdelegatevoidProc<T1,T2,T3,T4,T5>(T1arg1,T2args,T3arg3,T4arg4,T5arg5);
publicdelegateRFunc<R>();
publicdelegateRFunc<T1,R>(T1arg1);
publicdelegateRFunc<T1,T2,R>(T1arg1,T2args);
publicdelegateRFunc<T1,T2,T3,R>(T1arg1,T2args,T3arg3);
publicdelegateRFunc<T1,T2,T3,T4,R>(T1arg1,T2args,T3arg3,T4arg4);
publicdelegateRFunc<T1,T2,T3,T4,T5,R>(T1arg1,T2args,T3arg3,T4arg4,T5arg5);
publicclassReflector
{
privateobjecttarget;
publicobjectTarget
{
get{returntarget;}
}
publicTProperty<T>(stringname)
{
PropertyInfopi=target.GetType().GetProperty(name,typeof(T));
if(null!=pi&&pi.CanRead)
{
objectvalue=pi.GetValue(target,null);
if(null!=value)
{
return(T)value;
}
}
returndefault(T);
}
publicTSetProperty<T>(stringname,Tvalue)
{
PropertyInfopi=target.GetType().GetProperty(name,typeof(T));
if(null!=pi&&pi.CanWrite)
{
pi.SetValue(target,value,null);
}
returnvalue;
}
publicProcProc(stringname)
{
MethodInfomi=target.GetType().GetMethod(name,Type.EmptyTypes);
if(null!=mi)
{
returnDelegate.CreateDelegate(typeof(Proc),target,mi.Name,false)asProc;
}
returnnull;
}
publicProc<T>Proc<T>(stringname)
{
MethodInfomi=target.GetType().GetMethod(name,newType[]{typeof(T)});
if(null!=mi)
{
returnDelegate.CreateDelegate(typeof(Proc<T>),target,mi.Name,false)asProc<T>;
}
returnnull;
}
publicProc<T1,T2>Proc<T1,T2>(stringname)
{
MethodInfomi=target.GetType().GetMethod(name,newType[]{typeof(T1),typeof(T2)});
if(null!=mi)
{
returnDelegate.CreateDelegate(typeof(Proc<T1,T2>),target,mi.Name,false)asProc<T1,T2>;
}
returnnull;
}
publicProc<T1,T2,T3>Proc<T1,T2,T3>(stringname)
{
//...
}
publicProc<T1,T2,T3,T4>Proc<T1,T2,T3,T4>(stringname)
{
//...
}
publicProc<T1,T2,T3,T4,T5>Proc<T1,T2,T3,T4,T5>(stringname)
{
//...
}
publicFunc<R>Func<R>(stringname)
{
MethodInfomi=target.GetType().GetMethod(name,Type.EmptyTypes);
if(null!=mi)
{
returnDelegate.CreateDelegate(typeof(Func<R>),target,mi.Name,false)asFunc<R>;
}
returnnull;
}
publicFunc<T1,R>Func<T1,R>(stringname)
{
MethodInfomi=target.GetType().GetMethod(name,newType[]{typeof(T1)});
if(null!=mi)
{
returnDelegate.CreateDelegate(typeof(Func<T1,R>),target,mi.Name,false)asFunc<T1,R>;
}
returnnull;
}
publicFunc<T1,T2,R>Func<T1,T2,R>(stringname)
{
//...
}
}
封装的完成其实不庞大,只是使用了泛型和泛型托付为挪用者供应了强范例的属性和办法;除属性和办法的称号是静态的觉得,其他的都能够加上范例束缚。接待就此话题多多交换!
那做企业软件是不是最好用J2EE? |
|