Reflection and array´s...
-
And here I´m again... I have problems to get a specified Array Length from an assembly file. First here is a extract from the assembly file (source): public class SEGMENT_FLATNESS { public int ID { get{ return 221; } } // date as 8-digit integer in format yyyymmdd public int DATE; // time as 6-digit integer in format hhmmss public int TIME; // test public float[] FLATNESS; public SEGMENT_FLATNESS () { FLATNESS = new float[35]; } } To get the array length I tried following: first I have ceated an instance from the upper class, then I got the related Field and tried to get the Property 'Length' from it. The second way was to invoke the Method GetLength from the array, but both didn´t work. Here my last try: using System; using System.Reflection; namespace DummyTest { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { Assembly testAss = System.Reflection.Assembly.LoadFrom("KorrektesFile"); Type testType = testAss.GetType("SEGMENT_FLATNESS"); object testInvoke = Activator.CreateInstance(testType); FieldInfo testInfo = testType.GetField("FLATNESS"); Type fieldType = testInfo.FieldType; // weiß nicht ob das nötig ist PropertyInfo p = fieldType.GetProperty("Length"); MethodInfo[] testMethodInfo = p.GetAccessors(false); //liefert nur die public get Methode für die Länge!!! testMethodInfo[0].Invoke(testInvoke, null); // ************** string tmpString = testInfo.Name; //dummyZeile wg. debug (kein vorzeitiges Ende) } } } What did I wrong? And how can I get the array length? Please help me... Norman-Timo
-
And here I´m again... I have problems to get a specified Array Length from an assembly file. First here is a extract from the assembly file (source): public class SEGMENT_FLATNESS { public int ID { get{ return 221; } } // date as 8-digit integer in format yyyymmdd public int DATE; // time as 6-digit integer in format hhmmss public int TIME; // test public float[] FLATNESS; public SEGMENT_FLATNESS () { FLATNESS = new float[35]; } } To get the array length I tried following: first I have ceated an instance from the upper class, then I got the related Field and tried to get the Property 'Length' from it. The second way was to invoke the Method GetLength from the array, but both didn´t work. Here my last try: using System; using System.Reflection; namespace DummyTest { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { Assembly testAss = System.Reflection.Assembly.LoadFrom("KorrektesFile"); Type testType = testAss.GetType("SEGMENT_FLATNESS"); object testInvoke = Activator.CreateInstance(testType); FieldInfo testInfo = testType.GetField("FLATNESS"); Type fieldType = testInfo.FieldType; // weiß nicht ob das nötig ist PropertyInfo p = fieldType.GetProperty("Length"); MethodInfo[] testMethodInfo = p.GetAccessors(false); //liefert nur die public get Methode für die Länge!!! testMethodInfo[0].Invoke(testInvoke, null); // ************** string tmpString = testInfo.Name; //dummyZeile wg. debug (kein vorzeitiges Ende) } } } What did I wrong? And how can I get the array length? Please help me... Norman-Timo
Norman-Timo wrote: testMethodInfo[0].Invoke(testInvoke, null); if u want a value, u should assign it! top secret xacc-ide 0.0.1
-
And here I´m again... I have problems to get a specified Array Length from an assembly file. First here is a extract from the assembly file (source): public class SEGMENT_FLATNESS { public int ID { get{ return 221; } } // date as 8-digit integer in format yyyymmdd public int DATE; // time as 6-digit integer in format hhmmss public int TIME; // test public float[] FLATNESS; public SEGMENT_FLATNESS () { FLATNESS = new float[35]; } } To get the array length I tried following: first I have ceated an instance from the upper class, then I got the related Field and tried to get the Property 'Length' from it. The second way was to invoke the Method GetLength from the array, but both didn´t work. Here my last try: using System; using System.Reflection; namespace DummyTest { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { Assembly testAss = System.Reflection.Assembly.LoadFrom("KorrektesFile"); Type testType = testAss.GetType("SEGMENT_FLATNESS"); object testInvoke = Activator.CreateInstance(testType); FieldInfo testInfo = testType.GetField("FLATNESS"); Type fieldType = testInfo.FieldType; // weiß nicht ob das nötig ist PropertyInfo p = fieldType.GetProperty("Length"); MethodInfo[] testMethodInfo = p.GetAccessors(false); //liefert nur die public get Methode für die Länge!!! testMethodInfo[0].Invoke(testInvoke, null); // ************** string tmpString = testInfo.Name; //dummyZeile wg. debug (kein vorzeitiges Ende) } } } What did I wrong? And how can I get the array length? Please help me... Norman-Timo
Norman-Timo wrote: FieldInfo testInfo = testType.GetField("FLATNESS"); Type fieldType = testInfo.FieldType; // weiß nicht ob das nötig ist PropertyInfo p = fieldType.GetProperty("Length"); MethodInfo[] testMethodInfo = p.GetAccessors(false); //liefert nur die public get Methode für die Länge!!! testMethodInfo[0].Invoke(testInvoke, null); // ************** try this rather:
FieldInfo testInfo = testType.GetField("FLATNESS");
Type fieldType = testInfo.FieldType;
PropertyInfo p = fieldType.GetProperty("Length");
int length = (int)p.Invoke(testInvoke, new object[0]);
-
Norman-Timo wrote: FieldInfo testInfo = testType.GetField("FLATNESS"); Type fieldType = testInfo.FieldType; // weiß nicht ob das nötig ist PropertyInfo p = fieldType.GetProperty("Length"); MethodInfo[] testMethodInfo = p.GetAccessors(false); //liefert nur die public get Methode für die Länge!!! testMethodInfo[0].Invoke(testInvoke, null); // ************** try this rather:
FieldInfo testInfo = testType.GetField("FLATNESS");
Type fieldType = testInfo.FieldType;
PropertyInfo p = fieldType.GetProperty("Length");
int length = (int)p.Invoke(testInvoke, new object[0]);
Thanx for the very fast help, but there are compiler errors in your hint. p.Invoke... (-> 'System.Reflection.PropertyInfo' does not contain a definition for 'Invoke') Are there framework differences? I use framework 1.1! But I tried somithing similar and I only got Exceptions like that: "An unhandled exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll Additional information: Object does not match target type. " the Exception is trown with following lines: FieldInfo testInfo = testType.GetField("FLATNESS"); Type fieldType = testInfo.FieldType; PropertyInfo p = fieldType.GetProperty("Length"); int iii = (int) p.GetValue(testInvoke, null); // -> ********** Exception! Please help me (I can´t [under]stand this thing!) Norman-Timo
-
Thanx for the very fast help, but there are compiler errors in your hint. p.Invoke... (-> 'System.Reflection.PropertyInfo' does not contain a definition for 'Invoke') Are there framework differences? I use framework 1.1! But I tried somithing similar and I only got Exceptions like that: "An unhandled exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll Additional information: Object does not match target type. " the Exception is trown with following lines: FieldInfo testInfo = testType.GetField("FLATNESS"); Type fieldType = testInfo.FieldType; PropertyInfo p = fieldType.GetProperty("Length"); int iii = (int) p.GetValue(testInvoke, null); // -> ********** Exception! Please help me (I can´t [under]stand this thing!) Norman-Timo
Sorry that was meant to be GetValue! :) dont pass
null
passnew object[0]
top secret xacc-ide 0.0.1 -
Sorry that was meant to be GetValue! :) dont pass
null
passnew object[0]
top secret xacc-ide 0.0.1I´m very sorry, but this is not working too! I really don´t understand the second parameter? What sense does it make? I always found this method with second parameter is null! Hey, but I found the solution at another forum: int arrLength = (int) p.GetValue(testInfo.GetValue(testInvoke), null); This is the soltion. My problem was the wrong object for the first parameter! So now I can go on with my work. ;-) Anyway thanx a lot for your help @leppie! Norman-Timo