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 Problem !

Reflection Problem !

Scheduled Pinned Locked Moved C#
questionhelp
8 Posts 4 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Dear Friends, assume the following namespace:

    namespace MyNameSpace
     {
    	public struct MyStruct
    	{
              private int intVar;
    
              public int IntVar
               {
                get { return intVar; }
                set { intVar = value; }
               }
            }
    
            public class MyClass
            {
                private MyStruct structVar;
                //Public property.
                public MyStruct StructVar
                {
                    get { return structVar; }
                    set { structVar = value; }
                }
                public MyClass()
                {
                }
            }
    }
    }
    

    This class is in a dll file (MyAssembly.dll).I wanna load this dll at runtime by reflection and change the IntVar property of StructVar of my class instance by SetValue method of PropertyInfo class.

    namespace MyApplication
    {
        public class MainClass
        {
            public void ChangeProperty()
            {
                Assembly asm = Assembly.LoadFrom("MyAssembly.dll");
                Type t = asm.GetType("MyNamespace.MyClass");
                PropertyInfo pInfo = t.GetProperty("StructVar");
                //Here is my problem.How can I change 'IntVar' member of this structure instance of MyClass ?
                
                pInfo.SetValue(???);
            }
        }
    }
    

    Note that this class is in a seperate dll. Best Regards. [ _ Always there is another way _ ]

    R S S 3 Replies Last reply
    0
    • L Lost User

      Dear Friends, assume the following namespace:

      namespace MyNameSpace
       {
      	public struct MyStruct
      	{
                private int intVar;
      
                public int IntVar
                 {
                  get { return intVar; }
                  set { intVar = value; }
                 }
              }
      
              public class MyClass
              {
                  private MyStruct structVar;
                  //Public property.
                  public MyStruct StructVar
                  {
                      get { return structVar; }
                      set { structVar = value; }
                  }
                  public MyClass()
                  {
                  }
              }
      }
      }
      

      This class is in a dll file (MyAssembly.dll).I wanna load this dll at runtime by reflection and change the IntVar property of StructVar of my class instance by SetValue method of PropertyInfo class.

      namespace MyApplication
      {
          public class MainClass
          {
              public void ChangeProperty()
              {
                  Assembly asm = Assembly.LoadFrom("MyAssembly.dll");
                  Type t = asm.GetType("MyNamespace.MyClass");
                  PropertyInfo pInfo = t.GetProperty("StructVar");
                  //Here is my problem.How can I change 'IntVar' member of this structure instance of MyClass ?
                  
                  pInfo.SetValue(???);
              }
          }
      }
      

      Note that this class is in a seperate dll. Best Regards. [ _ Always there is another way _ ]

      R Offline
      R Offline
      rudy net
      wrote on last edited by
      #2

      First create an instance of your class using Activator. Then you should be able to access your structure as usual. Example: Type t = asm.GetType("MyNamespace.MyClass"); MyNamespace.MyClass cls = Activator.CreateInstance(t); cls.StructVar.IntVar = 4;

      L 1 Reply Last reply
      0
      • L Lost User

        Dear Friends, assume the following namespace:

        namespace MyNameSpace
         {
        	public struct MyStruct
        	{
                  private int intVar;
        
                  public int IntVar
                   {
                    get { return intVar; }
                    set { intVar = value; }
                   }
                }
        
                public class MyClass
                {
                    private MyStruct structVar;
                    //Public property.
                    public MyStruct StructVar
                    {
                        get { return structVar; }
                        set { structVar = value; }
                    }
                    public MyClass()
                    {
                    }
                }
        }
        }
        

        This class is in a dll file (MyAssembly.dll).I wanna load this dll at runtime by reflection and change the IntVar property of StructVar of my class instance by SetValue method of PropertyInfo class.

        namespace MyApplication
        {
            public class MainClass
            {
                public void ChangeProperty()
                {
                    Assembly asm = Assembly.LoadFrom("MyAssembly.dll");
                    Type t = asm.GetType("MyNamespace.MyClass");
                    PropertyInfo pInfo = t.GetProperty("StructVar");
                    //Here is my problem.How can I change 'IntVar' member of this structure instance of MyClass ?
                    
                    pInfo.SetValue(???);
                }
            }
        }
        

        Note that this class is in a seperate dll. Best Regards. [ _ Always there is another way _ ]

        S Offline
        S Offline
        Stefan Troschuetz
        wrote on last edited by
        #3

        This should do the trick:

        Assembly asm = Assembly.LoadFrom("MyAssembly.dll");
        Type t = asm.GetType("MyNamespace.MyClass");
        Object obj = Activator.CreateInstance(t);
        PropertyInfo pInfo = t.GetProperty("StructVar");
        PropertyInfo pInfo2 = pInfo.PropertyType.GetProperty("IntVar");
        pInfo2.SetValue(1, pInfo.GetValue(obj, null), null);


        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        www.troschuetz.de -- modified at 13:33 Sunday 7th May, 2006

        S 1 Reply Last reply
        0
        • L Lost User

          Dear Friends, assume the following namespace:

          namespace MyNameSpace
           {
          	public struct MyStruct
          	{
                    private int intVar;
          
                    public int IntVar
                     {
                      get { return intVar; }
                      set { intVar = value; }
                     }
                  }
          
                  public class MyClass
                  {
                      private MyStruct structVar;
                      //Public property.
                      public MyStruct StructVar
                      {
                          get { return structVar; }
                          set { structVar = value; }
                      }
                      public MyClass()
                      {
                      }
                  }
          }
          }
          

          This class is in a dll file (MyAssembly.dll).I wanna load this dll at runtime by reflection and change the IntVar property of StructVar of my class instance by SetValue method of PropertyInfo class.

          namespace MyApplication
          {
              public class MainClass
              {
                  public void ChangeProperty()
                  {
                      Assembly asm = Assembly.LoadFrom("MyAssembly.dll");
                      Type t = asm.GetType("MyNamespace.MyClass");
                      PropertyInfo pInfo = t.GetProperty("StructVar");
                      //Here is my problem.How can I change 'IntVar' member of this structure instance of MyClass ?
                      
                      pInfo.SetValue(???);
                  }
              }
          }
          

          Note that this class is in a seperate dll. Best Regards. [ _ Always there is another way _ ]

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          You simply have to call GetProperty again for IntVar, this time on pInfo.PropertyType. That will get you a reference to IntVar's property info, which you can then use to call SetValue. Just remember to call SetValue on pInfo again, as the struct instance you have is a boxed instance and setting it's member values will not affect the original struct.

          object o = Activator.CreateInstance(typeof(MyClass));
          PropertyInfo structPropertyInfo = typeof(MyClass).GetProperty("StructVar");
          object s = structPropertyInfo.GetValue(o, null);
          PropertyInfo intPropertyInfo = structPropertyInfo.PropertyType.GetProperty("IntVar");
          intPropertyInfo.SetValue(s, 2, null);
          structPropertyInfo.SetValue(o, s, null);
          MyClass a = (MyClass)o;
          Console.WriteLine(a.StructVar.IntVar);

          Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacro

          L 1 Reply Last reply
          0
          • S Stefan Troschuetz

            This should do the trick:

            Assembly asm = Assembly.LoadFrom("MyAssembly.dll");
            Type t = asm.GetType("MyNamespace.MyClass");
            Object obj = Activator.CreateInstance(t);
            PropertyInfo pInfo = t.GetProperty("StructVar");
            PropertyInfo pInfo2 = pInfo.PropertyType.GetProperty("IntVar");
            pInfo2.SetValue(1, pInfo.GetValue(obj, null), null);


            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

            www.troschuetz.de -- modified at 13:33 Sunday 7th May, 2006

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            Stefan Troschütz wrote:

            pInfo2.SetValue(1, pInfo.GetValue(obj, null), null);

            That won't work, pInfo.GetValue(...) will return a boxed instance of the struct, so setting it's IntVar property won't affect the original struct. As I suggested in my post below, you can call pInfo.SetValue to set the new struct, but I wonder if there's a better way? Maybe an API function to get the struct by reference? Or is this what the compiler does when you call myClass.StructVar.IntVar = 2? Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacro

            S 1 Reply Last reply
            0
            • S S Senthil Kumar

              Stefan Troschütz wrote:

              pInfo2.SetValue(1, pInfo.GetValue(obj, null), null);

              That won't work, pInfo.GetValue(...) will return a boxed instance of the struct, so setting it's IntVar property won't affect the original struct. As I suggested in my post below, you can call pInfo.SetValue to set the new struct, but I wonder if there's a better way? Maybe an API function to get the struct by reference? Or is this what the compiler does when you call myClass.StructVar.IntVar = 2? Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacro

              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #6

              Ups, I didn't thought so far and the code snippet was a quick, untested shot :-( Thanks for the correction. Currently no solution comes to my mind, that's better than yours. Would be interesting to dig a bit deeper into this, when having more time than now.


              "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

              www.troschuetz.de

              1 Reply Last reply
              0
              • R rudy net

                First create an instance of your class using Activator. Then you should be able to access your structure as usual. Example: Type t = asm.GetType("MyNamespace.MyClass"); MyNamespace.MyClass cls = Activator.CreateInstance(t); cls.StructVar.IntVar = 4;

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Deart Rudy. Thanx for your attention.But MyNamespace.MyClass is in a separate dll file and I haven't it at compile time.If I write this code I will receive the following error : The type or namespace name 'MyNamespace' could not be found (are you missing a using directive or an assembly reference?) Everything is at Runtime :confused: :sigh: [ _ Always there is another way _ ]

                1 Reply Last reply
                0
                • S S Senthil Kumar

                  You simply have to call GetProperty again for IntVar, this time on pInfo.PropertyType. That will get you a reference to IntVar's property info, which you can then use to call SetValue. Just remember to call SetValue on pInfo again, as the struct instance you have is a boxed instance and setting it's member values will not affect the original struct.

                  object o = Activator.CreateInstance(typeof(MyClass));
                  PropertyInfo structPropertyInfo = typeof(MyClass).GetProperty("StructVar");
                  object s = structPropertyInfo.GetValue(o, null);
                  PropertyInfo intPropertyInfo = structPropertyInfo.PropertyType.GetProperty("IntVar");
                  intPropertyInfo.SetValue(s, 2, null);
                  structPropertyInfo.SetValue(o, s, null);
                  MyClass a = (MyClass)o;
                  Console.WriteLine(a.StructVar.IntVar);

                  Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacro

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Dear Senthil ! Thanx for your fast reply.The solution you provided worked properly. King Regards, Amir. :-D:rose: [ _ Always there is another way _ ]

                  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