比如:
enum NetType
{
unicom,
telecom
}class Config
{
public NetType TheType;
{
get{return theType};
}
private NetType theType
}现在我从配置文件中读出以下信息,
类名是config,属性名TheType,属性值telecom,属性类型:NetType那么,我如何通过反射的方法,将Config的实例 config 的 TheType 字段值设置为 NetType.telecom 呢?目前我的代码大致是这样的:config.GetType().InvokeMember

"TheType", 
BindingFlags.SetProperty, 
null, 
config.GetType().InvokeMember( null, BindingFlags.Public, null, null, null ), 
new object[]{ /* 形成一个枚举值,不会写 ! */ ) 
}
);

解决方案 »

  1.   

    不好意思 那个config类写的有问题class Config
    {
    public NetType TheType;
    {
    set{theType =value};
    }
    private NetType theType
    }
      

  2.   

    object o = Enum.Parse( typeof(NetType ) , "telecom")
      

  3.   

    object o = NetType.telecom ;
      

  4.   

    to:hdt(倦怠) object o = Enum.Parse( typeof(NetType ) , "telecom")关键是 nettype 事先还不知道啊!
      

  5.   

    config.GetType().InvokeMember

    "TheType", 
    BindingFlags.SetProperty, 
    null, 
    config.GetType().InvokeMember( null, BindingFlags.Public, null, null, null ), 
    new object[]{ /* 形成一个枚举值,不会写 ! */ ) 
    }
    );
    这一段是有问题的。你先说这个方法要实现什么功能吧
      

  6.   

    or
    Assembly assembly = Assembly.load( .... 
    assembly.GetType( "type string" )
      

  7.   

    try..            config.GetType().InvokeMember("TheType", BindingFlags.SetProperty, null, config, new object[] { Enum.Parse(typeof(NetType), "telecom") });
      

  8.   

    TO:关键是 nettype 事先还不知道啊!那你肯定得先知道这个类型啊..
      

  9.   

    Assembly l_Assembly = Assembly.LoadFrom("c:\Config.dll" )//看你Config在哪个程序集里,例"c:\Config.dll."
    Type[] l_typeArray = l_Assembly.GetTypes(); foreach (Type type in l_typeArray)
                    {
                        //取得方法名
                        MethodInfo l_method = type.GetMethod("TheType");
                        if (l_method != null)
                        {
                            //动态创建实例
                            object o = Activator.CreateInstance(type);
                            
                            o.TheType = xxx
                        }
                    }
      

  10.   

    TO:类名是config,属性名TheType,属性值telecom,属性类型:NetType你这里已经得到了属性类型的名了..先通过类型名得到类型,即Type.GetType("name");
      

  11.   

    config.GetType().GetMember("TheType").GetType()能得到TheType的类型,但不知道lz到底要实现什么样的功能,感觉这里没必要用反射。
      

  12.   

    sorry,错了。
    只能用config.GetType().GetProperty("TheType").PropertyType来得到类型,GetMember不行。
      

  13.   

    明白了,类名是config,属性名TheType,属性值telecom,属性类型:NetType都是从配置文件读出来的字符串。
    Type.GetType("config").GetProperty("TheType").SetValue(config,Enum.Parse(Type.GetType("NetType"),"telecom");
    这样应该可以。
      

  14.   

    enum NetType
    {
        unicom,
        telecom
    }
    class Config
    {
        public NetType TheType
        {
            set { theType = value; }
            get { return theType; }
        }
        private NetType theType;
    }private void button1_Click(object sender, EventArgs e)
    {
        Config vConfig = new Config();
        PropertyInfo vPropertyInfo = typeof(Config).GetProperty("TheType");
        if (vPropertyInfo == null) return;
        vPropertyInfo.SetValue(vConfig, Enum.Parse(typeof(NetType), "telecom"), null);
        
        Text = vConfig.TheType.ToString();
    }