我在运行时加载程序集,并尝试在运行时执行很多操作,但这太过分了,但反正这就是我努力实现:'System.Reflection.MdFieldInfo'类型的对象无法转换为'MyEnum'类型
我有在国外组装一类叫学生这我在运行时加载
public class Student
{
public bool IsGood { get; set; }
public StudentType St { get; set; }
public University University { get; set; }
}
我可以动态地从装配通过执行类似加载对象:
var assembly = Assembly.LoadFrom("//path");
Type type = assembly.GetType("TestFrameWork.Student");
var student = Activator.CreateInstance(type);
现在轮到设置属性,这些属性也是从外部程序集动态加载的某种类型。
Type UniversityType = assembly.GetType("TestFrameWork.University");
type.GetProperty("University").SetValue(student, Activator.CreateInstance(UniversityType), null);
不错的部分是,它的工作太!
但是
这里是问题开始的地方。当我尝试分配StudentType这是从它失败,错误
同一组件的枚举类型“System.Reflection.MdFieldInfo”的对象不能被转换为
类型“MyEnum”
这是我想设置它:
type3 = assembly.GetType("TestFrameWork.StudentType");
type.GetProperty("St").SetValue(student, type3.GetField("Good"), null);
此代码失败..
你的问题是关于'MdFieldInfo'&'MyEnum'但你永远不向他们展示在你的代码。 – I4V
我很抱歉混淆它不MyEnum它的StudentType和MdFieldInfoe是东西.net是抛出错误。我不知道它来自何处。我最好的猜测是,它是FieldInfo的子类型 – TeaLeave