MethodWrapper
The MethodWrapper class wraps a MethodInfo so that it may
be used in a platform-independent manner.
using NUnit.Framework.Interfaces;
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Internal
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class MethodWrapper : IMethodInfo, IReflectionInfo, IEquatable<MethodWrapper>
{
public ITypeInfo TypeInfo { get; }
public MethodInfo MethodInfo { get; }
public string Name => MethodInfo.Name;
public bool IsAbstract => MethodInfo.IsAbstract;
public bool IsPublic => MethodInfo.IsPublic;
public bool IsStatic => MethodInfo.IsStatic;
public bool ContainsGenericParameters => MethodInfo.ContainsGenericParameters;
public bool IsGenericMethod => MethodInfo.IsGenericMethod;
public bool IsGenericMethodDefinition => MethodInfo.IsGenericMethodDefinition;
public ITypeInfo ReturnType => new TypeWrapper(MethodInfo.ReturnType);
public MethodWrapper(Type type, MethodInfo method)
{
TypeInfo = new TypeWrapper(type);
MethodInfo = method;
}
public MethodWrapper(Type type, string methodName)
{
TypeInfo = new TypeWrapper(type);
MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
if ((object)method == null) {
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(18, 2);
defaultInterpolatedStringHandler.AppendLiteral("Method ");
defaultInterpolatedStringHandler.AppendFormatted(type);
defaultInterpolatedStringHandler.AppendLiteral(".");
defaultInterpolatedStringHandler.AppendFormatted(methodName);
defaultInterpolatedStringHandler.AppendLiteral(" not found");
throw new ArgumentException(defaultInterpolatedStringHandler.ToStringAndClear());
}
MethodInfo = method;
}
public IParameterInfo[] GetParameters()
{
ParameterInfo[] parameters = MethodInfo.GetParameters();
IParameterInfo[] array = new IParameterInfo[parameters.Length];
for (int i = 0; i < parameters.Length; i++) {
array[i] = new ParameterWrapper(this, parameters[i]);
}
return array;
}
public Type[] GetGenericArguments()
{
return MethodInfo.GetGenericArguments();
}
public IMethodInfo MakeGenericMethod(params Type[] typeArguments)
{
return new MethodWrapper(TypeInfo.Type, MethodInfo.MakeGenericMethod(typeArguments));
}
public T[] GetCustomAttributes<T>(bool inherit) where T : class
{
return MethodInfo.GetAttributes<T>(inherit);
}
public bool IsDefined<T>(bool inherit) where T : class
{
return MethodInfo.HasAttribute<T>(inherit);
}
[System.Runtime.CompilerServices.NullableContext(2)]
public object Invoke(object fixture, params object[] args)
{
return Reflect.InvokeMethod(MethodInfo, fixture, args);
}
public override string ToString()
{
return MethodInfo.Name;
}
[System.Runtime.CompilerServices.NullableContext(2)]
public bool Equals(MethodWrapper other)
{
if (other == null)
return false;
if (this == other)
return true;
return MethodInfo.Equals(other.MethodInfo);
}
[System.Runtime.CompilerServices.NullableContext(2)]
public override bool Equals(object obj)
{
if (obj == null)
return false;
if (this == obj)
return true;
if (obj.GetType() != GetType())
return false;
return Equals((MethodWrapper)obj);
}
public override int GetHashCode()
{
return MethodInfo.GetHashCode();
}
}
}