nHapiNET / nHapi

nHapi is the .Net port of the original Java project HAPI.
Mozilla Public License 2.0
273 stars 155 forks source link

.Net Language incompatibility #233

Closed kalatchev closed 2 years ago

kalatchev commented 2 years ago

Description In class NHapi.Base.Model.ExtraComponents there is a .net language incompatibility because there are two methods (GetComponent and NumComponents) that have duplicates with a different casing, which works only in C#, but not in a case-insensitive languages such as VB.NET. Example: getComponent and GetComponent. These two methods are uninvokable through VB.NET and generally have to be considered as a non-CLS compliant. Moreover, lower-case variants are marked as deprecated, so IMHO they should be simply removed.

Environmental Details:

Additional details I tried to invoke them with CallByName(obj1, "GetComponent", CallType.Method, 1) but even that doesn't work and I'm getting Exception thrown: 'System.Reflection.AmbiguousMatchException' in Microsoft.VisualBasic.dll.

I searched over the code, and issue SA1300 is found on numerous places, and all they will have the same issue.

milkshakeuk commented 2 years ago

@kalatchev the dotnet CLR is case-sensitive which is what VB.Net uses.

We will remove the duplicate names in the next major release.

In the mean time can you not do something with reflection like this?

Dim methodInfo = GetType(ExtraComponents).GetMethods().FirstOrDefault(Function(m) m.Name = "GetComponent" AndAlso m.GetCustomAttribute(GetType(ObsoleteAttribute), False) Is Nothing)
methodInfo?.Invoke(component, New Object() {val})

you could then perhaps wrap that up in a helper extension method.

kalatchev commented 2 years ago

Thank you @milkshakeuk !

Your suggestion works just fine. The only modification I did is

Dim methodInfo = GetType(ExtraComponents).GetMethods().FirstOrDefault(Function(m) m.Name = "GetComponent")

since string comparison is case sensitive and Function(m) m.Name = "GetComponent" returns correct MethodInfo, there is no need to check for attributes, at least in this given situation.

Once again, thanks!

milkshakeuk commented 2 years ago

@kalatchev no problem, glad it worked for you.