2

I've been reading examples of Reflection for two days and I can't quite seem to piece together everything to fit what I need. In fact I thought I was using reflection, until I noticed my "using System.Reflection" was grayed out. :-)

I have an xml file that contains my class names and the methods contained within. The order of the methods or even the names can change. Once I read them in I want to be able to execute them. Seems simple enough.

I have the following test code:

    // myClassName = "namespace.TransactionServices"
    Type tb = Type.GetType(myClassName);

    // Classes will always have the same constructor
    object classInstance = Activator.CreateInstance (
        tb, 
        new object[] 
        {
            authorization.apiKey, 
            authorization.userName, 
            testData.servicesEndPoint
        });

    //Grab the method I want

    //myMethodName = "GetVersion"
    MethodInfo mymethod = tb.GetMethod(myMethodName);
    // no parameters for this method call
    object result = mymethod.Invoke(classInstance, null);

tb is null. I was just going to work away at trying to get the correct API for creating the class, but I don't even know if the rest of what I have is valid.

Any suggestions?

Thanks

Edit: Added namespace. Activator.CreateInstance is returning error that constructor is not found.. It is there.

johnny g
  • 3,533
  • 1
  • 25
  • 40
Jason
  • 463
  • 1
  • 11
  • 25
  • as an aside, have you taken a peek at CastleWindsor, Unity, NInject, or any other Inversion of Control containers? sounds like a fit for this problem. – johnny g Apr 05 '11 at 18:56

3 Answers3

3

The class name must be fully qualified (including namespace and, most likely, the assembly) in order for it to be resolved. If you know the class is in your current executing assembly, you can use Assembly.GetExecutingAssembly().GetType()

Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
2

Are you sure you specified the complete class name including namespace? without the namespace, the result will be null.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • been a while since i've tried dynamic instantiation, but i believe you need an assembly-qualified name, no? eg "MyAwesomeNamespace.WickedClassName, ReallyRadLibrary" – johnny g Apr 05 '11 at 18:53
  • Thank you. I added the namespace, but now my Activator.CreateInstance is returning an error that the constructor is not found. The constructor is there (same name, public). – Jason Apr 05 '11 at 18:54
  • @Jason, could you publish ctor signature? you may wish to verify/confirm order and type of parameters. is there any other detail on error? – johnny g Apr 05 '11 at 18:59
  • @johnny In the few cases i had to use the method, i didn't need to specify the assembly, but of course in general you are right. MSDN says "If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace." – Botz3000 Apr 05 '11 at 19:02
  • @Johnny, thanks, I swapped the order of the last two. Stupid. Problem solved. – Jason Apr 05 '11 at 19:09
0

For GetType you have to provide a fully qualified name. You can get it for an existing object using

MyObject.GetType().AssemblyQualifiedName;