InvokeMember "Member Not Found" when attempting to set a property
-
I’m dabbling with some of .Nets reflection features and am running into the following problem… I have the following code,
Assembly myAssembly = AppDomain.CurrentDomain.Load("MyAssembly"); Type myType = localDataAssm.GetType("MyNameSpace.MyType”, true, true); Object myObjectInstance = Activator.CreateInstance(myType); myType.InvokeMember("MyProperty", System.Reflection.BindingFlags.SetProperty , null, myObjectInstance, new object[] {"blah"});
The InvokeMember step fails with “Member Not Found”, even though the said property does indeed exist. This seems to be straight forward to me so what am I missing?? Woke up this morning...and got myself a blog -
I’m dabbling with some of .Nets reflection features and am running into the following problem… I have the following code,
Assembly myAssembly = AppDomain.CurrentDomain.Load("MyAssembly"); Type myType = localDataAssm.GetType("MyNameSpace.MyType”, true, true); Object myObjectInstance = Activator.CreateInstance(myType); myType.InvokeMember("MyProperty", System.Reflection.BindingFlags.SetProperty , null, myObjectInstance, new object[] {"blah"});
The InvokeMember step fails with “Member Not Found”, even though the said property does indeed exist. This seems to be straight forward to me so what am I missing?? Woke up this morning...and got myself a blogSome things to check: 1) Is your property ReadOnly? 2) Is is private? 3) What does the QuckWatch window say when you open it for "myObjectInstance" ? Regards, Serge (Logic Software, Easy Projects .NET site)
-
Some things to check: 1) Is your property ReadOnly? 2) Is is private? 3) What does the QuckWatch window say when you open it for "myObjectInstance" ? Regards, Serge (Logic Software, Easy Projects .NET site)
Hi Serge, thanks for replying. The property is not readonly or private, it looks like this...
public string MyProperty { get { return _myPropertyValue; } set { _myPropertyValue = value; } }
However, the quickwatch window gives me what I expect to see. All the fields and properties are initialized to null or 0 (strings and ints) Woke up this morning...and got myself a blog -
Hi Serge, thanks for replying. The property is not readonly or private, it looks like this...
public string MyProperty { get { return _myPropertyValue; } set { _myPropertyValue = value; } }
However, the quickwatch window gives me what I expect to see. All the fields and properties are initialized to null or 0 (strings and ints) Woke up this morning...and got myself a blogHi again, Noticed one thing - you get a reference to the assembly to the var called "myAssembly" but you get a particular type with the following line (localDataAssm): Type myType = localDataAssm.GetType("MyNameSpace.MyType”, true, true); Object myObjectInstance = Activator.CreateInstance(myType); I'll give an example on how I instantiate objects via Reflection:
' persistedType should be a full type name, e.g. "MyNamespace.MyClass" function myCreateObject(persistedType as string) return AppDomain.CurrentDomain.CreateInstanceAndUnwrap("HereGoesAssemblyFriendlyName", persistedType) end function
Regards, Serge (Logic Software, Easy Projects .NET site) -
Hi again, Noticed one thing - you get a reference to the assembly to the var called "myAssembly" but you get a particular type with the following line (localDataAssm): Type myType = localDataAssm.GetType("MyNameSpace.MyType”, true, true); Object myObjectInstance = Activator.CreateInstance(myType); I'll give an example on how I instantiate objects via Reflection:
' persistedType should be a full type name, e.g. "MyNamespace.MyClass" function myCreateObject(persistedType as string) return AppDomain.CurrentDomain.CreateInstanceAndUnwrap("HereGoesAssemblyFriendlyName", persistedType) end function
Regards, Serge (Logic Software, Easy Projects .NET site)Thanks Serge, I didn't know about that particular method. In any case, it turns out I wasn't casting my parameter to the correct type and that was the problem. Thanks for your help Serge, I appreciate it :-) Woke up this morning...and got myself a blog
-
Thanks Serge, I didn't know about that particular method. In any case, it turns out I wasn't casting my parameter to the correct type and that was the problem. Thanks for your help Serge, I appreciate it :-) Woke up this morning...and got myself a blog
No problem :) Regards, Serge (Logic Software, Easy Projects .NET site)