Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
TypeExtensions.cs
Go to the documentation of this file.
1using System.Reflection;
2using System.Runtime.CompilerServices;
3
5
6internal static class TypeExtensions
7{
8 public static bool IsClosureType(this Type ty)
9 {
10 var compilerGen = ty.GetCustomAttribute<CompilerGeneratedAttribute>() != null;
11 // check for the closure class name pattern. see
12 // https://stackoverflow.com/questions/2508828/where-to-learn-about-vs-debugger-magic-names/2509524#2509524
13 var dcName = ty.Name.StartsWith("<>c__DisplayClass");
14
15 return compilerGen && dcName;
16 }
17
18 public static Type? GetGenInst(this Type ty, Type genTy)
19 {
20 if (!genTy.IsGenericTypeDefinition)
21 {
22 throw new ArgumentException($"{nameof(genTy)} is not a generic type definition.");
23 }
24
25 if (genTy.IsInterface)
26 {
27 foreach (var iface in ty.GetInterfaces())
28 {
29 if (iface.IsGenericType && iface.GetGenericTypeDefinition() == genTy)
30 {
31 return iface;
32 }
33 }
34 }
35 else
36 {
37 Type? curr = ty;
38
39 while (curr is not null)
40 {
41 if (curr.IsGenericType && curr.GetGenericTypeDefinition() == genTy)
42 {
43 return curr;
44 }
45
46 curr = curr.BaseType;
47 }
48 }
49
50 return null;
51 }
52}
System.ArgumentException ArgumentException