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.ProcessEst ablishment .doProcess ing method not found.
Code:
// Build Dynamic Assembly reference using Reflection (RPC)
Assembly assembly = Assembly.Load("MyStudentRe cord");
Type type = assembly.GetType(methodPat h);
object remoteObject = Activator.CreateInstance(t ype);
// 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)typ e.InvokeMe mber("doPr ocessing", 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!?
Reflection in C#!! This works in one different program I have written, but this one is a pain!
It tells me MyStudentRecord.ProcessEst
Code:
// Build Dynamic Assembly reference using Reflection (RPC)
Assembly assembly = Assembly.Load("MyStudentRe
Type type = assembly.GetType(methodPat
object remoteObject = Activator.CreateInstance(t
// 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)typ
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!?
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
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("doProce ssing",Sys tem.Reflec tion.Membe rTypes.Met hod,null) != null)
{
mro = (MessageResponseObject)typ e.InvokeMe mber("doPr ocessing", BindingFlags.InvokeMethod, null, remoteObject, arguments);
}
else
{
throw new Exception ("Not Found");
}
try this
if(type.GetMember("doProce
{
mro = (MessageResponseObject)typ
}
else
{
throw new Exception ("Not Found");
}
ASKER
mmarinov
This is the list from the GetMethods loop:
MyStudentRecord.MessageRes ponseObjec t doProcessing(MyStudentReco rd.Message Object)
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!!!
This is the list from the GetMethods loop:
MyStudentRecord.MessageRes
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!!!
ASKER
If I change my GetMethods call to include binding flags ie GetMethids(BindingFlags.De fault) 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)
The method to be called is public override MessageResponseObject doProcessing(MessageObject
Have you tried the public BindingFlag ?
Martin
Martin
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.ProcessEst ablishment .doProcess ing not found
I put it into the mro line just in case and the same, Method MyStudentRecord.ProcessEst
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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
Martin
in stead of BindingFlags.InvokeMethod