C#反射实例——获取及设置对象属性,运行对象方法

时间:2021-10-28 05:15:55
 
string fieldName; //属性名
string fieldValue; //属性值

object userValue = new object();

EUserInfo user = ToolBase . UserInfo . GetInfo( custID); //对象实例化
Type t = user . GetType();
Type proType = t . GetProperty( fieldName ). GetValue( user , null ). GetType();
Assembly common = Assembly . Load( "Common");
Type commonType = common . GetType( "Common.StringToOtherType");
MethodInfo commonMethod = commonType . GetMethod( "To_" + proType . FullName . Replace( '.' , '_'));
object [] parmArray = new object [] { fieldValue };
userValue = commonMethod . Invoke( null , parmArray);
t . GetProperty( fieldName ). SetValue( user , userValue , null);