runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Why do we have IsConstructedGenericType but not IsConstructedGenericMethod?

Open ghost opened this issue 9 years ago • 13 comments

Proposal for New API:

   class MethodBase
   {
        public virtual bool IsConstructedGenericMethod => IsGenericMethod && !IsGenericMethodDefinition;
   }

This is both for completeness (Type has IsConstructedGenericType) and for better expressibility when testing if a MethodBase represents a method instantation.

Right now, the only way to perform that task is through this eyesore:

    if (method.IsGenericMethod && !method.IsGenericMethodDefinition) {...}

This is wordy, requires two calls and requires the use of the 1.0-era IsGenericMethod (whose very use I consider to be a code smell due to its poor conceptual grasp of the distinction between generic methods and their instantiations.)

The proposed implementation allows the new api to give the correct result for 3rd party Reflection providers without any additional work on their part. Declaring it "virtual" (like its IsConstructedGenericType counterpart) makes it possible for providers to override with a more performant version (CoreRT is certainly well positioned to do this.)

ghost avatar Mar 13 '17 17:03 ghost

Makes sense and looks good as proposed.

terrajobst avatar Mar 14 '17 17:03 terrajobst

Adding it to CoreRt now.

CoreClr and CoreFx can be considered up for grabs, for at least the next few days.

ghost avatar Mar 14 '17 19:03 ghost

@AtsushiKan in that case please mark it as "up for grabs" ;-) (I did it this time) And when you work on it / grab it, please assign it to yourself ...

karelz avatar Mar 14 '17 22:03 karelz

IsConstructedGenericMethod now lit up on CoreClr (as part of https://github.com/dotnet/coreclr/pull/10202)

All that's left to do is to add it the contract and tests on corefx.

ghost avatar Mar 15 '17 23:03 ghost

This should be fairly straightforward and somewhat fun with generics - see example of exposing API in dotnet/corefx#16996

karelz avatar Apr 05 '17 08:04 karelz

Hi, I'd like to help resolve this issue seeing as it's up for grabs.

I've created a commit with the change as I've understood it.

  • suggested API in MethodBase
  • extended the MethodBaseTests of System.Runtime.Tests.

I'm not sure about the test coverage though.

DawidTabak avatar Apr 29 '17 13:04 DawidTabak

I think I'm supposed to replace the MethodBase.IsConstructedGenericMethod implementation in ref project with an throw null, if I understand the ref project correctly. I believe the same throw null is needed in MethodBuilder in System.Reflection.Emit Could someone please confirm this?

I also see that the IsConstructedGenericMethod of TypeBuilder is not tested in System.Reflection.Emit tests. Should the IsConstructedGenericMethod of MethodBuilder be tested or should I omit the tests as it is omitted in TypeBuilder tests?

DawidTabak avatar Apr 29 '17 13:04 DawidTabak

Thanks for contribution @DawidTabak! I sent you contributor invitation, so that we can assign this issue to you (GitHub limitation). Please ping me here when you accept the invitation.

Let's move the technical discussion to your PR.

karelz avatar Apr 29 '17 17:04 karelz

Also note that we are finishing .NET Core 2.0 -- we will delay any new APIs and larger changes which are not critical for 2.0 until after May 10 (1.5 weeks). Just setting the expectation ... see https://github.com/dotnet/corefx/issues/17619#issuecomment-296872132 for details

karelz avatar Apr 29 '17 17:04 karelz

Saw you accept @DawidTabak, assigning to you ...

karelz avatar Apr 29 '17 17:04 karelz

@DawidTabak we got the okay to submit this API for 2.0 if we get a PR up in the next 2 or 3 days. If you can't manage that, it's okay, it will just miss 2.0. :)

danmoseley avatar May 04 '17 04:05 danmoseley

@AtsushiKan, So if I declare a method void Foo<T, V>() and then using MakeGenericMethod to create a method void Foo<T, int>() the MethodInfo for the latter will return true for IsConstructedGenericMethod even though it cannot be invoked? If so, what does it buy me to know a method is IsConstructedGenericMethod?

kingces95 avatar Aug 01 '17 19:08 kingces95

If you're looking to discover if a method has open types that need further specialization, the api you're looking for is MethodBase.ContainsGenericParameters

ghost avatar Aug 01 '17 20:08 ghost