42 internal static bool IsSameAssembly(
Type type1,
Type type2)
44 return type1.Assembly.Equals(type2.Assembly);
47 internal static bool IsFromAssembly(
Type type,
Assembly assembly)
49 return type.Assembly.Equals(assembly);
57 internal static bool IsDynamicAssembly(
Assembly asm)
59#if IMPORTER || EXPORTER
66 internal static bool IsReflectionOnly(
Type type)
68 while (type.HasElementType)
69 type = type.GetElementType();
71 var
asm = type.Assembly;
72 if (
asm !=
null &&
asm.ReflectionOnly)
75 if (!type.IsGenericType || type.IsGenericTypeDefinition)
79 foreach (
Type arg
in type.GetGenericArguments())
80 if (IsReflectionOnly(arg))
86 internal static bool ContainsTypeBuilder(
Type type)
88 while (type.HasElementType)
89 type = type.GetElementType();
91 if (!type.IsGenericType || type.IsGenericTypeDefinition)
92 return type is TypeBuilder;
94 foreach (
Type arg
in type.GetGenericArguments())
95 if (ContainsTypeBuilder(arg))
98 return type.GetGenericTypeDefinition() is TypeBuilder;
101 internal static bool IsVector(
Type type)
103#if IMPORTER || EXPORTER
104 return type.IsSZArray;
107 return type.IsSZArray;
112 return type.IsArray && type.Name.EndsWith(
"[]");
117 internal static bool IsDynamicMethod(
MethodInfo method)
124 return method.MetadataToken == 0;
126 catch (InvalidOperationException)
132 internal static MethodBuilder DefineTypeInitializer(TypeBuilder typeBuilder,
RuntimeClassLoader loader)
134 var attr = MethodAttributes.Static | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName | MethodAttributes.Private;
135 return typeBuilder.DefineMethod(
ConstructorInfo.TypeConstructorName, attr,
null,
Type.EmptyTypes);
140 return name1.Name.Equals(name2.Name, StringComparison.OrdinalIgnoreCase) && CompareKeys(name1.GetPublicKeyToken(), name2.GetPublicKeyToken());
143 static bool CompareKeys(
byte[] b1,
byte[] b2)
145 int len1 = b1 ==
null ? 0 : b1.Length;
146 int len2 = b2 ==
null ? 0 : b2.Length;
150 for (
int i = 0; i < len1; i++)
157 internal static bool IsConstructor(
MethodBase method)
159 return method.IsSpecialName && method.Name ==
ConstructorInfo.ConstructorName;
162 internal static MethodBuilder DefineConstructor(TypeBuilder tb, MethodAttributes attribs,
Type[] parameterTypes)
164 return tb.DefineMethod(
ConstructorInfo.ConstructorName, attribs | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
null, parameterTypes);
172 internal static bool CanOwnDynamicMethod(
Type type)
174 return type !=
null && !type.IsInterface && !type.HasElementType && !type.IsGenericTypeDefinition && !type.IsGenericParameter;
179 if (p1.ParameterType != p2.ParameterType)
183 if (!MatchTypes(p1.GetOptionalCustomModifiers(), p2.GetOptionalCustomModifiers()))
187 if (!MatchTypes(p1.GetRequiredCustomModifiers(), p2.GetRequiredCustomModifiers()))
194 private static bool MatchTypes(
Type[] t1,
Type[] t2)
196 if (t1.Length == t2.Length)
198 for (
int i = 0; i < t1.Length; i++)
212 internal static Type GetMissingType(
Type type)
214 while (type.HasElementType)
215 type = type.GetElementType();
217 if (type.__IsMissing)
221 else if (type.__ContainsMissingType)
223 if (type.IsGenericType)
225 foreach (var arg
in type.GetGenericArguments())
227 var t1 = GetMissingType(arg);
233 throw new NotImplementedException(type.FullName);