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. Array´s via reflection...

Array´s via reflection...

Scheduled Pinned Locked Moved C#
helpcsharpdata-structuresdebuggingregex
3 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

    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

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

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

      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

      N 1 Reply Last reply
      0
      • L leppie

        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

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

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

        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