|
2 | 2 | using System.Diagnostics.CodeAnalysis;
|
3 | 3 | using System.Reflection;
|
4 | 4 | using System.Runtime.CompilerServices;
|
| 5 | +using System.Text; |
5 | 6 |
|
6 | 7 | namespace VReflector;
|
7 | 8 |
|
@@ -57,28 +58,25 @@ public static bool RecordClass(Type type)
|
57 | 58 | if (type == null || !type.IsClass)
|
58 | 59 | return false;
|
59 | 60 |
|
60 |
| - bool hasCompilerGeneratedToString = type |
61 |
| - .GetMethod("ToString") |
62 |
| - ?.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false) |
63 |
| - ?.Any() == true; |
64 |
| - |
65 |
| - bool hasEqualityContract = type |
66 |
| - ?.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance) |
67 |
| - ?.Any(p => p.Name == "EqualityContract") == true; |
68 |
| - |
69 |
| - return hasCompilerGeneratedToString && hasEqualityContract; |
| 61 | + return type.GetMethod("<Clone>$", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) is { } && |
| 62 | + type.GetProperty("EqualityContract", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)? |
| 63 | + .GetMethod?.IsDecoratedWith<CompilerGeneratedAttribute>() == true; |
70 | 64 | }
|
71 | 65 | public static bool RecordStruct(Type type)
|
72 | 66 | {
|
73 | 67 | if (type == null || !type.IsValueType || type.IsPrimitive)
|
74 | 68 | return false;
|
75 | 69 |
|
76 |
| - bool hasCompilerGeneratedToString = type |
77 |
| - .GetMethod("ToString") |
78 |
| - ?.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false) |
79 |
| - .Any() == true; |
80 |
| - |
81 |
| - return hasCompilerGeneratedToString; |
| 70 | + return type.BaseType == typeof(ValueType) && |
| 71 | + type.GetMethod("PrintMembers", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, |
| 72 | + new Type[] { typeof(StringBuilder) }, null) is { } && |
| 73 | + type.GetMethod("op_Equality", BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly, null, |
| 74 | + new Type[] { type, type }, null)? |
| 75 | + .IsDecoratedWith<CompilerGeneratedAttribute>() == true; |
| 76 | + } |
| 77 | + public static bool KeyValuePair(Type type) |
| 78 | + { |
| 79 | + return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>); |
82 | 80 | }
|
83 | 81 | public static bool Enum(Type type) => type?.GetTypeInfo()?.BaseType == typeof(Enum);
|
84 | 82 | public static bool Nullable(Type? type) => type is null || type.IsClass || System.Nullable.GetUnderlyingType(type) != null;
|
@@ -107,6 +105,32 @@ public static bool Anonymous(Type type)
|
107 | 105 | type.GetCustomAttributes(false).Any(a => a is CompilerGeneratedAttribute) &&
|
108 | 106 | type.Name.StartsWith("<>") && type.IsClass;
|
109 | 107 | }
|
| 108 | + private static bool IsTuple(Type type) |
| 109 | + { |
| 110 | + if (type == null || !type.IsGenericType) |
| 111 | + { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + Type openType = type.GetGenericTypeDefinition(); |
| 116 | + |
| 117 | + return openType == typeof(ValueTuple<>) |
| 118 | + || openType == typeof(ValueTuple<,>) |
| 119 | + || openType == typeof(ValueTuple<,,>) |
| 120 | + || openType == typeof(ValueTuple<,,,>) |
| 121 | + || openType == typeof(ValueTuple<,,,,>) |
| 122 | + || openType == typeof(ValueTuple<,,,,,>) |
| 123 | + || openType == typeof(ValueTuple<,,,,,,>) |
| 124 | + || (openType == typeof(ValueTuple<,,,,,,,>) && IsTuple(type.GetGenericArguments()[7])) |
| 125 | + || openType == typeof(Tuple<>) |
| 126 | + || openType == typeof(Tuple<,>) |
| 127 | + || openType == typeof(Tuple<,,>) |
| 128 | + || openType == typeof(Tuple<,,,>) |
| 129 | + || openType == typeof(Tuple<,,,,>) |
| 130 | + || openType == typeof(Tuple<,,,,,>) |
| 131 | + || openType == typeof(Tuple<,,,,,,>) |
| 132 | + || (openType == typeof(Tuple<,,,,,,,>) && IsTuple(type.GetGenericArguments()[7])); |
| 133 | + } |
110 | 134 | public static bool ReadonlyStruct(Type type)
|
111 | 135 | {
|
112 | 136 | return type != null && Struct(type) && type.GetCustomAttributes(typeof(IsReadOnlyAttribute), false).Any();
|
@@ -242,7 +266,7 @@ public static bool Memory(Type type, [NotNullWhen(true)] out Type? elementType)
|
242 | 266 | elementType = null;
|
243 | 267 | return false;
|
244 | 268 | }
|
245 |
| - public static bool ReadOnlyMemory( Type type) |
| 269 | + public static bool ReadOnlyMemory(Type type) |
246 | 270 | {
|
247 | 271 | if (GenericType(type) && type.GetGenericTypeDefinition().FullName == "System.ReadOnlyMemory`1")
|
248 | 272 | {
|
|
0 commit comments