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#
helpcsharpvisual-studioquestion
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#-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

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

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

      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 screenshots

      N 1 Reply Last reply
      0
      • L leppie

        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 screenshots

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

        Hi 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

        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