oop - C# - How to access member variable from reference to "object" class that refer to some other object type -
i have reference class object pointed class, say, xyz follow:
public class xyz { int x; int y; int z; xyz() { x=0; y=1; z=2; } }; object objref; objref = new xyz(); now, want access member variable x, y , z through objref. how can achieve this?
thanks in advance.
edit :
the class xyz inside client's dll. have loaded class using
assembly myassembly = assembly.loadfrom(assemblyname) type xyztype = myassembly.gettype("xyz"); object objectref = activator.createinstance(xyztype); so don't have direct access xyz. have object reference pointed xyz.
your classes using private variables. should use reflection access private property private variables when must create instance via outside assembly cannot access directly.
you can use reflector bindingflags.nonpublic , bindingflags.instance flags
fieldinfo[] fields = typeof(xyz).getfields( bindingflags.nonpublic | bindingflags.instance); and access private member following code statement below:
object objref = new xyz(); int x = (int)fields.single(f => f.name.equals("x")).getvalue(objref);
Comments
Post a Comment