hi have different classes same properties , want access peoperties of classes dynamically.
public class1 { public const prop1="some"; } public class2 { public const prop1="some"; }
and in code getting class name this
string classname="session["myclass"].tostring();";//say have class1 now.
and want prop1 value .
something string mypropvalue=classname+".prop1";//my expected result
/// type typ=type.gettype(classname);
please me in getting this
var nameofproperty = "prop1"; var propertyinfo = class1object.gettype().getproperty(nameofproperty); var value = propertyinfo.getvalue(myobject, null);
for static:
var nameofproperty = "prop1"; var propertyinfo = typeof(class1).getproperty("prop1", bindingflags.static); var value = propertyinfo.getvalue(myobject, null);
edit (i made example):
class program { static void main(string[] args) { var list = assembly.load("consoleapplication4").gettypes().tolist(); type ty = type.gettype(list.firstordefault(t => t.name == "foo").tostring()); //this works too: type ty = type.gettype("consoleapplication4.foo"); var prop1 = ty.getproperty("temp", bindingflags.static | bindingflags.public); console.writeline(prop1.getvalue(ty.name, null)); console.readline(); } } public static class foo { private static string = "hello world"; public static string temp { { return a; } } }
Comments
Post a Comment