.NET Portable TypeCast  3.1.0.4
A, easy-to-use tested, generic, portable, runtime-extensible, arbitrary type converter library
TypeInfoExtension.cs
Go to the documentation of this file.
1 // <copyright file=mitlicense.md url=http://lsauer.mit-license.org/ >
2 // Lo Sauer, 2016
3 // </copyright>
4 // <summary> A custom excerpt of the Core.Extensions library. Not for reuse! </summary
5 // <language> C# > 3.0 </language>
6 // <version> 2.0.0.4 </version>
7 // <author> Lo Sauer; people credited in the sources </author>
8 // <project> https://github.com/lsauer/dotnet-core.extensions </project>
9 namespace Core.Extensions
10 {
11  using System;
12  using System.Collections.Generic;
13  using System.Linq;
14  using System.Reflection;
15  using System.Runtime.CompilerServices;
16 
20  public static partial class TypeInfoExtension
21  {
29  public static IEnumerable<ConstructorInfo> GetConstructorsByParameterType(this TypeInfo type, Type parameterType)
30  {
31  return from constructorInfo in type.DeclaredConstructors
32  where constructorInfo.GetParameters().Any(p => p.IsOptional == false && p.ParameterType == parameterType)
33  select constructorInfo;
34  }
35 
43  public static ConstructorInfo GetConstructorByParameterTypes(this TypeInfo type, Type[] parameterTypes)
44  {
45  return (from constructorInfo in type.DeclaredConstructors
46  let parameterInfos = constructorInfo.GetParameters()
47  where
48  parameterInfos.Length == parameterTypes.Length
49  && parameterInfos.Select(p => p.ParameterType.Name).SequenceEqual(parameterTypes.Select(t => t.Name))
50  select constructorInfo)?.
51  FirstOrDefault();
52  }
53 
61  [MethodImpl(MethodImplOptions.AggressiveInlining)]
62  public static bool IsDependencyInjectable(this TypeInfo type, Type interfaceType)
63  {
64  return type.GetConstructorsByParameterType(interfaceType).Any();
65  }
66 
67 
75  public static bool IsInvokableWithParameters(this TypeInfo method, Type returnParameter, params Type[] argumentParameters)
76  {
77  var methodInfo = method.GetDeclaredMethod("Invoke");
78  if(methodInfo == null)
79  {
80  return false;
81  }
82  var parameters = methodInfo.GetParameters();
83  bool trueForAll = true;
84  for(int i = 0; i < argumentParameters.Length; i++)
85  {
86  if(argumentParameters[i] == null)
87  {
88  continue;
89  }
90  if(i <= parameters.Length)
91  {
92  trueForAll &= (parameters[i] != null && parameters[i].ParameterType == argumentParameters[i]);
93  }
94  }
95  return methodInfo.ReturnParameter.ParameterType == returnParameter && trueForAll;
96  }
97 
103  [MethodImpl(MethodImplOptions.AggressiveInlining)]
104  public static object GetDefault(this TypeInfo type)
105  {
106  if(type.GetConstructorsByParameterType(null) != null && type.IsAbstract == false && type.IsValueType == true)
107  {
108  return Activator.CreateInstance(type.AsType());
109  }
110  else
111  {
112  return null;
113  }
114  }
115 
123  [MethodImpl(MethodImplOptions.AggressiveInlining)]
124  public static Type GetReturnParameterType(this TypeInfo type, Type defaultType = null)
125  {
126  if(typeof(Delegate).GetTypeInfo().IsAssignableFrom(type) == true)
127  {
128  return type.GetDeclaredMethod("Invoke")?.ReturnParameter?.ParameterType ?? defaultType;
129  }
130  else
131  {
132  return null;
133  }
134  }
135 
148  [MethodImpl(MethodImplOptions.AggressiveInlining)]
149  public static TypeMatch IsSameOrSimilar<TOut>(this TypeInfo self, bool? checkIsSame = null)
150  {
151  var typeOut = typeof(TOut).GetTypeInfo();
152  if((checkIsSame == null || checkIsSame == true) && self == typeOut)
153  {
154  return TypeMatch.Same;
155  }
156 
157  if((checkIsSame == null || checkIsSame == false) && (self.Namespace == typeOut.Namespace) && (self.BaseType == typeOut.BaseType))
158  {
159  return TypeMatch.Similar;
160  }
161  return TypeMatch.None;
162  }
163 
164 
173  [MethodImpl(MethodImplOptions.AggressiveInlining)]
174  public static bool IsSuperOrSubType(this TypeInfo self, TypeInfo typeBase)
175  {
176  if(self == typeBase)
177  {
178  return true;
179  }
180  return self.AsType() != typeof(object) && (self.IsAssignableFrom(typeBase) || typeBase.IsAssignableFrom(self));
181  }
182  }
183 }
TypeMatch
An enumerating of values containing a rough 2bit classification of relationship between two types ...
Definition: TypeMatch.cs:17