Array´s via reflection...
-
Hello C# Guru´s! I´ve a serious problem with array´s and reflection. I have a assembly source something like that: namespace Test { class TestClass { public int intTest1; public float fltTest1; public int[] arrTest; // default constructor TestClass() { arrTest = new int[12]; } } } For each instance a default constructor defines the array 'arrTest'. I read MetaInforation from this dll and instanced a object from this class. Then I walk through every Field and do something. But if the field is an array I would do something before, and for that I need the Length of the array. I tried following lines: using System; using System.Reflection; namespace Test { class MainClass { [STAThread] static void Main() { Assembly assembly = System.Reflection.Assembly.LoadFrom("Test.dll"); foreach(Type tmpType in assembly.GetTypes()) { if (tmpType.IsClass) { MethodInfo[] m = tmpType.GetMethods(); object myInstance = tmpType.InvokeMember(m[0].Name, BindingFlags.Default | BindingFlags.InvokeMethod, null, tmpType, null); foreach (FieldInfo tmpField in tmpType.GetFields()) { if (tmpField.FieldType.IsArray) { PropertyInfo p = tmpField.FieldType.GetProperty("Length", typeof(int)); int arrLength = (int) p.GetValue(tmpField,null); // ************************************* // } // do something } } } } } } Compile and Link is no problem... But if I debug the lines it stops at // ********* with an Exception: The target object does not match (or something like that) Why? I can´t help myself anymore, what did I wrong? Please help me... With regards Norman-Timo
-
Hello C# Guru´s! I´ve a serious problem with array´s and reflection. I have a assembly source something like that: namespace Test { class TestClass { public int intTest1; public float fltTest1; public int[] arrTest; // default constructor TestClass() { arrTest = new int[12]; } } } For each instance a default constructor defines the array 'arrTest'. I read MetaInforation from this dll and instanced a object from this class. Then I walk through every Field and do something. But if the field is an array I would do something before, and for that I need the Length of the array. I tried following lines: using System; using System.Reflection; namespace Test { class MainClass { [STAThread] static void Main() { Assembly assembly = System.Reflection.Assembly.LoadFrom("Test.dll"); foreach(Type tmpType in assembly.GetTypes()) { if (tmpType.IsClass) { MethodInfo[] m = tmpType.GetMethods(); object myInstance = tmpType.InvokeMember(m[0].Name, BindingFlags.Default | BindingFlags.InvokeMethod, null, tmpType, null); foreach (FieldInfo tmpField in tmpType.GetFields()) { if (tmpField.FieldType.IsArray) { PropertyInfo p = tmpField.FieldType.GetProperty("Length", typeof(int)); int arrLength = (int) p.GetValue(tmpField,null); // ************************************* // } // do something } } } } } } Compile and Link is no problem... But if I debug the lines it stops at // ********* with an Exception: The target object does not match (or something like that) Why? I can´t help myself anymore, what did I wrong? Please help me... With regards Norman-Timo
Norman-Timo wrote: what did I wrong? How about passing the object instance rather than the type ;p
int arrLength = (int) p.GetValue(myInstance,null);
top secret xacc-ide 0.0.1 -
Norman-Timo wrote: what did I wrong? How about passing the object instance rather than the type ;p
int arrLength = (int) p.GetValue(myInstance,null);
top secret xacc-ide 0.0.1Thank´s for your reply! But this is the first thing I tried! I only get an exception that the target object does not match :-( I tried every object which is thinkable but alway´s the exception. Maybe there´s another (nicer) way to get the array length? But the Method 'GetValue(obj,null)' does not work! Maybe somone has another idea of getting the arrayLength via reflection??? Please answer Norman-Timo