Reflection Problem...
-
Hello C#-Comunity! I have a little problem using Reflection under C#. I defined an abstract class with a static method in an assembly '.dll'-file. In source code it looks like:
public abstract class MyClass { // ... public static int MyStaticMethod(int paraNumber) { // ... } }
This code is compiled as a '.dll' file. Then it´s not implemented via Reference in Visual Studio, but as an reflection-load with the 'System.Reflection.Assembly.LoadFile(...)' - statement. My problem is to invoke the static method in this abstract class via reflection. With the statements:Type MyMetaObject = assembly.GetType("MyClass", true, true); object[] parameters = new object[1]; parameters[0] = (object) paraNumber; object retObject = MyClass.("MyStaticMethod", System.Reflection.BindingFlags.InvokeMethod, null, ?X?, parameters);
This doesn´t work :-( I think the problem is the '?X?', because I tried it with 'null', but it causes a runtime error like 'Object reference is not set to an instance of object' [or something like that]. Normally the '?X?' parameter is for an instance of the object which method will be invoked, but I can´t create an instance from an abstract class object! Has anybody some solution proposals? Thanx for any help! Ciao Norman-Timo -
Hello C#-Comunity! I have a little problem using Reflection under C#. I defined an abstract class with a static method in an assembly '.dll'-file. In source code it looks like:
public abstract class MyClass { // ... public static int MyStaticMethod(int paraNumber) { // ... } }
This code is compiled as a '.dll' file. Then it´s not implemented via Reference in Visual Studio, but as an reflection-load with the 'System.Reflection.Assembly.LoadFile(...)' - statement. My problem is to invoke the static method in this abstract class via reflection. With the statements:Type MyMetaObject = assembly.GetType("MyClass", true, true); object[] parameters = new object[1]; parameters[0] = (object) paraNumber; object retObject = MyClass.("MyStaticMethod", System.Reflection.BindingFlags.InvokeMethod, null, ?X?, parameters);
This doesn´t work :-( I think the problem is the '?X?', because I tried it with 'null', but it causes a runtime error like 'Object reference is not set to an instance of object' [or something like that]. Normally the '?X?' parameter is for an instance of the object which method will be invoked, but I can´t create an instance from an abstract class object! Has anybody some solution proposals? Thanx for any help! Ciao Norman-TimoNorman-Timo wrote: object retObject = MyClass.("MyStaticMethod", System.Reflection.BindingFlags.InvokeMethod, null, ?X?, parameters); 1. You cant use reflection directly on the class. In fact the compiler shouldnt even compile such code... :confused: 2. Your bindingflags are wrong. Try a combo of Static and Public. 3. DONT ever skip steps with reflection. It will save you time figuring out what is wrong. Eg
Type t = typeof(MyClass);
MethodInfo mi = t.GetMethod("MyStaticMethod", bindingflags);
object ret = mi.Invoke(null, params);top secret
Download xacc-ide 0.0.6 now!
See some screenshots -
Norman-Timo wrote: object retObject = MyClass.("MyStaticMethod", System.Reflection.BindingFlags.InvokeMethod, null, ?X?, parameters); 1. You cant use reflection directly on the class. In fact the compiler shouldnt even compile such code... :confused: 2. Your bindingflags are wrong. Try a combo of Static and Public. 3. DONT ever skip steps with reflection. It will save you time figuring out what is wrong. Eg
Type t = typeof(MyClass);
MethodInfo mi = t.GetMethod("MyStaticMethod", bindingflags);
object ret = mi.Invoke(null, params);top secret
Download xacc-ide 0.0.6 now!
See some screenshotsHi leppie! Sorry! I forgot the InvokeMember statement It should be:
object retObject = MyClass.InvokeMember("MyStaticMethod", System.Reflection.BindingFlags.InvokeMethod, null, ?X?, parameters);
And why should this not work? Ok I tried with both Flags, but ths won´t work, too! But my error was much more simple, I detected that my MyClass was 'null' because I forgot the namespace to find my 'MyClass' --> 'Namespace.MyClass'. But why he didn´t throw an error, I don´t know! Thx therefore Leppie, explain why I should make it with your solution? Is it faster? Ciao Norman-Timo