Link to home
Start Free TrialLog in
Avatar of wooderz
wooderz

asked on

C#: Reflection Method not Found

Experts please help!!!

Reflection in C#!! This works in one different program I have written, but this one is a pain!

It tells me MyStudentRecord.ProcessEstablishment.doProcessing method not found.

Code:
// Build Dynamic Assembly reference using Reflection (RPC)
Assembly assembly = Assembly.Load("MyStudentRecord");
Type type = assembly.GetType(methodPath);
object remoteObject = Activator.CreateInstance(type);
                   
// Build an object array for the arguments to the method (the body of the MessageObject)
object[] arguments = new object[] { m.MessageBody };

if (remoteObject.Equals(null))
    throw new Exception();

mro = (MessageResponseObject)type.InvokeMember("doProcessing", BindingFlags.InvokeMethod, null, remoteObject, arguments);

The exception is thrown at the mro = (Mess... line. The dll "MyStudentRecord" is referenced and I can actually find the doProcessing method if I create the object manually, but this needs to be dynamic as different requests will fire different objects.

Please help!?
Avatar of Gautham Janardhan
Gautham Janardhan

try BindingFlags.Default
 in stead of BindingFlags.InvokeMethod
Hello wooderz,

are you sure that you are trying to invoke method on the correct object? try to debug the code and check on the problem line by type.GetMethods() if the doProcessing is there

Regards,
Martin
or

try this

if(type.GetMember("doProcessing",System.Reflection.MemberTypes.Method,null) != null)
{  
   mro = (MessageResponseObject)type.InvokeMember("doProcessing", BindingFlags.InvokeMethod, null, remoteObject, arguments);

}

else
{
   throw new Exception ("Not Found");
}
Avatar of wooderz

ASKER

mmarinov

This is the list from the GetMethods loop:
MyStudentRecord.MessageResponseObject doProcessing(MyStudentRecord.MessageObject)
System.Type GetType()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()

So there it is, why doesn't the thing find in InvokeMethod then!!!
Avatar of wooderz

ASKER

If I change my GetMethods call to include binding flags ie GetMethids(BindingFlags.Default) I get nothing in my array..so obviously it is the BindingFlag, what do I choose for an overridden method??

The method to be called is public override MessageResponseObject doProcessing(MessageObject message)
Have you tried the public BindingFlag ?
Martin
Avatar of wooderz

ASKER

In the GetMethods loop, the BindingFlags.Public returned nothing at all.

I put it into the mro line just in case and the same, Method MyStudentRecord.ProcessEstablishment.doProcessing not found
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wooderz

ASKER

mmarinov:

It was my own stupid fault really, but using your method made me see where I was going wrong. My object array for arguments contained objects of the wrong type. So when doing it using your method, the exception was actually "Cannot cast x into y" rather than cannot find. So when I changed my arguments array to the correct type, it worked...with m.Invoke and type.InvokeMember...so is there a best practice between those two, one or the other?? Or is it programmer's choice?
I can not say for sure for this. In my experience i preffer to get the exact method/member that i need and always use getmethod/getproperty and not left the opertion to the InvokeMember. Moslty because (as you have seen) you receive more appropriate exception if there is a problem

Martin