Reflection Problem !
-
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 _ ]
-
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 _ ]
-
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 _ ]
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
-
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 _ ]
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
-
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
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 callmyClass.StructVar.IntVar = 2
? Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacro -
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 callmyClass.StructVar.IntVar = 2
? Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacroUps, 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
-
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;
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 _ ]
-
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