Announcement

Collapse
No announcement yet.

MethodInfo.MakeGenericMethod()

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    MethodInfo.MakeGenericMethod()

    Hi guys, I see that MethodInfo.MakeGenericMethod() is not implemented in Bridge. I also found an issue in github on this topic (https://github.com/bridgedotnet/Bridge/issues/2894). Will it be available sometimes?

    The thing is, I refactored a Dependency Injection framework to work with Bridge and this is the last missing piece for me. Here's an example of what I need to achieve, is there any alternative solution available on "making generic methods" ?

    private static MethodInfo GetMethod(Type sourceType, BindingFlags bindingFlags, string methodName, Type[] genericTypes, Type[] parameterTypes)
    {            
        List<MethodInfo> methods = sourceType.GetMethods(bindingFlags)  
            .Where(mi => string.Equals(methodName, mi.Name, StringComparison.Ordinal))                    
            .Where(mi => mi.ContainsGenericParameters)                    
            .Where(mi => mi.GetGenericArguments().Length == genericTypes.Length)                    
            .Where(mi => mi.GetParameters().Length == parameterTypes.Length)                    
            .Select( mi => mi.MakeGenericMethod(genericTypes))                    
            .Where(mi => mi.GetParameters().Select(pi => pi.ParameterType).SequenceEqual(parameterTypes))                    
            .ToList();              
    
        var methods = validMethods.ToList();            
        if (methods.Count > 1)            
        {                
            throw new AmbiguousMatchException();            
        }              
        return methods.FirstOrDefault();        
    }
    Thanks a lot,
    Patrik
    Last edited by Patrik; 2019-06-06 @ 11:17 AM.

    #2
    Hello @Parik! Thanks for the follow-up, I've added a comment to the issue pointing you also want it.

    By the way, was it you who upvoted the issue in github? That'd help us decide which feature should be worked on next (many votes = more likely). Of course, it depends on how feasible it is.

    Also the context you provided helps a lot so we know what we're developing for. A deck (versus dotnetfiddle or repl.it) test case also helps, so that we imbue that test case in our unit testing project and ensure the feature continues working.

    So if you're willing to help more, expand the code block you provided into an actual test code (with, maybe, a Console.WriteLine showing what you expect), then we can assess having it work in Deck.

    Comment


      #3
      Originally posted by fabricio.murta View Post
      By the way, was it you who upvoted the issue in github?
      I was who upvoted that issue. My use case was a bit complicated, I was trying to compile a serialization library https://github.com/Cyxor/Cyxor.Serialization for Bridge and I partially achieved it except for some features like MakeGenericMethod, but end up removing all the workarounds (#if BRIDGE_NET) in my code since this is a relatively complex code using pointers among other complicated constructions and I wasn't happy with the result.

      Comment


        #4
        A pull-request for this feature has just been added. Once a review and final testing are complete the featue will be included in the next release of Bridge.

        You can track the status of this feature at:
        https://github.com/bridgedotnet/Bridge/issues/2894

        Hope this helps.

        Comment


          #5
          Sounds good :)

          What about MethodInfo.Invoke method? I found out that there are 2 overloads:

          [Template("Bridge.Reflection.midel({this}, {obj})({*arguments})", "Bridge.Reflection.midel({this}, {obj}).apply(null, {arguments:array})")]
          public extern object Invoke(object obj, params object[] arguments);
          and

          [Template("Bridge.Reflection.midel({this}, {obj}, {typeArguments})({*arguments})", "Bridge.Reflection.midel({this}, {obj}, {typeArguments}).apply(null, {arguments:array})")]
          public extern object Invoke(object obj, Type[] typeArguments, params object[] arguments);
          And I was able to get my code working using the second one.

          I mean, in mscorlib none of Invoke method overloads hase parameter similar to
          Type[] typeArguments
          .
          From what I understood, simply calling this overload of Invoke method works for generic methods and provides us another way of specifying generic parameters, without need to use MakeGenericMethod.

          Can you confirm my assumption? Did you provide it as workaround for unsupported MakeGenericMethod feature?

          Comment


            #6
            Hello @Patrik!

            Not sure about the Invoke method overloads you're talking about, but the feature you asked here has been implemented and is going to make it to the next Bridge release. Currently that would be 17.9.0.

            I don't think the overload you mentioned in your last post was provided as a workaround for the lack of MethodInfo.MakeGenericMethod, but if I understand it well, you won't need this nonstandard path if the implementation is in place, right?..

            Anyway, if with the next Bridge release, you still need to use that overload, please open a new thread, so we can discuss it. Does that sound alright for you?

            Comment


              #7
              Sounds right, loking forward for release ;)

              Comment

              Working...
              X