Click here to Skip to main content
15,885,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing an AOP against an old legacy class that does not have the benefit of a built in IoC container. I am employing the power of dynamic types, using the DynamicObject derived class. When I use the

mi = obj.GetType().GetMethod(methNm, parmType);

call, if the parameters types (represented by parmType, above) are not exactly the types that the method was declared with, then the above GetMthod(..) will not find the method.

My question to you is: Do you know of a way, potentially using Roslyn's semantic model, to employ the same set of rules that are used by the compiler to match the method in GetMethod(..) to the "best choice" of a method?

What I have tried:

I am reading through the RuntimeBinder hoping to find a clue...
Posted
Updated 27-Jul-18 17:05pm
Comments
Nathan Minier 10-Jan-17 7:48am    
Not in an automagic manner, but you could enumerate through obj.GetType().GetMethods().Select(x => x.GetParameters) to map through a switch.

The problem is that it's a runtime determination, so the compiler can't do a "best-guess".
Avi Farah 11-Jan-17 21:39pm    
Nathan, thank you! I'm hoping to be able use an existing functionality as opposed to building the logic that is in the compiler. When I figure it out I'll post it.

1 solution

The problem with my code above is attempting to find the method using type arguments that do not exist in the original class. The line:

mi = obj.GetType().GetMethod(methNm, parmType);

is the culprit.

What I needed to do is use the Type1.IsAssignableFrom(Type2) to resolve which method to call.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900