GithubHelp home page GithubHelp logo

Comments (7)

ZacharyPatten avatar ZacharyPatten commented on August 29, 2024 1

I'm so dumb. I found a fix... and it was really easy... there is a MetadataToken. If the DeclaringType is generic via IsGenericType, then I just need to call GetGenericTypeDefinition then GetMethods and find the MethodInfo with the matching MetadataToken. Then I will have the generic MethodInfo if it was indeed generic.

I will have this fixed in the next release.

Sorry this took so long. @bert2

from towel.

ZacharyPatten avatar ZacharyPatten commented on August 29, 2024

@bert2 Thanks for the input. I was able to reproduce this issue.

Workaround

There is a workaround you can use at the moment. Rather than supplying a placeholder type in the generics, you can just use empty generics:

Assert.IsNotNull(typeof(MyGenericClass<>).GetMethod("Foo").GetDocumentation());

I removed the T from MyGenericClass<T>. This should work with the current code in Towel. This works with multiple generic parameters too; you just add commas: MyGenericClass<,,,>.

Fix

There is probably a fix so that you do not need to remove the explicit generic types before calling GetDocumentation in Towel. I will look into a fix for that. :)

from towel.

bert2 avatar bert2 commented on August 29, 2024

The accepted answer on my StackOverflow question mentions using GetGenericTypeDefinition() on a constructed generic type to turn it into an open generic type again. This should do the trick.

from towel.

ZacharyPatten avatar ZacharyPatten commented on August 29, 2024

So GetGenericTypeDefinition() is nice, but it works on Type rather than MethodInfo. I'm given a MethodInfo in that overload of GetDocumentation(). I could grab the DeclaringType off the MethodInfo and then call GetGenericTypeDefinition() on the DeclaringType, but then I would have no way to get back to the respective MethodInfo as there could be overloads of that method (I cannot simply look it up by name).

For example, there could be something like this:

class A<T>
{
 void B(int a) {}
 void B(T a) {}
}

Those are two seperate methods, but if we are dealing with type A<int> then they have the same signature.

There is a GetGenericMethodDefinition() but that doesn't appear to do exactly what is needed either.

What is actually needed is a way to tell if a ParameterInfo on a MethodInfo.GetParameters() was defined as a generic type, and if it was, which generic from the DeclaringType is it. I don't think there are any exposed methods for doing this currently without getting dirty and looking into the IL manually (if even possible).

from towel.

ZacharyPatten avatar ZacharyPatten commented on August 29, 2024

I am curious though. Did you run into a use case where you cannot use the generic definition (so that the workaround I mentioned is not possible)?

  • generic: typeof(MyClass<>).GetMethod("MyMethod")
  • non-generic (using int): typeof(MyClass<int>).GetMethod("MyMethod")

Cause what I should probably do is just force people to provide the GetDocumentation() method with the generic types, and throw a ArgumentException if they are not the generic definitions.

from towel.

bert2 avatar bert2 commented on August 29, 2024

Hi @ZacharyPatten, thanks for investigating this.

I'm currently trying to fix a related bug in a tool to auto-generate markdown from an assembly having XML documentation. Through google I found your article/library and just wanted to inform you about this issue.

In my case I might get away by using GetGenericTypeDefinition() on all generic types the tool finds in the specified assembly before descending into the method/parameter analysis.

Your case, however, is more complicated as you explained. To me it seems that clients of your library have to be aware of this and use either the open generic type or GetGenericTypeDefinition() when they hit that problem.

from towel.

bert2 avatar bert2 commented on August 29, 2024

No worries, @ZacharyPatten!

...and now I finally know what the MetadataToken is actually for 😄

from towel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.