Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reflection and array´s...

Reflection and array´s...

Scheduled Pinned Locked Moved C#
questiondata-structuresdebugginghelp
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Norman Timo
    wrote on last edited by
    #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

    L 2 Replies Last reply
    0
    • N 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

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Norman-Timo wrote: testMethodInfo[0].Invoke(testInvoke, null); if u want a value, u should assign it! top secret xacc-ide 0.0.1

      1 Reply Last reply
      0
      • N 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

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        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]);

        top secret xacc-ide 0.0.1

        N 1 Reply Last reply
        0
        • L leppie

          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]);

          top secret xacc-ide 0.0.1

          N Offline
          N Offline
          Norman Timo
          wrote on last edited by
          #4

          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

          L 1 Reply Last reply
          0
          • N 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

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            Sorry that was meant to be GetValue! :) dont pass null pass new object[0] top secret xacc-ide 0.0.1

            N 1 Reply Last reply
            0
            • L leppie

              Sorry that was meant to be GetValue! :) dont pass null pass new object[0] top secret xacc-ide 0.0.1

              N Offline
              N Offline
              Norman Timo
              wrote on last edited by
              #6

              I´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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups