// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-// NOTE: These are extension methods in the contract, but plain static methods
-// in this implementation. This is done to avoid confusion around what would
-// look like infinite recursion in the implementation. Callers compiled against
-// the contract will still be able to invoke them as extension methods and get
-// source compatibility with classic reflection code.
-//
-// However, this does not apply if there is no 1:1 correspondence with an instance
-// in mscorlib. New extension methods should be marked with 'this'.
-
namespace System.Reflection
{
public static class TypeExtensions
{
- public static ConstructorInfo? GetConstructor(Type type, Type[] types)
+ public static ConstructorInfo? GetConstructor(this Type type, Type[] types)
{
Requires.NotNull(type, nameof(type));
return type.GetConstructor(types);
}
- public static ConstructorInfo[] GetConstructors(Type type)
+ public static ConstructorInfo[] GetConstructors(this Type type)
{
Requires.NotNull(type, nameof(type));
return type.GetConstructors();
}
- public static ConstructorInfo[] GetConstructors(Type type, BindingFlags bindingAttr)
+ public static ConstructorInfo[] GetConstructors(this Type type, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetConstructors(bindingAttr);
}
- public static MemberInfo[] GetDefaultMembers(Type type)
+ public static MemberInfo[] GetDefaultMembers(this Type type)
{
Requires.NotNull(type, nameof(type));
return type.GetDefaultMembers();
}
- public static EventInfo? GetEvent(Type type, string name)
+ public static EventInfo? GetEvent(this Type type, string name)
{
Requires.NotNull(type, nameof(type));
return type.GetEvent(name);
}
- public static EventInfo? GetEvent(Type type, string name, BindingFlags bindingAttr)
+ public static EventInfo? GetEvent(this Type type, string name, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetEvent(name, bindingAttr);
}
- public static EventInfo[] GetEvents(Type type)
+ public static EventInfo[] GetEvents(this Type type)
{
Requires.NotNull(type, nameof(type));
return type.GetEvents();
}
- public static EventInfo[] GetEvents(Type type, BindingFlags bindingAttr)
+ public static EventInfo[] GetEvents(this Type type, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetEvents(bindingAttr);
}
- public static FieldInfo? GetField(Type type, string name)
+ public static FieldInfo? GetField(this Type type, string name)
{
Requires.NotNull(type, nameof(type));
return type.GetField(name);
}
- public static FieldInfo? GetField(Type type, string name, BindingFlags bindingAttr)
+ public static FieldInfo? GetField(this Type type, string name, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetField(name, bindingAttr);
}
- public static FieldInfo[] GetFields(Type type)
+ public static FieldInfo[] GetFields(this Type type)
{
Requires.NotNull(type, nameof(type));
return type.GetFields();
}
- public static FieldInfo[] GetFields(Type type, BindingFlags bindingAttr)
+ public static FieldInfo[] GetFields(this Type type, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetFields(bindingAttr);
}
- public static Type[] GetGenericArguments(Type type)
+ public static Type[] GetGenericArguments(this Type type)
{
Requires.NotNull(type, nameof(type));
return type.GetGenericArguments();
}
- public static Type[] GetInterfaces(Type type)
+ public static Type[] GetInterfaces(this Type type)
{
Requires.NotNull(type, nameof(type));
return type.GetInterfaces();
}
- public static MemberInfo[] GetMember(Type type, string name)
+ public static MemberInfo[] GetMember(this Type type, string name)
{
Requires.NotNull(type, nameof(type));
return type.GetMember(name);
}
- public static MemberInfo[] GetMember(Type type, string name, BindingFlags bindingAttr)
+ public static MemberInfo[] GetMember(this Type type, string name, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetMember(name, bindingAttr);
}
- public static MemberInfo[] GetMembers(Type type)
+ public static MemberInfo[] GetMembers(this Type type)
{
Requires.NotNull(type, nameof(type));
return type.GetMembers();
}
- public static MemberInfo[] GetMembers(Type type, BindingFlags bindingAttr)
+ public static MemberInfo[] GetMembers(this Type type, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetMembers(bindingAttr);
}
- public static MethodInfo? GetMethod(Type type, string name)
+ public static MethodInfo? GetMethod(this Type type, string name)
{
Requires.NotNull(type, nameof(type));
return type.GetMethod(name);
}
- public static MethodInfo? GetMethod(Type type, string name, BindingFlags bindingAttr)
+ public static MethodInfo? GetMethod(this Type type, string name, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetMethod(name, bindingAttr);
}
- public static MethodInfo? GetMethod(Type type, string name, Type[] types)
+ public static MethodInfo? GetMethod(this Type type, string name, Type[] types)
{
Requires.NotNull(type, nameof(type));
return type.GetMethod(name, types);
}
- public static MethodInfo[] GetMethods(Type type)
+ public static MethodInfo[] GetMethods(this Type type)
{
Requires.NotNull(type, nameof(type));
return type.GetMethods();
}
- public static MethodInfo[] GetMethods(Type type, BindingFlags bindingAttr)
+ public static MethodInfo[] GetMethods(this Type type, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetMethods(bindingAttr);
}
- public static Type? GetNestedType(Type type, string name, BindingFlags bindingAttr)
+ public static Type? GetNestedType(this Type type, string name, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetNestedType(name, bindingAttr);
}
- public static Type[] GetNestedTypes(Type type, BindingFlags bindingAttr)
+ public static Type[] GetNestedTypes(this Type type, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetNestedTypes(bindingAttr);
}
- public static PropertyInfo[] GetProperties(Type type)
+ public static PropertyInfo[] GetProperties(this Type type)
{
Requires.NotNull(type, nameof(type));
return type.GetProperties();
}
- public static PropertyInfo[] GetProperties(Type type, BindingFlags bindingAttr)
+ public static PropertyInfo[] GetProperties(this Type type, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetProperties(bindingAttr);
}
- public static PropertyInfo? GetProperty(Type type, string name)
+ public static PropertyInfo? GetProperty(this Type type, string name)
{
Requires.NotNull(type, nameof(type));
return type.GetProperty(name);
}
- public static PropertyInfo? GetProperty(Type type, string name, BindingFlags bindingAttr)
+ public static PropertyInfo? GetProperty(this Type type, string name, BindingFlags bindingAttr)
{
Requires.NotNull(type, nameof(type));
return type.GetProperty(name, bindingAttr);
}
- public static PropertyInfo? GetProperty(Type type, string name, Type? returnType)
+ public static PropertyInfo? GetProperty(this Type type, string name, Type? returnType)
{
Requires.NotNull(type, nameof(type));
return type.GetProperty(name, returnType);
}
- public static PropertyInfo? GetProperty(Type type, string name, Type? returnType, Type[] types)
+ public static PropertyInfo? GetProperty(this Type type, string name, Type? returnType, Type[] types)
{
Requires.NotNull(type, nameof(type));
return type.GetProperty(name, returnType, types);
}
- public static bool IsAssignableFrom(Type type, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] Type? c)
+ public static bool IsAssignableFrom(this Type type, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] Type? c)
{
Requires.NotNull(type, nameof(type));
return type.IsAssignableFrom(c);
}
- public static bool IsInstanceOfType(Type type, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? o)
+ public static bool IsInstanceOfType(this Type type, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? o)
{
Requires.NotNull(type, nameof(type));
return type.IsInstanceOfType(o);
public static class AssemblyExtensions
{
- public static Type[] GetExportedTypes(Assembly assembly)
+ public static Type[] GetExportedTypes(this Assembly assembly)
{
Requires.NotNull(assembly, nameof(assembly));
return assembly.GetExportedTypes();
}
- public static Module[] GetModules(Assembly assembly)
+ public static Module[] GetModules(this Assembly assembly)
{
Requires.NotNull(assembly, nameof(assembly));
return assembly.GetModules();
}
- public static Type[] GetTypes(Assembly assembly)
+ public static Type[] GetTypes(this Assembly assembly)
{
Requires.NotNull(assembly, nameof(assembly));
return assembly.GetTypes();
public static class EventInfoExtensions
{
- public static MethodInfo? GetAddMethod(EventInfo eventInfo)
+ public static MethodInfo? GetAddMethod(this EventInfo eventInfo)
{
Requires.NotNull(eventInfo, nameof(eventInfo));
return eventInfo.GetAddMethod();
}
- public static MethodInfo? GetAddMethod(EventInfo eventInfo, bool nonPublic)
+ public static MethodInfo? GetAddMethod(this EventInfo eventInfo, bool nonPublic)
{
Requires.NotNull(eventInfo, nameof(eventInfo));
return eventInfo.GetAddMethod(nonPublic);
}
- public static MethodInfo? GetRaiseMethod(EventInfo eventInfo)
+ public static MethodInfo? GetRaiseMethod(this EventInfo eventInfo)
{
Requires.NotNull(eventInfo, nameof(eventInfo));
return eventInfo.GetRaiseMethod();
}
- public static MethodInfo? GetRaiseMethod(EventInfo eventInfo, bool nonPublic)
+ public static MethodInfo? GetRaiseMethod(this EventInfo eventInfo, bool nonPublic)
{
Requires.NotNull(eventInfo, nameof(eventInfo));
return eventInfo.GetRaiseMethod(nonPublic);
}
- public static MethodInfo? GetRemoveMethod(EventInfo eventInfo)
+ public static MethodInfo? GetRemoveMethod(this EventInfo eventInfo)
{
Requires.NotNull(eventInfo, nameof(eventInfo));
return eventInfo.GetRemoveMethod();
}
- public static MethodInfo? GetRemoveMethod(EventInfo eventInfo, bool nonPublic)
+ public static MethodInfo? GetRemoveMethod(this EventInfo eventInfo, bool nonPublic)
{
Requires.NotNull(eventInfo, nameof(eventInfo));
return eventInfo.GetRemoveMethod(nonPublic);
return token;
}
- private static int GetMetadataTokenOrZeroOrThrow(MemberInfo member)
+ private static int GetMetadataTokenOrZeroOrThrow(this MemberInfo member)
{
int token = member.MetadataToken;
public static class MethodInfoExtensions
{
- public static MethodInfo GetBaseDefinition(MethodInfo method)
+ public static MethodInfo GetBaseDefinition(this MethodInfo method)
{
Requires.NotNull(method, nameof(method));
return method.GetBaseDefinition();
public static class PropertyInfoExtensions
{
- public static MethodInfo[] GetAccessors(PropertyInfo property)
+ public static MethodInfo[] GetAccessors(this PropertyInfo property)
{
Requires.NotNull(property, nameof(property));
return property.GetAccessors();
}
- public static MethodInfo[] GetAccessors(PropertyInfo property, bool nonPublic)
+ public static MethodInfo[] GetAccessors(this PropertyInfo property, bool nonPublic)
{
Requires.NotNull(property, nameof(property));
return property.GetAccessors(nonPublic);
}
- public static MethodInfo? GetGetMethod(PropertyInfo property)
+ public static MethodInfo? GetGetMethod(this PropertyInfo property)
{
Requires.NotNull(property, nameof(property));
return property.GetGetMethod();
}
- public static MethodInfo? GetGetMethod(PropertyInfo property, bool nonPublic)
+ public static MethodInfo? GetGetMethod(this PropertyInfo property, bool nonPublic)
{
Requires.NotNull(property, nameof(property));
return property.GetGetMethod(nonPublic);
}
- public static MethodInfo? GetSetMethod(PropertyInfo property)
+ public static MethodInfo? GetSetMethod(this PropertyInfo property)
{
Requires.NotNull(property, nameof(property));
return property.GetSetMethod();
}
- public static MethodInfo? GetSetMethod(PropertyInfo property, bool nonPublic)
+ public static MethodInfo? GetSetMethod(this PropertyInfo property, bool nonPublic)
{
Requires.NotNull(property, nameof(property));
return property.GetSetMethod(nonPublic);
public void GetExportedTypesTest()
{
Assembly executingAssembly = GetType().GetTypeInfo().Assembly;
- Assert.True(executingAssembly.GetExportedTypes().Length >= 60);
+ Assert.True(AssemblyExtensions.GetExportedTypes(executingAssembly).Length >= 60);
}
[Fact]
public void GetModulesTest()
{
Assembly executingAssembly = GetType().GetTypeInfo().Assembly;
- Assert.Equal(1, executingAssembly.GetModules().Length);
+ Assert.Equal(1, AssemblyExtensions.GetModules(executingAssembly).Length);
}
[Fact]
public void GetTypes()
{
Assembly executingAssembly = GetType().GetTypeInfo().Assembly;
- Assert.True(executingAssembly.GetTypes().Length >= 140);
+ Assert.True(AssemblyExtensions.GetTypes(executingAssembly).Length >= 140);
}
}
}
public void Invoke_SZArrayConstructor()
{
Type type = Type.GetType("System.Object[]");
- ConstructorInfo[] constructors = type.GetConstructors();
+ ConstructorInfo[] constructors = TypeExtensions.GetConstructors(type);
Assert.Equal(1, constructors.Length);
ConstructorInfo constructor = constructors[0];
public void Invoke_1DArrayConstructor()
{
Type type = Type.GetType("System.Char[*]");
- MethodInfo getLowerBound = type.GetMethod("GetLowerBound");
- MethodInfo getUpperBound = type.GetMethod("GetUpperBound");
- PropertyInfo getLength = type.GetProperty("Length");
+ MethodInfo getLowerBound = TypeExtensions.GetMethod(type, "GetLowerBound");
+ MethodInfo getUpperBound = TypeExtensions.GetMethod(type, "GetUpperBound");
+ PropertyInfo getLength = TypeExtensions.GetProperty(type, "Length");
- ConstructorInfo[] constructors = type.GetConstructors();
+ ConstructorInfo[] constructors = TypeExtensions.GetConstructors(type);
Assert.Equal(2, constructors.Length);
for (int i = 0; i < constructors.Length; i++)
{
Type type = Type.GetType("System.Int32[,]", false);
- ConstructorInfo[] constructors = type.GetConstructors();
+ ConstructorInfo[] constructors = TypeExtensions.GetConstructors(type);
Assert.Equal(2, constructors.Length);
for (int i = 0; i < constructors.Length; i++)
public void Invoke_LargeDimensionalArrayConstructor()
{
Type type = Type.GetType("System.Type[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]");
- ConstructorInfo[] cia = type.GetConstructors();
+ ConstructorInfo[] cia = TypeExtensions.GetConstructors(type);
Assert.Equal(2, cia.Length);
Assert.Throws<TypeLoadException>(() => Type.GetType("System.Type[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]"));
}
public void Invoke_JaggedArrayConstructor()
{
Type type = Type.GetType("System.String[][]");
- ConstructorInfo[] constructors = type.GetConstructors();
+ ConstructorInfo[] constructors = TypeExtensions.GetConstructors(type);
Assert.Equal(2, constructors.Length);
for (int i = 0; i < constructors.Length; i++)
[MemberData(nameof(Invoke_TestData))]
public void Invoke(Type[] constructorTypeParameters, object[] parameters)
{
- ConstructorInfo constructor = typeof(ConstructorInfoInvoke).GetConstructor(constructorTypeParameters);
+ ConstructorInfo constructor = TypeExtensions.GetConstructor(typeof(ConstructorInfoInvoke), constructorTypeParameters);
object constructedObject = constructor.Invoke(parameters);
Assert.NotNull(constructedObject);
}
[MemberData(nameof(Invoke_Invalid_TestData))]
public void Invoke_Invalid(Type constructorParent, Type[] constructorTypeParameters, object[] parameters, Type exceptionType)
{
- ConstructorInfo constructor = constructorParent.GetConstructor(constructorTypeParameters);
+ ConstructorInfo constructor = TypeExtensions.GetConstructor(constructorParent, constructorTypeParameters);
Assert.Throws(exceptionType, () => constructor.Invoke(parameters));
}
[InlineData(typeof(string), new Type[] { typeof(char), typeof(int) })]
public void Properties(Type type, Type[] typeParameters)
{
- ConstructorInfo constructor = type.GetConstructor(typeParameters);
+ ConstructorInfo constructor = TypeExtensions.GetConstructor(type, typeParameters);
Assert.Equal(type, constructor.DeclaringType);
Assert.Equal(type.GetTypeInfo().Module, constructor.Module);
public static IEnumerable<object[]> AddEventHandler_TestData()
{
EI_Class tc1 = new EI_Class();
- yield return new object[] { typeof(EI_Class).GetEvent("PublicEvent"), tc1, new VoidDelegate(tc1.PublicVoidMethod1), 1 };
- yield return new object[] { typeof(EI_Class).GetEvent("PublicStaticEvent"), null, new VoidDelegate(tc1.ProtectedInternalVoidMethod), 2 };
- yield return new object[] { typeof(EI_Class).GetEvent("PublicStaticEvent"), tc1, new VoidDelegate(tc1.PublicVoidMethod2), 3 };
+ yield return new object[] { TypeExtensions.GetEvent(typeof(EI_Class), "PublicEvent"), tc1, new VoidDelegate(tc1.PublicVoidMethod1), 1 };
+ yield return new object[] { TypeExtensions.GetEvent(typeof(EI_Class), "PublicStaticEvent"), null, new VoidDelegate(tc1.ProtectedInternalVoidMethod), 2 };
+ yield return new object[] { TypeExtensions.GetEvent(typeof(EI_Class), "PublicStaticEvent"), tc1, new VoidDelegate(tc1.PublicVoidMethod2), 3 };
}
[Theory]
{
// Null target for instance method
EI_Class tc1 = new EI_Class();
- yield return new object[] { typeof(EI_Class).GetEvent("PublicEvent"), null, new VoidDelegate(tc1.ProtectedInternalVoidMethod), typeof(TargetException) };
+ yield return new object[] { TypeExtensions.GetEvent(typeof(EI_Class), "PublicEvent"), null, new VoidDelegate(tc1.ProtectedInternalVoidMethod), typeof(TargetException) };
// Event not declared on target
- yield return new object[] { typeof(EI_Class).GetEvent("PublicEvent"), new DummyClass(), new VoidDelegate(tc1.ProtectedInternalVoidMethod), typeof(TargetException) };
+ yield return new object[] { TypeExtensions.GetEvent(typeof(EI_Class), "PublicEvent"), new DummyClass(), new VoidDelegate(tc1.ProtectedInternalVoidMethod), typeof(TargetException) };
// Event does not have a public add accessor
- yield return new object[] { typeof(EI_Class).GetEvent("PrivateEvent", BindingFlags.NonPublic | BindingFlags.Instance), new DummyClass(), new VoidDelegate(tc1.ProtectedInternalVoidMethod), typeof(InvalidOperationException) };
+ yield return new object[] { TypeExtensions.GetEvent(typeof(EI_Class), "PrivateEvent", BindingFlags.NonPublic | BindingFlags.Instance), new DummyClass(), new VoidDelegate(tc1.ProtectedInternalVoidMethod), typeof(InvalidOperationException) };
}
[Theory]
[InlineData(nameof(EI_Class.ProtectedInternalEvent))]
public void Attributes_IsSpecialName(string name)
{
- EventInfo eventInfo = Helpers.GetEvent(typeof(EI_Class), name);
+ EventInfo eventInfo = TypeExtensions.GetEvent(typeof(EI_Class), name, Helpers.AllFlags);
Assert.Equal(EventAttributes.None, eventInfo.Attributes);
Assert.False(eventInfo.IsSpecialName);
}
[InlineData(nameof(EI_Class.ProtectedInternalEvent), typeof(VoidDelegate))]
public void EventHandlerType(string name, Type expected)
{
- EventInfo eventInfo = Helpers.GetEvent(typeof(EI_Class), name);
+ EventInfo eventInfo = TypeExtensions.GetEvent(typeof(EI_Class), name, Helpers.AllFlags);
Assert.Equal(expected, eventInfo.EventHandlerType);
}
[InlineData(nameof(EI_Class.ProtectedInternalEvent), "Void add_ProtectedInternalEvent(System.Reflection.Tests.VoidDelegate)", true)]
public void GetAddMethod(string name, string expectedToString, bool nonPublic)
{
- EventInfo eventInfo = Helpers.GetEvent(typeof(EI_Class), name);
+ EventInfo eventInfo = TypeExtensions.GetEvent(typeof(EI_Class), name, Helpers.AllFlags);
MethodInfo method = eventInfo.GetAddMethod();
Assert.Equal(nonPublic, method == null);
if (method != null)
[InlineData(nameof(EI_Class.ProtectedInternalEvent))]
public void GetRaiseMethod(string name)
{
- EventInfo eventInfo = Helpers.GetEvent(typeof(EI_Class), name);
+ EventInfo eventInfo = TypeExtensions.GetEvent(typeof(EI_Class), name, Helpers.AllFlags);
Assert.Null(eventInfo.GetRaiseMethod());
Assert.Null(eventInfo.GetRaiseMethod(false));
Assert.Null(eventInfo.GetRaiseMethod(true));
[InlineData(nameof(EI_Class.ProtectedInternalEvent), "Void remove_ProtectedInternalEvent(System.Reflection.Tests.VoidDelegate)", true)]
public void GetRemoveMethod(string name, string expectedToString, bool nonPublic)
{
- EventInfo eventInfo = Helpers.GetEvent(typeof(EI_Class), name);
+ EventInfo eventInfo = TypeExtensions.GetEvent(typeof(EI_Class), name, Helpers.AllFlags);
MethodInfo method = eventInfo.GetRemoveMethod();
Assert.Equal(nonPublic, method == null);
if (method != null)
[InlineData("InternalEvent")]
public void DeclaringType_Module(string name)
{
- EventInfo eventInfo = Helpers.GetEvent(typeof(EI_Class), name);
+ EventInfo eventInfo = TypeExtensions.GetEvent(typeof(EI_Class), name, Helpers.AllFlags);
Assert.Equal(typeof(EI_Class), eventInfo.DeclaringType);
Assert.Equal(typeof(EI_Class).GetTypeInfo().Module, eventInfo.Module);
}
[InlineData("InternalEvent")]
public void Name(string name)
{
- EventInfo eventInfo = Helpers.GetEvent(typeof(EI_Class), name);
+ EventInfo eventInfo = TypeExtensions.GetEvent(typeof(EI_Class), name, Helpers.AllFlags);
Assert.Equal(name, eventInfo.Name);
}
}
[InlineData("ConstField", FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal | FieldAttributes.HasDefault, typeof(FI_Enum))]
public void Properties(string name, FieldAttributes attributes, Type fieldType)
{
- FieldInfo field = Helpers.GetField(typeof(FI_BaseClass), name);
+ FieldInfo field = TypeExtensions.GetField(typeof(FI_BaseClass), name, Helpers.AllFlags);
Assert.Equal(name, field.Name);
Assert.Equal(attributes, field.Attributes);
[MemberData(nameof(GetValue_TestData))]
public void GetValue(Type type, string name, object obj, object value)
{
- FieldInfo field = Helpers.GetField(type, name);
+ FieldInfo field = TypeExtensions.GetField(type, name, Helpers.AllFlags);
Assert.Equal(value, field.GetValue(obj));
}
[Fact]
public void GetValue_NullTarget_ThrowsTargetException()
{
- FieldInfo field = Helpers.GetField(typeof(FI_BaseClass), "_privateStringField");
+ FieldInfo field = TypeExtensions.GetField(typeof(FI_BaseClass), "_privateStringField", Helpers.AllFlags);
Assert.Throws<TargetException>(() => field.GetValue(null));
}
}
{
public class Helpers
{
- private const BindingFlags AllFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
-
- public static EventInfo GetEvent(Type type, string name) => type.GetEvent(name, AllFlags);
- public static FieldInfo GetField(Type type, string name) => type.GetField(name, AllFlags);
- public static PropertyInfo GetProperty(Type type, string name) => type.GetProperty(name, AllFlags);
- public static MethodInfo GetMethod(Type type, string name) => type.GetMethod(name, AllFlags);
+ public const BindingFlags AllFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
}
}
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Collections.Generic;
-using System.Linq;
using System.Reflection.Emit;
using Xunit;
public static IEnumerable<object[]> ContainsGenericParameters_TestData()
{
// Methods
- yield return new object[] { Helpers.GetMethod(typeof(NonGenericClass), nameof(NonGenericClass.TestGenericMethod)), true };
- yield return new object[] { Helpers.GetMethod(typeof(NonGenericClass), nameof(NonGenericClass.TestGenericMethod)).MakeGenericMethod(typeof(int)), false };
- yield return new object[] { Helpers.GetMethod(typeof(NonGenericClass), nameof(NonGenericClass.TestMethod)), false };
- yield return new object[] { Helpers.GetMethod(typeof(NonGenericClass), nameof(NonGenericClass.TestPartialGenericMethod)), true };
- yield return new object[] { Helpers.GetMethod(typeof(NonGenericClass), nameof(NonGenericClass.TestGenericReturnTypeMethod)), true };
-
- yield return new object[] { Helpers.GetMethod(typeof(GenericClass<int>), nameof(GenericClass<int>.TestMethod)), false };
- yield return new object[] { Helpers.GetMethod(typeof(GenericClass<>), nameof(GenericClass<int>.TestMethod)), true };
- yield return new object[] { Helpers.GetMethod(typeof(GenericClass<>), nameof(GenericClass<int>.TestMultipleGenericMethod)), true };
- yield return new object[] { Helpers.GetMethod(typeof(GenericClass<int>), nameof(GenericClass<int>.TestMultipleGenericMethod)), true };
- yield return new object[] { Helpers.GetMethod(typeof(GenericClass<>), nameof(GenericClass<int>.TestVoidMethod)), true };
- yield return new object[] { Helpers.GetMethod(typeof(GenericClass<int>), nameof(GenericClass<int>.TestVoidMethod)), false };
+ yield return new object[] { TypeExtensions.GetMethod(typeof(NonGenericClass), nameof(NonGenericClass.TestGenericMethod), Helpers.AllFlags), true };
+ yield return new object[] { TypeExtensions.GetMethod(typeof(NonGenericClass), nameof(NonGenericClass.TestGenericMethod), Helpers.AllFlags).MakeGenericMethod(typeof(int)), false };
+ yield return new object[] { TypeExtensions.GetMethod(typeof(NonGenericClass), nameof(NonGenericClass.TestMethod), Helpers.AllFlags), false };
+ yield return new object[] { TypeExtensions.GetMethod(typeof(NonGenericClass), nameof(NonGenericClass.TestPartialGenericMethod), Helpers.AllFlags), true };
+ yield return new object[] { TypeExtensions.GetMethod(typeof(NonGenericClass), nameof(NonGenericClass.TestGenericReturnTypeMethod), Helpers.AllFlags), true };
+
+ yield return new object[] { TypeExtensions.GetMethod(typeof(GenericClass<int>), nameof(GenericClass<int>.TestMethod), Helpers.AllFlags), false };
+ yield return new object[] { TypeExtensions.GetMethod(typeof(GenericClass<>), nameof(GenericClass<int>.TestMethod), Helpers.AllFlags), true };
+ yield return new object[] { TypeExtensions.GetMethod(typeof(GenericClass<>), nameof(GenericClass<int>.TestMultipleGenericMethod), Helpers.AllFlags), true };
+ yield return new object[] { TypeExtensions.GetMethod(typeof(GenericClass<int>), nameof(GenericClass<int>.TestMultipleGenericMethod), Helpers.AllFlags), true };
+ yield return new object[] { TypeExtensions.GetMethod(typeof(GenericClass<>), nameof(GenericClass<int>.TestVoidMethod), Helpers.AllFlags), true };
+ yield return new object[] { TypeExtensions.GetMethod(typeof(GenericClass<int>), nameof(GenericClass<int>.TestVoidMethod), Helpers.AllFlags), false };
// Constructors
- yield return new object[] { typeof(NonGenericClass).GetConstructor(new Type[0]), false };
- yield return new object[] { typeof(NonGenericClass).GetConstructor(new Type[] { typeof(int) }), false };
+ yield return new object[] { TypeExtensions.GetConstructor(typeof(NonGenericClass), new Type[0]), false };
+ yield return new object[] { TypeExtensions.GetConstructor(typeof(NonGenericClass), new Type[] { typeof(int) }), false };
- foreach (MethodBase constructor in typeof(GenericClass<>).GetConstructors())
+ foreach (MethodBase constructor in TypeExtensions.GetConstructors(typeof(GenericClass<>)))
{
// ContainsGenericParameters should behave same for both methods and constructors.
// If method/ctor or the declaring type contains uninstantiated open generic parameter,
yield return new object[] { constructor, true };
}
- foreach (MethodBase constructor in typeof(GenericClass<int>).GetConstructors())
+ foreach (MethodBase constructor in TypeExtensions.GetConstructors(typeof(GenericClass<int>)))
{
yield return new object[] { constructor, false };
}
[InlineData(typeof(NonGenericClass), "TestMethod2", new Type[] { typeof(int), typeof(float), typeof(string) })]
public void GetMethod_String_Type(Type type, string name, Type[] typeArguments)
{
- MethodInfo method = type.GetMethod(name, typeArguments);
+ MethodInfo method = TypeExtensions.GetMethod(type, name, typeArguments);
Assert.Equal(name, method.Name);
}
[InlineData(typeof(StringImpersonator), nameof(StringImpersonator.IsNullOrEmpty))]
public void Properties(Type type, string name)
{
- MethodInfo method = Helpers.GetMethod(type, name);
+ MethodInfo method = TypeExtensions.GetMethod(type, name, Helpers.AllFlags);
Assert.Equal(type, method.DeclaringType);
Assert.Equal(type.GetTypeInfo().Module, method.Module);
[InlineData(typeof(MI_ClassWithInterface), nameof(MI_ClassWithInterface.MethodA), new Type[0], typeof(MI_ClassWithInterface))]
public void GetBaseDefinition(Type type, string name, Type[] typeArguments, Type declaringType)
{
- MethodInfo method = type.GetMethod(name, typeArguments);
+ MethodInfo method = TypeExtensions.GetMethod(type, name, typeArguments);
MethodInfo baseDefinition = method.GetBaseDefinition();
Assert.Equal(name, baseDefinition.Name);
[MemberData(nameof(GetGenericArguments_TestData))]
public void GetGenericArguments(Type type, string name, string[] argumentNames)
{
- MethodInfo method = Helpers.GetMethod(type, name);
+ MethodInfo method = TypeExtensions.GetMethod(type, name, Helpers.AllFlags);
Type[] arguments = method.GetGenericArguments();
Assert.Equal(argumentNames.Length, arguments.Length);
Assert.Equal(argumentNames, arguments.Select(argumentType => argumentType.Name));
[Fact]
public void Invoke_StringArgument_ReturnsString()
{
- MethodInfo method = typeof(MI_NonGenericClass).GetMethod(nameof(MI_NonGenericClass.MethodA), new Type[] { typeof(string) });
+ MethodInfo method = TypeExtensions.GetMethod(typeof(MI_NonGenericClass), nameof(MI_NonGenericClass.MethodA), new Type[] { typeof(string) });
Assert.Equal("test string", method.Invoke(new MI_NonGenericClass(), new object[] { "test string" }));
}
}
[InlineData(typeof(string), nameof(string.Length))]
public void Properties(Type type, string name)
{
- PropertyInfo property = Helpers.GetProperty(type, name);
+ PropertyInfo property = TypeExtensions.GetProperty(type, name, Helpers.AllFlags);
Assert.Equal(type, property.DeclaringType);
Assert.Equal(type.GetTypeInfo().Module, property.Module);
Assert.Equal(name, property.Name);
[Fact]
public void SetValue_CantWrite_ThrowsArgumentException()
{
- PropertyInfo property = Helpers.GetProperty(typeof(PI_SubClass), nameof(PI_BaseClass.PublicGetPrivateSetProperty));
+ PropertyInfo property = TypeExtensions.GetProperty(typeof(PI_SubClass), nameof(PI_BaseClass.PublicGetPrivateSetProperty), Helpers.AllFlags);
Assert.False(property.CanWrite);
AssertExtensions.Throws<ArgumentException>(null, () => property.SetValue(new PI_SubClass(), 5));
}
[InlineData(typeof(PI_SubClass), nameof(PI_BaseClass.PublicGetPrivateSetProperty), true, false, false, false)]
public void GetGetMethod_GetSetMethod(Type type, string name, bool hasGetter, bool nonPublicGetter, bool hasSetter, bool nonPublicSetter)
{
- PropertyInfo property = Helpers.GetProperty(type, name);
+ PropertyInfo property = TypeExtensions.GetProperty(type, name, Helpers.AllFlags);
VerifyGetMethod(property, property.GetGetMethod(), hasGetter && !nonPublicGetter, nonPublicGetter);
Assert.Equal(property.GetGetMethod(), property.GetGetMethod(false));
[InlineData(typeof(GenericClass<int>), new string[] { "Int32 ReturnAndSetField(Int32)" })]
public void GetDefaultMembers(Type type, string[] expectedNames)
{
- string[] memberNames = type.GetDefaultMembers().Select(member => member.Name).ToArray();
+ string[] memberNames = TypeExtensions.GetDefaultMembers(type).Select(member => member.Name).ToArray();
Assert.Equal(expectedNames.Length, memberNames.Length);
Assert.All(expectedNames, toString => memberNames.Contains(toString));
}
{
if (bindingAttributes == DefaultBindingFlags)
{
- string[] eventNames1 = typeof(Cat<int>).GetEvents().Select(eventInfo => eventInfo.Name).ToArray();
+ string[] eventNames1 = TypeExtensions.GetEvents(typeof(Cat<int>)).Select(eventInfo => eventInfo.Name).ToArray();
Assert.Equal(expectedNames.Length, eventNames1.Length);
Assert.All(expectedNames, name => eventNames1.Contains(name));
}
- string[] eventNames2 = typeof(Cat<int>).GetEvents(bindingAttributes).Select(eventInfo => eventInfo.Name).ToArray();
+ string[] eventNames2 = TypeExtensions.GetEvents(typeof(Cat<int>), bindingAttributes).Select(eventInfo => eventInfo.Name).ToArray();
Assert.Equal(expectedNames.Length, eventNames2.Length);
Assert.All(expectedNames, name => eventNames2.Contains(name));
}
[MemberData(nameof(GetFields_TestData))]
public void GetFields(Type type, string[] expectedNames)
{
- string[] fieldNames = type.GetFields().Select(field => field.Name).ToArray();
+ string[] fieldNames = TypeExtensions.GetFields(type).Select(field => field.Name).ToArray();
Assert.Equal(expectedNames.Length, fieldNames.Length);
Assert.All(expectedNames, name => fieldNames.Contains(name));
}
[InlineData(typeof(TI_Struct?), new Type[0])]
public void GetInterfaces(Type type, Type[] expected)
{
- Type[] interfaces = type.GetInterfaces();
+ Type[] interfaces = TypeExtensions.GetInterfaces(type);
Assert.Equal(expected.Length, interfaces.Length);
Assert.All(expected, interfaceType => interfaces.Equals(interfaceType));
}
[Fact]
public void GetInterfaces_GenericInterfaceWithTypeParameter_ReturnsExpectedToString()
{
- Type[] interfaces = typeof(GenericClassWithInterface<>).GetInterfaces();
+ Type[] interfaces = TypeExtensions.GetInterfaces(typeof(GenericClassWithInterface<>));
Assert.Equal(1, interfaces.Length);
Assert.Equal("System.Reflection.Tests.IGenericInterface`1[T]", interfaces[0].ToString());
}
{
if (bindingAttributes == DefaultBindingFlags)
{
- string[] memberNames1 = type.GetMember(name).Select(member => member.Name).ToArray();
+ string[] memberNames1 = TypeExtensions.GetMember(type, name).Select(member => member.Name).ToArray();
Assert.Equal(expectedNames.Length, memberNames1.Length);
Assert.All(expectedNames, expectedName => memberNames1.Contains(expectedName));
if (name == "*")
Assert.All(memberNamesFromAsterix1, memberInfo => memberNamesFromMethod1.Contains(memberInfo));
}
}
- string[] memberNames2 = type.GetMember(name, bindingAttributes).Select(member => member.Name).ToArray();
+ string[] memberNames2 = TypeExtensions.GetMember(type, name, bindingAttributes).Select(member => member.Name).ToArray();
Assert.Equal(expectedNames.Length, memberNames2.Length);
Assert.All(expectedNames, expectedName => memberNames2.Contains(expectedName));
if (name == "*")
{
- MemberInfo[] memberNamesFromAsterix2 = type.GetMember(name, bindingAttributes);
- MemberInfo[] memberNamesFromMethod2 = type.GetMembers(bindingAttributes);
+ MemberInfo[] memberNamesFromAsterix2 = TypeExtensions.GetMember(type, name, bindingAttributes);
+ MemberInfo[] memberNamesFromMethod2 = TypeExtensions.GetMembers(type, bindingAttributes);
Assert.Equal(memberNamesFromAsterix2.Length, memberNamesFromMethod2.Length);
Assert.All(memberNamesFromAsterix2, memberInfo => memberNamesFromMethod2.Contains(memberInfo));
{
if (bindingAttributes == DefaultBindingFlags)
{
- string[] methodNames1 = type.GetMethods().Select(method => method.Name).ToArray();
+ string[] methodNames1 = TypeExtensions.GetMethods(type).Select(method => method.Name).ToArray();
Assert.Equal(expectedNames.Length, methodNames1.Length);
Assert.All(expectedNames, name => methodNames1.Contains(name));
}
- string[] methodNames2 = type.GetMethods(bindingAttributes).Select(method => method.Name).ToArray();
+ string[] methodNames2 = TypeExtensions.GetMethods(type, bindingAttributes).Select(method => method.Name).ToArray();
Assert.Equal(expectedNames.Length, methodNames2.Length);
Assert.All(expectedNames, name => methodNames2.Contains(name));
}
{
if (bindingAttributes == DefaultBindingFlags)
{
- string[] propertyNames1 = type.GetProperties().Select(method => method.Name).ToArray();
+ string[] propertyNames1 = TypeExtensions.GetProperties(type).Select(method => method.Name).ToArray();
Assert.Equal(expectedNames.Length, propertyNames1.Length);
Assert.All(expectedNames, name => propertyNames1.Contains(name));
}
- string[] propertyNames2 = type.GetProperties(bindingAttributes).Select(method => method.Name).ToArray();
+ string[] propertyNames2 = TypeExtensions.GetProperties(type, bindingAttributes).Select(method => method.Name).ToArray();
Assert.Equal(expectedNames.Length, propertyNames2.Length);
Assert.All(expectedNames, name => propertyNames2.Contains(name));
}
{
if (bindingAttributes == DefaultBindingFlags)
{
- Assert.Equal(name, type.GetProperty(name).Name);
+ Assert.Equal(name, TypeExtensions.GetProperty(type, name).Name);
}
- Assert.Equal(name, type.GetProperty(name, bindingAttributes).Name);
+ Assert.Equal(name, TypeExtensions.GetProperty(type, name, bindingAttributes).Name);
}
else
{
if (types.Length == 0)
{
- Assert.Equal(name, type.GetProperty(name, returnType).Name);
+ Assert.Equal(name, TypeExtensions.GetProperty(type, name, returnType).Name);
}
- Assert.Equal(name, type.GetProperty(name, returnType, types).Name);
+ Assert.Equal(name, TypeExtensions.GetProperty(type, name, returnType, types).Name);
}
}
[MemberData(nameof(IsInstanceOfType_TestData))]
public void IsInstanceOfType(Type type, object value, bool expected)
{
- Assert.Equal(expected, type.IsInstanceOfType(value));
+ Assert.Equal(expected, TypeExtensions.IsInstanceOfType(type, value));
}
[Fact]
public void IsInstanceOfType_NullableTypeAndValue_ReturnsTrue()
{
- Assert.True(typeof(int?).IsInstanceOfType((int?)100));
+ Assert.True(TypeExtensions.IsInstanceOfType(typeof(int?), (int?)100));
}
[Theory]
TypeInfo typeInfo = type.GetTypeInfo();
BindingFlags declaredFlags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
- Assert.Equal(type.GetMethod("ProtectedMethod", declaredFlags), typeInfo.GetDeclaredMethods("ProtectedMethod").First());
- Assert.Equal(type.GetNestedType("PublicNestedType", declaredFlags), typeInfo.GetDeclaredNestedType("PublicNestedType").AsType());
- Assert.Equal(type.GetProperty("PrivateProperty", declaredFlags), typeInfo.GetDeclaredProperty("PrivateProperty"));
-
- Assert.All(type.GetFields(declaredFlags), field => typeInfo.DeclaredFields.Contains(field));
- Assert.All(type.GetMethods(declaredFlags), method => typeInfo.DeclaredMethods.Contains(method));
- Assert.All(type.GetNestedTypes(declaredFlags), nestedType => typeInfo.DeclaredNestedTypes.Contains(nestedType.GetTypeInfo()));
- Assert.All(type.GetProperties(declaredFlags), property => typeInfo.DeclaredProperties.Contains(property));
- Assert.All(type.GetEvents(declaredFlags), eventInfo => typeInfo.DeclaredEvents.Contains(eventInfo));
- Assert.All(type.GetConstructors(declaredFlags), constructor => typeInfo.DeclaredConstructors.Contains(constructor));
-
- Assert.All(type.GetEvents(), eventInfo => typeInfo.AsType().GetEvents().Contains(eventInfo));
- Assert.All(type.GetFields(), fieldInfo => typeInfo.AsType().GetFields().Contains(fieldInfo));
- Assert.All(type.GetMethods(), methodInfo => typeInfo.AsType().GetMethods().Contains(methodInfo));
- Assert.All(type.GetProperties(), propertyInfo => typeInfo.AsType().GetProperties().Contains(propertyInfo));
+ Assert.Equal(TypeExtensions.GetMethod(type, "ProtectedMethod", declaredFlags), typeInfo.GetDeclaredMethods("ProtectedMethod").First());
+ Assert.Equal(TypeExtensions.GetNestedType(type, "PublicNestedType", declaredFlags), typeInfo.GetDeclaredNestedType("PublicNestedType").AsType());
+ Assert.Equal(TypeExtensions.GetProperty(type, "PrivateProperty", declaredFlags), typeInfo.GetDeclaredProperty("PrivateProperty"));
+
+ Assert.All(TypeExtensions.GetFields(type, declaredFlags), field => typeInfo.DeclaredFields.Contains(field));
+ Assert.All(TypeExtensions.GetMethods(type, declaredFlags), method => typeInfo.DeclaredMethods.Contains(method));
+ Assert.All(TypeExtensions.GetNestedTypes(type, declaredFlags), nestedType => typeInfo.DeclaredNestedTypes.Contains(nestedType.GetTypeInfo()));
+ Assert.All(TypeExtensions.GetProperties(type, declaredFlags), property => typeInfo.DeclaredProperties.Contains(property));
+ Assert.All(TypeExtensions.GetEvents(type, declaredFlags), eventInfo => typeInfo.DeclaredEvents.Contains(eventInfo));
+ Assert.All(TypeExtensions.GetConstructors(type, declaredFlags), constructor => typeInfo.DeclaredConstructors.Contains(constructor));
+
+ Assert.All(TypeExtensions.GetEvents(type), eventInfo => typeInfo.AsType().GetEvents().Contains(eventInfo));
+ Assert.All(TypeExtensions.GetFields(type), fieldInfo => typeInfo.AsType().GetFields().Contains(fieldInfo));
+ Assert.All(TypeExtensions.GetMethods(type), methodInfo => typeInfo.AsType().GetMethods().Contains(methodInfo));
+ Assert.All(TypeExtensions.GetProperties(type), propertyInfo => typeInfo.AsType().GetProperties().Contains(propertyInfo));
Assert.Equal(type.GetType(), typeInfo.GetType());
Assert.True(type.Equals(typeInfo));