RickStrahl / wwDotnetBridge

.NET Interop for Visual FoxPro made easy
http://west-wind.com/wwDotnetBridge.aspx
MIT License
74 stars 35 forks source link

InvokeStaticMethod with Generic types #2

Closed horch004 closed 8 years ago

horch004 commented 9 years ago

Hello Rick,

Thank you very much for this great project. We made use of the 'old' DotnetExtender but we have now rewriten the code to use your wwDotnetBridge.

I ran into a issue with the InvokeStaticMethod (aka InvokeStaticMethod_Internal) method. I have a Generic class definition like the sample code below:

(I use the () instead of the gt and lt characters else it is not shown in this post)

public class MyList(T) : List(T) { public static string StaticMethod() { return "this is a test"; } }

And a class based on this generic definition like:

public class MyDerivedList : MyList(MyListElement) { (no code for brevity) }

In C# the class MyDerivedList now has the static method StaticMethod inherited from the base class MyList. When I try to call the static method StaticMethod on the MyDerivedList class using the InvokeStaticMethod method of the wwDotnetBridge in VFP I get an error saying the method could not be found.

When I define the StaticMethod directly on the MyDerivedList class overriding the base class the InvokeStaticMethod works.

I traced the problem to the InvokeStaticMethod_Internal type.InvokeMember call. When I add the binding flag BindingFlags.FlattenHierarchy, the static method StaticMethod of the base class is found and invoked. Without the flag I get the 'not found' error again.

The normal, not static, methods inherited from the base class are invoked just fine without this binding flag in the CallMethodCom InvokeMember call.

I don't know the underlying reason of this issue and why the difference between the static and not static methods. But the binding flag just solved my problem.

Maybe you can incorporate the solution into the project.

Best Regards, Wijnand

horch004 commented 9 years ago

Hi Rick,

Can you explain a bit why you won't add the binding flag to the code (wontfix label)? Maybe there is a good reason.

Regards, Wijnand

RickStrahl commented 9 years ago

.NET languages don't support it. Neither C# or VB support instantiating static methods (directly without reflection) on inherited types - it only works on the declared type, just like the default Reflection behavior we are using to call statics. Doing anything different would be unexpected behavior.

The workaround is easy - just call the static on the base class where it is declared.