9 namespace Core.Extensions
12 using System.Collections.Generic;
14 using System.Reflection;
15 using System.Runtime.CompilerServices;
20 public static partial class TypeInfoExtension
29 public static IEnumerable<ConstructorInfo> GetConstructorsByParameterType(
this TypeInfo type, Type parameterType)
31 return from constructorInfo in type.DeclaredConstructors
32 where constructorInfo.GetParameters().Any(p => p.IsOptional ==
false && p.ParameterType == parameterType)
33 select constructorInfo;
43 public static ConstructorInfo GetConstructorByParameterTypes(
this TypeInfo type, Type[] parameterTypes)
45 return (from constructorInfo in type.DeclaredConstructors
46 let parameterInfos = constructorInfo.GetParameters()
48 parameterInfos.Length == parameterTypes.Length
49 && parameterInfos.Select(p => p.ParameterType.Name).SequenceEqual(parameterTypes.Select(t => t.Name))
50 select constructorInfo)?.
61 [MethodImpl(MethodImplOptions.AggressiveInlining)]
62 public static bool IsDependencyInjectable(
this TypeInfo type, Type interfaceType)
64 return type.GetConstructorsByParameterType(interfaceType).Any();
75 public static bool IsInvokableWithParameters(
this TypeInfo method, Type returnParameter, params Type[] argumentParameters)
77 var methodInfo = method.GetDeclaredMethod(
"Invoke");
78 if(methodInfo == null)
82 var parameters = methodInfo.GetParameters();
83 bool trueForAll =
true;
84 for(
int i = 0; i < argumentParameters.Length; i++)
86 if(argumentParameters[i] == null)
90 if(i <= parameters.Length)
92 trueForAll &= (parameters[i] != null && parameters[i].ParameterType == argumentParameters[i]);
95 return methodInfo.ReturnParameter.ParameterType == returnParameter && trueForAll;
103 [MethodImpl(MethodImplOptions.AggressiveInlining)]
104 public static object GetDefault(
this TypeInfo type)
106 if(type.GetConstructorsByParameterType(null) != null && type.IsAbstract ==
false && type.IsValueType ==
true)
108 return Activator.CreateInstance(type.AsType());
123 [MethodImpl(MethodImplOptions.AggressiveInlining)]
124 public static Type GetReturnParameterType(
this TypeInfo type, Type defaultType = null)
126 if(typeof(Delegate).GetTypeInfo().IsAssignableFrom(type) ==
true)
128 return type.GetDeclaredMethod(
"Invoke")?.ReturnParameter?.ParameterType ?? defaultType;
148 [MethodImpl(MethodImplOptions.AggressiveInlining)]
149 public static TypeMatch IsSameOrSimilar<TOut>(
this TypeInfo
self,
bool? checkIsSame = null)
151 var typeOut = typeof(TOut).GetTypeInfo();
152 if((checkIsSame == null || checkIsSame ==
true) &&
self == typeOut)
157 if((checkIsSame == null || checkIsSame ==
false) && (
self.Namespace == typeOut.Namespace) && (
self.BaseType == typeOut.BaseType))
173 [MethodImpl(MethodImplOptions.AggressiveInlining)]
174 public static bool IsSuperOrSubType(
this TypeInfo
self, TypeInfo typeBase)
180 return self.AsType() != typeof(
object) && (
self.IsAssignableFrom(typeBase) || typeBase.IsAssignableFrom(
self));
TypeMatch
An enumerating of values containing a rough 2bit classification of relationship between two types ...