c# - How to get Length of array using reflector -
i have library , console program, library dynamically. in library on class exist int array. so. can on program, use reflector array? code of library:
public class class1 { public int [] arrayint; public class1() { arrayint = new int[5] {1,2,3,4,5}; } }
this code of program:
assembly asm = assembly.loadfile(@"c:\testlibrary.dll"); type class1 = asm.gettype("testlibrary.class1") type; var testclass = activator.createinstance(class1); propertyinfo list = class1.getproperty("arrayint"); int[] arraytest = (int[])list.getvalue(testclass, null);//throw exception here console.writeline("length of array: "+arraytest.count); console.writeline("first element: "+arraytest[0]);
you exception because public int[] arrayint;
not property member variable, class1.getproperty(...)
returns null
.
alternative 1) use getmember
instead of getproperty
memberinfo list = class1.getmember("arrayint");
alternative 2) declare property in class1
public int[] arrayint { { return arrayint; } }
and change ethe reflection code to:
propertyinfo list = class1.getproperty("arrayint");
also, please note code shouldn't compile, array not have count
property, length
property. following line should give compilation error:
console.writeline("length of array: "+arraytest.count);
and should read
console.writeline("length of array: "+arraytest.length);
Comments
Post a Comment