[已解决问题] 反射遇到的问题
提问时间: 2008-05-28 17:55
悬赏分:15 浏览:349 次

这个反射是该怎么写的呀?

Register是CommonUserOperation类下的方法,ICommonUser接口

CommonUserProperty继承了这个接口, 

,我想用反射得到所有属性值,

红颜色 Load()该怎么写呀?我看了MSDN要加引用,不明白,请指点。

public bool Register(ICommonUser user)
        {
            Assembly theAssembly = Assembly.Load(user.ToString());
            if (theAssembly == null) return false;
            Type type = theAssembly.GetType();
            PropertyInfo[] myProperty = type.GetProperties();
            object[] param = new object[myProperty.Length];
            //param = myProperty.CopyTo(param, 0);
            for (int i = 0; i < myProperty.Length; i++)
            {
                param[i] = myProperty[i].GetValue(myProperty, null);
            }
            DiseaseDetails dis = new DiseaseDetails();
            return (bool)dis.Execute("CommonUserRegister", "int", param);
        }

上面代码调用时出错:"未能加载文件或程序集“UserRegister.CommonUserProperty”或它的某一个依赖项。系统找不到指定的文件。"

提问者:weblogical - 初学一级
最佳答案
没有看懂你要干什么。如果你是想取出传入的对象的所有Public属性,下面的代码可能更简单。 public bool Register(ICommonUser user) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(user); object[] param = new object[properties.Count]; foreach (PropertyDescriptor prop in properties) { param[i++] = prop.GetValue(user); } // do others... }
2008/5/29 13:12:01 回答者:Colin Han


提问者对于答案的评价:解决了本质问题。
其它回答(3)
Assembly.Load(string assemblyName)的参数是程序集的名字,而不是一个类的名字。 请参考:http://msdn.microsoft.com/zh-cn/library/ky3942xh(VS.80).aspx
4个月前   回答者:玉开 - 大侠五级
我觉得用Assembly.Load来载入程序集, 和使用Type来加载,我觉的是不是应该Type性能更好? Type应该是直接加载Bin目录下的DLL,而Bin目录下的应该是以及被保存在缓存中了。而Assmebly.Load应该是去相应目录去找程序集,然后加载的吧 ??
4个月前   回答者:荒芜 - 菜鸟二级
SampleAssembly = Assembly.Load(“Cigem.SQLServerDAL.Account”); 其中,Account为类。 这里要求你的dll是强制签名的,否则会报错。未能加载文件或程序集... // Assembly.Load("Cigem.SQLServerDAL, 1.0.0.0, neutral, 6c13c48089e0ee07")
4个月前   回答者:mjgforever - 菜鸟二级
评论
   您需要登录以后才能回答!
 

我要提问

我的问题


快到期问题

> 问题排行榜

相关内容

相关链接