Skip to content

GetGenericArguments throws NotSupportedException on ConstructorInfo #128041

@taco-indenbosch

Description

@taco-indenbosch

Description

I would expect MethodBase.GetGenericArguments() to return an empty array when calling on any non-generic method including constructors. A NotSupportedException with the message "Derived classes must provide an implementation" is thrown instead when called on a ConstructorInfo object.

Reproduction Steps

This unit test fails:

namespace TestProject1
{
	[TestClass]
	public sealed class Test1
	{
		[TestMethod]
		public void TestMethod1()
		{
			var ctor = typeof(SimpleClass).GetConstructor(new[] { typeof(int) });
			Assert.IsEmpty(ctor.GetGenericArguments());
		}
	}

	public class SimpleClass
	{

		public SimpleClass(int x)
		{ }

	}
}

I have created a tiny project that reproduces the problem - at least on my machine:

Constructor_GetGenericArguments.zip

Expected behavior

GetGenericArguments() should return an empy array.

Actual behavior

The call throws an exception, seemingly because GetGenericArguments() is not overridden in RuntimeConstructorInfo.

Regression?

No response

Known Workarounds

I check MethodBase.IsGenericMethod first, because this is false for constructors; I can then assume that GetGenericArguments() is empty.

Configuration

.NET version: 10.0.203
OS version: Windows 11 Pro 22H2
CPU architecture: x64

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions