c# - Object of type 'System.Reflection.MdFieldInfo' cannot be converted to type 'MyEnum' -
i loading assembly @ runtime , trying lot of operations @ runtime going far anyways trying achieve:
i have class called student in foreign assembly loading @ runtime
public class student { public bool isgood { get; set; } public studenttype st { get; set; } public university university { get; set; } }
i can dynamically load object assembly doing like:
var assembly = assembly.loadfrom("//path"); type type = assembly.gettype("testframework.student"); var student = activator.createinstance(type);
now, comes turn set properties type dynamically loaded foreign assembly.
type universitytype = assembly.gettype("testframework.university"); type.getproperty("university").setvalue(student, activator.createinstance(universitytype), null);
nice part works too!!
but
here problem starts. when try assign studenttype enum same assembly fails error
object of type 'system.reflection.mdfieldinfo' cannot converted to
type 'myenum'
this how trying set it:
type3 = assembly.gettype("testframework.studenttype"); type.getproperty("st").setvalue(student, type3.getfield("good"), null);
this code fails..
i think need field.getvalue
here
type.getproperty("st").setvalue(student, type3.getfield("good").getvalue(instance), null);
type.getfield
returns fieldinfo
need pass parameter enum value.
note: assume good
field on type3
of type enum studenttype
Comments
Post a Comment