Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ public override IList<CustomAttributeData> GetCustomAttributesData()

#region MemberInfo Overrides
public override string Name => RuntimeMethodHandle.GetName(this);
public override MemberTypes MemberType => MemberTypes.Constructor;

public override Type? DeclaringType => m_reflectedTypeCache.IsGlobal ? null : m_declaringType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ public sealed override bool ContainsGenericParameters

public abstract override Type DeclaringType { get; }

public sealed override Type[] GetGenericArguments()
{
// Constructors cannot be generic. Desktop compat dictates that We throw NotSupported rather than returning a 0-length array.
throw new NotSupportedException();
}

[RequiresUnreferencedCode("Trimming may change method bodies. For example it can change some instructions, remove branches or local variables.")]
public sealed override MethodBody GetMethodBody()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ protected ConstructorInfo() { }

public override MemberTypes MemberType => MemberTypes.Constructor;
Comment thread
steveisok marked this conversation as resolved.

// Constructors are never generic methods, so the generic method arguments are always empty.
public override Type[] GetGenericArguments() => [];
Comment thread
steveisok marked this conversation as resolved.

[DebuggerHidden]
[DebuggerStepThrough]
public object Invoke(object?[]? parameters) => Invoke(BindingFlags.Default, binder: null, parameters: parameters, culture: null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ public void IsDefined_ReturnsFalseForUnattributedConstructor()
}

[Fact]
public void GetGenericArguments_ThrowsNotSupported()
public void GetGenericArguments_ReturnsEmpty()
{
// Constructors don't support GetGenericArguments
Assert.Throws<NotSupportedException>(() => _customConstructor.GetGenericArguments());
// Constructors are never generic methods, so GetGenericArguments returns an empty array.
Assert.Empty(_customConstructor.GetGenericArguments());
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMethodBodySupported))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ private static void TestConstructorInfoCommonInvariants(this ConstructorInfo c)
Assert.False(c.IsConstructedGenericMethod());
Assert.False(c.IsGenericMethod);

#if NET
Assert.Empty(c.GetGenericArguments());
#else
// On .NET Framework, ConstructorInfo.GetGenericArguments() throws NotSupportedException.
Assert.Throws<NotSupportedException>(() => c.GetGenericArguments());
#endif
}

private static void TestMethodInfoCommonInvariants(this MethodInfo m)
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12309,6 +12309,7 @@ public abstract partial class ConstructorInfo : System.Reflection.MethodBase
protected ConstructorInfo() { }
public override System.Reflection.MemberTypes MemberType { get { throw null; } }
public override bool Equals(object? obj) { throw null; }
public override System.Type[] GetGenericArguments() { throw null; }
public override int GetHashCode() { throw null; }
public object Invoke(object?[]? parameters) { throw null; }
public abstract object Invoke(System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? parameters, System.Globalization.CultureInfo? culture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ public void IsPublic()
Assert.True(constructors[0].IsPublic);
}

[Fact]
public void GetGenericArguments_ReturnsEmptyArray()
{
ConstructorInfo[] constructors = GetConstructors(typeof(ClassWith3Constructors));
Assert.All(constructors, constructorInfo => Assert.Empty(constructorInfo.GetGenericArguments()));

ConstructorInfo[] genericTypeConstructors = GetConstructors(typeof(GenericClassWithConstructor<int>));
Assert.All(genericTypeConstructors, constructorInfo => Assert.Empty(constructorInfo.GetGenericArguments()));

ConstructorInfo[] openGenericTypeConstructors = GetConstructors(typeof(GenericClassWithConstructor<>));
Assert.All(openGenericTypeConstructors, constructorInfo => Assert.Empty(constructorInfo.GetGenericArguments()));
}
Comment thread
steveisok marked this conversation as resolved.

// Use this class only from the Invoke_StaticConstructorMultipleTimes method
public static class ClassWithStaticConstructorThatIsCalledMultipleTimesViaReflection
{
Expand All @@ -159,6 +172,12 @@ public class ConstructorInfoDerived : ConstructorInfoAbstractBase
public ConstructorInfoDerived() { }
}

public class GenericClassWithConstructor<T>
{
public GenericClassWithConstructor() { }
public GenericClassWithConstructor(T value) { }
}

public class ClassWith3Constructors
{
public int intValue = 0;
Expand Down
Loading