2 Copyright (C) 2009-2012 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
25 using System.Runtime.InteropServices;
27 using System.Collections.Generic;
28 using IKVM.Reflection.Emit;
30 namespace IKVM.Reflection
32 interface IGenericContext
34 Type GetGenericTypeArgument(int index);
35 Type GetGenericMethodArgument(int index);
38 interface IGenericBinder
40 Type BindTypeParameter(Type type);
41 Type BindMethodParameter(Type type);
44 public abstract class Type : MemberInfo, IGenericContext, IGenericBinder
46 public static readonly Type[] EmptyTypes = Empty<Type>.Array;
47 protected readonly Type underlyingType;
48 protected TypeFlags typeFlags;
51 protected enum TypeFlags
53 // for use by TypeBuilder
54 IsGenericTypeDefinition = 1,
58 // for use by MissingType
62 // for use by TypeDef, TypeBuilder or MissingType
63 PotentialEnumOrValueType = 32,
67 // prevent subclassing by outsiders
70 this.underlyingType = this;
73 internal Type(Type underlyingType)
75 System.Diagnostics.Debug.Assert(underlyingType.underlyingType == underlyingType);
76 this.underlyingType = underlyingType;
77 this.typeFlags = underlyingType.typeFlags;
80 public static Binder DefaultBinder
85 public sealed override MemberTypes MemberType
87 get { return IsNested ? MemberTypes.NestedType : MemberTypes.TypeInfo; }
90 public virtual string AssemblyQualifiedName
92 // NOTE the assembly name is not escaped here, only when used in a generic type instantiation
93 get { return this.FullName + ", " + this.Assembly.FullName; }
96 public abstract Type BaseType
101 public abstract TypeAttributes Attributes
106 public virtual Type GetElementType()
111 internal virtual void CheckBaked()
115 public virtual Type[] __GetDeclaredTypes()
117 return Type.EmptyTypes;
120 public virtual Type[] __GetDeclaredInterfaces()
122 return Type.EmptyTypes;
125 public virtual MethodBase[] __GetDeclaredMethods()
127 return Empty<MethodBase>.Array;
130 public virtual __MethodImplMap __GetMethodImplMap()
132 throw new NotSupportedException();
135 public virtual FieldInfo[] __GetDeclaredFields()
137 return Empty<FieldInfo>.Array;
140 public virtual EventInfo[] __GetDeclaredEvents()
142 return Empty<EventInfo>.Array;
145 public virtual PropertyInfo[] __GetDeclaredProperties()
147 return Empty<PropertyInfo>.Array;
150 public virtual CustomModifiers __GetCustomModifiers()
152 return new CustomModifiers();
155 [Obsolete("Please use __GetCustomModifiers() instead.")]
156 public Type[] __GetRequiredCustomModifiers()
158 return __GetCustomModifiers().GetRequired();
161 [Obsolete("Please use __GetCustomModifiers() instead.")]
162 public Type[] __GetOptionalCustomModifiers()
164 return __GetCustomModifiers().GetOptional();
167 public virtual __StandAloneMethodSig __MethodSignature
169 get { throw new InvalidOperationException(); }
172 public virtual bool HasElementType
174 get { return false; }
177 public virtual bool IsArray
179 get { return false; }
182 public virtual bool __IsVector
184 get { return false; }
187 public virtual bool IsByRef
189 get { return false; }
192 public virtual bool IsPointer
194 get { return false; }
197 public virtual bool __IsFunctionPointer
199 get { return false; }
202 public virtual bool IsValueType
206 Type baseType = this.BaseType;
207 return baseType != null
208 && baseType.IsEnumOrValueType
209 && !this.IsEnumOrValueType;
213 public virtual bool IsGenericParameter
215 get { return false; }
218 public virtual int GenericParameterPosition
220 get { throw new NotSupportedException(); }
223 public virtual MethodBase DeclaringMethod
228 public Type UnderlyingSystemType
230 get { return underlyingType; }
233 public override Type DeclaringType
238 public virtual string __Name
240 get { throw new InvalidOperationException(); }
243 public virtual string __Namespace
245 get { throw new InvalidOperationException(); }
248 public abstract override string Name
253 public virtual string Namespace
259 return DeclaringType.Namespace;
265 internal virtual int GetModuleBuilderToken()
267 throw new InvalidOperationException();
270 public static bool operator ==(Type t1, Type t2)
272 // Casting to object results in smaller code than calling ReferenceEquals and makes
273 // this method more likely to be inlined.
274 // On CLR v2 x86, microbenchmarks show this to be faster than calling ReferenceEquals.
275 return (object)t1 == (object)t2
276 || ((object)t1 != null && (object)t2 != null && (object)t1.underlyingType == (object)t2.underlyingType);
279 public static bool operator !=(Type t1, Type t2)
284 public bool Equals(Type type)
289 public override bool Equals(object obj)
291 return Equals(obj as Type);
294 public override int GetHashCode()
296 Type type = this.UnderlyingSystemType;
297 return ReferenceEquals(type, this) ? base.GetHashCode() : type.GetHashCode();
300 public virtual Type[] GetGenericArguments()
302 return Type.EmptyTypes;
305 public virtual CustomModifiers[] __GetGenericArgumentsCustomModifiers()
307 return Empty<CustomModifiers>.Array;
310 [Obsolete("Please use __GetGenericArgumentsCustomModifiers() instead")]
311 public Type[][] __GetGenericArgumentsRequiredCustomModifiers()
313 CustomModifiers[] customModifiers = __GetGenericArgumentsCustomModifiers();
314 Type[][] array = new Type[customModifiers.Length][];
315 for (int i = 0; i < array.Length; i++)
317 array[i] = customModifiers[i].GetRequired();
322 [Obsolete("Please use __GetGenericArgumentsCustomModifiers() instead")]
323 public Type[][] __GetGenericArgumentsOptionalCustomModifiers()
325 CustomModifiers[] customModifiers = __GetGenericArgumentsCustomModifiers();
326 Type[][] array = new Type[customModifiers.Length][];
327 for (int i = 0; i < array.Length; i++)
329 array[i] = customModifiers[i].GetOptional();
334 public virtual Type GetGenericTypeDefinition()
336 throw new InvalidOperationException();
339 public virtual StructLayoutAttribute StructLayoutAttribute
344 public virtual bool __GetLayout(out int packingSize, out int typeSize)
351 public virtual bool IsGenericType
353 get { return false; }
356 public virtual bool IsGenericTypeDefinition
358 get { return false; }
361 internal virtual bool IsGenericTypeInstance
363 get { return false; }
366 public virtual bool ContainsGenericParameters
370 if (this.IsGenericParameter)
374 foreach (Type arg in this.GetGenericArguments())
376 if (arg.ContainsGenericParameters)
385 public virtual Type[] GetGenericParameterConstraints()
387 throw new InvalidOperationException();
390 public virtual GenericParameterAttributes GenericParameterAttributes
392 get { throw new InvalidOperationException(); }
395 public virtual int GetArrayRank()
397 throw new NotSupportedException();
400 public virtual int[] __GetArraySizes()
402 throw new NotSupportedException();
405 public virtual int[] __GetArrayLowerBounds()
407 throw new NotSupportedException();
411 public virtual Type GetEnumUnderlyingType()
415 throw new ArgumentException();
418 return GetEnumUnderlyingTypeImpl();
421 internal Type GetEnumUnderlyingTypeImpl()
423 foreach (FieldInfo field in __GetDeclaredFields())
427 // the CLR assumes that an enum has only one instance field, so we can do the same
428 return field.FieldType;
431 throw new InvalidOperationException();
434 public override string ToString()
439 public abstract string FullName
444 protected string GetFullName()
446 string ns = TypeNameParser.Escape(this.__Namespace);
447 Type decl = this.DeclaringType;
456 return ns + "." + this.Name;
463 return decl.FullName + "+" + this.Name;
467 return decl.FullName + "+" + ns + "." + this.Name;
472 internal virtual bool IsModulePseudoType
474 get { return false; }
477 internal virtual Type GetGenericTypeArgument(int index)
479 throw new InvalidOperationException();
482 public MemberInfo[] GetDefaultMembers()
484 Type defaultMemberAttribute = this.Module.universe.Import(typeof(System.Reflection.DefaultMemberAttribute));
485 foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(this))
487 if (cad.Constructor.DeclaringType.Equals(defaultMemberAttribute))
489 return GetMember((string)cad.ConstructorArguments[0].Value);
492 return Empty<MemberInfo>.Array;
495 public MemberInfo[] GetMember(string name)
497 return GetMember(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
500 public MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
502 return GetMember(name, MemberTypes.All, bindingAttr);
505 public MemberInfo[] GetMembers()
507 return GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
510 public MemberInfo[] GetMembers(BindingFlags bindingAttr)
512 List<MemberInfo> members = new List<MemberInfo>();
513 members.AddRange(GetConstructors(bindingAttr));
514 members.AddRange(GetMethods(bindingAttr));
515 members.AddRange(GetFields(bindingAttr));
516 members.AddRange(GetProperties(bindingAttr));
517 members.AddRange(GetEvents(bindingAttr));
518 members.AddRange(GetNestedTypes(bindingAttr));
519 return members.ToArray();
522 public MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr)
524 MemberFilter filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.Equals(filterCriteria); };
525 return FindMembers(type, bindingAttr, filter, name);
528 private static void AddMembers(List<MemberInfo> list, MemberFilter filter, object filterCriteria, MemberInfo[] members)
530 foreach (MemberInfo member in members)
532 if (filter == null || filter(member, filterCriteria))
539 public MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter filter, object filterCriteria)
541 List<MemberInfo> members = new List<MemberInfo>();
542 if ((memberType & MemberTypes.Constructor) != 0)
544 AddMembers(members, filter, filterCriteria, GetConstructors(bindingAttr));
546 if ((memberType & MemberTypes.Method) != 0)
548 AddMembers(members, filter, filterCriteria, GetMethods(bindingAttr));
550 if ((memberType & MemberTypes.Field) != 0)
552 AddMembers(members, filter, filterCriteria, GetFields(bindingAttr));
554 if ((memberType & MemberTypes.Property) != 0)
556 AddMembers(members, filter, filterCriteria, GetProperties(bindingAttr));
558 if ((memberType & MemberTypes.Event) != 0)
560 AddMembers(members, filter, filterCriteria, GetEvents(bindingAttr));
562 if ((memberType & MemberTypes.NestedType) != 0)
564 AddMembers(members, filter, filterCriteria, GetNestedTypes(bindingAttr));
566 return members.ToArray();
569 public EventInfo GetEvent(string name)
571 return GetEvent(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
574 public EventInfo GetEvent(string name, BindingFlags bindingAttr)
576 foreach (EventInfo evt in GetEvents(bindingAttr))
578 if (evt.Name == name)
586 public EventInfo[] GetEvents()
588 return GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
591 public EventInfo[] GetEvents(BindingFlags bindingAttr)
593 List<EventInfo> list = new List<EventInfo>();
598 foreach (EventInfo evt in type.__GetDeclaredEvents())
600 if (BindingFlagsMatch(evt.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
601 && BindingFlagsMatch(evt.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
606 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
608 if ((bindingAttr & BindingFlags.FlattenHierarchy) == 0)
610 bindingAttr &= ~BindingFlags.Static;
612 type = type.BaseType;
619 return list.ToArray();
622 public FieldInfo GetField(string name)
624 return GetField(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
627 public FieldInfo GetField(string name, BindingFlags bindingAttr)
629 foreach (FieldInfo field in GetFields(bindingAttr))
631 if (field.Name == name)
639 public FieldInfo[] GetFields()
641 return GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
644 public FieldInfo[] GetFields(BindingFlags bindingAttr)
646 List<FieldInfo> list = new List<FieldInfo>();
648 foreach (FieldInfo field in __GetDeclaredFields())
650 if (BindingFlagsMatch(field.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
651 && BindingFlagsMatch(field.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
656 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
658 for (Type type = this.BaseType; type != null; type = type.BaseType)
661 foreach (FieldInfo field in type.__GetDeclaredFields())
663 if ((field.Attributes & FieldAttributes.FieldAccessMask) > FieldAttributes.Private
664 && BindingFlagsMatch(field.IsStatic, bindingAttr, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance))
671 return list.ToArray();
674 public Type[] GetInterfaces()
676 List<Type> list = new List<Type>();
677 for (Type type = this; type != null; type = type.BaseType)
679 AddInterfaces(list, type);
681 return list.ToArray();
684 private static void AddInterfaces(List<Type> list, Type type)
687 foreach (Type iface in type.__GetDeclaredInterfaces())
689 if (!list.Contains(iface))
692 AddInterfaces(list, iface);
697 public MethodInfo[] GetMethods(BindingFlags bindingAttr)
700 List<MethodInfo> list = new List<MethodInfo>();
701 foreach (MethodBase mb in __GetDeclaredMethods())
703 MethodInfo mi = mb as MethodInfo;
705 && BindingFlagsMatch(mi.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
706 && BindingFlagsMatch(mi.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
711 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
713 for (Type type = this.BaseType; type != null; type = type.BaseType)
716 foreach (MethodBase mb in type.__GetDeclaredMethods())
718 MethodInfo mi = mb as MethodInfo;
720 && (mi.Attributes & MethodAttributes.MemberAccessMask) > MethodAttributes.Private
721 && BindingFlagsMatch(mi.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
722 && BindingFlagsMatch(mi.IsStatic, bindingAttr, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance)
723 && !FindMethod(list, mi))
730 return list.ToArray();
733 private static bool FindMethod(List<MethodInfo> methods, MethodInfo method)
735 foreach (MethodInfo m in methods)
737 if (m.Name == method.Name && m.MethodSignature.Equals(method.MethodSignature))
745 public MethodInfo[] GetMethods()
747 return GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
750 public MethodInfo GetMethod(string name)
752 return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
755 public MethodInfo GetMethod(string name, BindingFlags bindingAttr)
757 MethodInfo found = null;
758 foreach (MethodInfo method in GetMethods(bindingAttr))
760 if (method.Name == name)
764 throw new AmbiguousMatchException();
772 public MethodInfo GetMethod(string name, Type[] types)
774 return GetMethod(name, types, null);
777 public MethodInfo GetMethod(string name, Type[] types, ParameterModifier[] modifiers)
779 return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, types, modifiers);
782 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
784 MethodInfo found = null;
785 foreach (MethodInfo method in GetMethods(bindingAttr))
787 if (method.Name == name && method.MethodSignature.MatchParameterTypes(types))
791 throw new AmbiguousMatchException();
799 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
801 // FXBUG callConvention seems to be ignored
802 return GetMethod(name, bindingAttr, binder, types, modifiers);
805 public ConstructorInfo[] GetConstructors()
807 return GetConstructors(BindingFlags.Public | BindingFlags.Instance);
810 public ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
813 List<ConstructorInfo> list = new List<ConstructorInfo>();
814 foreach (MethodBase mb in __GetDeclaredMethods())
816 ConstructorInfo constructor = mb as ConstructorInfo;
817 if (constructor != null
818 && BindingFlagsMatch(constructor.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
819 && BindingFlagsMatch(constructor.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
821 list.Add(constructor);
824 return list.ToArray();
827 public ConstructorInfo GetConstructor(Type[] types)
829 return GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Standard, types, null);
832 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
834 foreach (ConstructorInfo constructor in GetConstructors(bindingAttr))
836 if (constructor.MethodSignature.MatchParameterTypes(types))
844 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callingConvention, Type[] types, ParameterModifier[] modifiers)
846 // FXBUG callConvention seems to be ignored
847 return GetConstructor(bindingAttr, binder, types, modifiers);
850 internal Type ResolveNestedType(TypeName typeName)
852 return FindNestedType(typeName) ?? Module.universe.GetMissingTypeOrThrow(Module, this, typeName);
855 // unlike the public API, this takes the namespace and name into account
856 internal virtual Type FindNestedType(TypeName name)
858 foreach (Type type in __GetDeclaredTypes())
860 if (type.__Namespace == name.Namespace && type.__Name == name.Name)
868 public Type GetNestedType(string name)
870 return GetNestedType(name, BindingFlags.Public);
873 public Type GetNestedType(string name, BindingFlags bindingAttr)
875 foreach (Type type in GetNestedTypes(bindingAttr))
877 // FXBUG the namespace is ignored
878 if (type.__Name == name)
886 public Type[] GetNestedTypes()
888 return GetNestedTypes(BindingFlags.Public);
891 public Type[] GetNestedTypes(BindingFlags bindingAttr)
894 List<Type> list = new List<Type>();
895 foreach (Type type in __GetDeclaredTypes())
897 if (BindingFlagsMatch(type.IsNestedPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic))
902 return list.ToArray();
905 public PropertyInfo[] GetProperties()
907 return GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
910 public PropertyInfo[] GetProperties(BindingFlags bindingAttr)
912 List<PropertyInfo> list = new List<PropertyInfo>();
917 foreach (PropertyInfo property in type.__GetDeclaredProperties())
919 if (BindingFlagsMatch(property.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
920 && BindingFlagsMatch(property.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
925 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
927 if ((bindingAttr & BindingFlags.FlattenHierarchy) == 0)
929 bindingAttr &= ~BindingFlags.Static;
931 type = type.BaseType;
938 return list.ToArray();
941 public PropertyInfo GetProperty(string name)
943 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
946 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
948 foreach (PropertyInfo prop in GetProperties(bindingAttr))
950 if (prop.Name == name)
958 public PropertyInfo GetProperty(string name, Type returnType)
960 PropertyInfo found = null;
961 foreach (PropertyInfo prop in GetProperties())
963 if (prop.Name == name && prop.PropertyType.Equals(returnType))
967 throw new AmbiguousMatchException();
975 public PropertyInfo GetProperty(string name, Type[] types)
977 PropertyInfo found = null;
978 foreach (PropertyInfo prop in GetProperties())
980 if (prop.Name == name && MatchParameterTypes(prop.GetIndexParameters(), types))
984 throw new AmbiguousMatchException();
992 private static bool MatchParameterTypes(ParameterInfo[] parameters, Type[] types)
994 if (parameters.Length == types.Length)
996 for (int i = 0; i < parameters.Length; i++)
998 if (!parameters[i].ParameterType.Equals(types[i]))
1008 public PropertyInfo GetProperty(string name, Type returnType, Type[] types)
1010 return GetProperty(name, returnType, types, null);
1013 public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1015 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, returnType, types, modifiers);
1018 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1020 PropertyInfo found = null;
1021 foreach (PropertyInfo prop in GetProperties(bindingAttr))
1023 if (prop.Name == name && prop.PropertyType.Equals(returnType) && MatchParameterTypes(prop.GetIndexParameters(), types))
1027 throw new AmbiguousMatchException();
1035 public Type GetInterface(string name)
1037 return GetInterface(name, false);
1040 public Type GetInterface(string name, bool ignoreCase)
1044 throw new NotImplementedException();
1046 foreach (Type type in GetInterfaces())
1048 if (type.FullName == name)
1056 public Type[] FindInterfaces(TypeFilter filter, object filterCriteria)
1058 List<Type> list = new List<Type>();
1059 foreach (Type type in GetInterfaces())
1061 if (filter(type, filterCriteria))
1066 return list.ToArray();
1069 public ConstructorInfo TypeInitializer
1071 get { return GetConstructor(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); }
1074 public bool IsPrimitive
1078 Universe u = this.Universe;
1079 return this == u.System_Boolean
1080 || this == u.System_Byte
1081 || this == u.System_SByte
1082 || this == u.System_Int16
1083 || this == u.System_UInt16
1084 || this == u.System_Int32
1085 || this == u.System_UInt32
1086 || this == u.System_Int64
1087 || this == u.System_UInt64
1088 || this == u.System_IntPtr
1089 || this == u.System_UIntPtr
1090 || this == u.System_Char
1091 || this == u.System_Double
1092 || this == u.System_Single
1101 Type baseType = this.BaseType;
1102 return baseType != null
1103 && baseType.IsEnumOrValueType
1104 && baseType.__Name[0] == 'E';
1108 public bool IsSealed
1110 get { return (Attributes & TypeAttributes.Sealed) != 0; }
1113 public bool IsAbstract
1115 get { return (Attributes & TypeAttributes.Abstract) != 0; }
1118 private bool CheckVisibility(TypeAttributes access)
1120 return (Attributes & TypeAttributes.VisibilityMask) == access;
1123 public bool IsPublic
1125 get { return CheckVisibility(TypeAttributes.Public); }
1128 public bool IsNestedPublic
1130 get { return CheckVisibility(TypeAttributes.NestedPublic); }
1133 public bool IsNestedPrivate
1135 get { return CheckVisibility(TypeAttributes.NestedPrivate); }
1138 public bool IsNestedFamily
1140 get { return CheckVisibility(TypeAttributes.NestedFamily); }
1143 public bool IsNestedAssembly
1145 get { return CheckVisibility(TypeAttributes.NestedAssembly); }
1148 public bool IsNestedFamANDAssem
1150 get { return CheckVisibility(TypeAttributes.NestedFamANDAssem); }
1153 public bool IsNestedFamORAssem
1155 get { return CheckVisibility(TypeAttributes.NestedFamORAssem); }
1158 public bool IsNotPublic
1160 get { return CheckVisibility(TypeAttributes.NotPublic); }
1163 public bool IsImport
1165 get { return (Attributes & TypeAttributes.Import) != 0; }
1168 public bool IsCOMObject
1170 get { return IsClass && IsImport; }
1173 public bool IsContextful
1175 get { return IsSubclassOf(this.Module.universe.Import(typeof(ContextBoundObject))); }
1178 public bool IsMarshalByRef
1180 get { return IsSubclassOf(this.Module.universe.Import(typeof(MarshalByRefObject))); }
1183 public virtual bool IsVisible
1185 get { return IsPublic || (IsNestedPublic && this.DeclaringType.IsVisible); }
1188 public bool IsAnsiClass
1190 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
1193 public bool IsUnicodeClass
1195 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
1198 public bool IsAutoClass
1200 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
1203 public bool IsAutoLayout
1205 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
1208 public bool IsLayoutSequential
1210 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
1213 public bool IsExplicitLayout
1215 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
1218 public bool IsSpecialName
1220 get { return (Attributes & TypeAttributes.SpecialName) != 0; }
1223 public bool IsSerializable
1225 get { return (Attributes & TypeAttributes.Serializable) != 0; }
1230 get { return !IsInterface && !IsValueType; }
1233 public bool IsInterface
1235 get { return (Attributes & TypeAttributes.Interface) != 0; }
1238 public bool IsNested
1240 // FXBUG we check the declaring type (like .NET) and this results
1241 // in IsNested returning true for a generic type parameter
1242 get { return this.DeclaringType != null; }
1245 public virtual bool __ContainsMissingType
1249 if (this.__IsMissing)
1253 foreach (Type arg in this.GetGenericArguments())
1255 if (arg.__ContainsMissingType)
1264 public Type MakeArrayType()
1266 return ArrayType.Make(this, new CustomModifiers());
1269 public Type __MakeArrayType(CustomModifiers customModifiers)
1271 return ArrayType.Make(this, customModifiers);
1274 [Obsolete("Please use __MakeArrayType(CustomModifiers) instead.")]
1275 public Type __MakeArrayType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1277 return __MakeArrayType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1280 public Type MakeArrayType(int rank)
1282 return __MakeArrayType(rank, new CustomModifiers());
1285 public Type __MakeArrayType(int rank, CustomModifiers customModifiers)
1287 return MultiArrayType.Make(this, rank, Empty<int>.Array, new int[rank], customModifiers);
1290 [Obsolete("Please use __MakeArrayType(int, CustomModifiers) instead.")]
1291 public Type __MakeArrayType(int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1293 return __MakeArrayType(rank, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1296 public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, CustomModifiers customModifiers)
1298 return MultiArrayType.Make(this, rank, sizes ?? Empty<int>.Array, lobounds ?? Empty<int>.Array, customModifiers);
1301 [Obsolete("Please use __MakeArrayType(int, int[], int[], CustomModifiers) instead.")]
1302 public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1304 return __MakeArrayType(rank, sizes, lobounds, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1307 public Type MakeByRefType()
1309 return ByRefType.Make(this, new CustomModifiers());
1312 public Type __MakeByRefType(CustomModifiers customModifiers)
1314 return ByRefType.Make(this, customModifiers);
1317 [Obsolete("Please use __MakeByRefType(CustomModifiers) instead.")]
1318 public Type __MakeByRefType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1320 return __MakeByRefType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1323 public Type MakePointerType()
1325 return PointerType.Make(this, new CustomModifiers());
1328 public Type __MakePointerType(CustomModifiers customModifiers)
1330 return PointerType.Make(this, customModifiers);
1333 [Obsolete("Please use __MakeByRefType(CustomModifiers) instead.")]
1334 public Type __MakePointerType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1336 return __MakePointerType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1339 public Type MakeGenericType(params Type[] typeArguments)
1341 return __MakeGenericType(typeArguments, null);
1344 public Type __MakeGenericType(Type[] typeArguments, CustomModifiers[] customModifiers)
1346 if (!this.__IsMissing && !this.IsGenericTypeDefinition)
1348 throw new InvalidOperationException();
1350 return GenericTypeInstance.Make(this, Util.Copy(typeArguments), customModifiers == null ? null : (CustomModifiers[])customModifiers.Clone());
1353 [Obsolete("Please use __MakeGenericType(Type[], CustomModifiers[]) instead.")]
1354 public Type __MakeGenericType(Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
1356 if (!this.__IsMissing && !this.IsGenericTypeDefinition)
1358 throw new InvalidOperationException();
1360 CustomModifiers[] mods = null;
1361 if (requiredCustomModifiers != null || optionalCustomModifiers != null)
1363 mods = new CustomModifiers[typeArguments.Length];
1364 for (int i = 0; i < mods.Length; i++)
1366 mods[i] = CustomModifiers.FromReqOpt(Util.NullSafeElementAt(requiredCustomModifiers, i), Util.NullSafeElementAt(optionalCustomModifiers, i));
1369 return GenericTypeInstance.Make(this, Util.Copy(typeArguments), mods);
1372 public static System.Type __GetSystemType(TypeCode typeCode)
1376 case TypeCode.Boolean:
1377 return typeof(System.Boolean);
1379 return typeof(System.Byte);
1381 return typeof(System.Char);
1382 case TypeCode.DBNull:
1383 return typeof(System.DBNull);
1384 case TypeCode.DateTime:
1385 return typeof(System.DateTime);
1386 case TypeCode.Decimal:
1387 return typeof(System.Decimal);
1388 case TypeCode.Double:
1389 return typeof(System.Double);
1390 case TypeCode.Empty:
1392 case TypeCode.Int16:
1393 return typeof(System.Int16);
1394 case TypeCode.Int32:
1395 return typeof(System.Int32);
1396 case TypeCode.Int64:
1397 return typeof(System.Int64);
1398 case TypeCode.Object:
1399 return typeof(System.Object);
1400 case TypeCode.SByte:
1401 return typeof(System.SByte);
1402 case TypeCode.Single:
1403 return typeof(System.Single);
1404 case TypeCode.String:
1405 return typeof(System.String);
1406 case TypeCode.UInt16:
1407 return typeof(System.UInt16);
1408 case TypeCode.UInt32:
1409 return typeof(System.UInt32);
1410 case TypeCode.UInt64:
1411 return typeof(System.UInt64);
1413 throw new ArgumentOutOfRangeException();
1417 public static TypeCode GetTypeCode(Type type)
1421 return TypeCode.Empty;
1423 if (!type.__IsMissing && type.IsEnum)
1425 type = type.GetEnumUnderlyingType();
1427 Universe u = type.Module.universe;
1428 if (type == u.System_Boolean)
1430 return TypeCode.Boolean;
1432 else if (type == u.System_Char)
1434 return TypeCode.Char;
1436 else if (type == u.System_SByte)
1438 return TypeCode.SByte;
1440 else if (type == u.System_Byte)
1442 return TypeCode.Byte;
1444 else if (type == u.System_Int16)
1446 return TypeCode.Int16;
1448 else if (type == u.System_UInt16)
1450 return TypeCode.UInt16;
1452 else if (type == u.System_Int32)
1454 return TypeCode.Int32;
1456 else if (type == u.System_UInt32)
1458 return TypeCode.UInt32;
1460 else if (type == u.System_Int64)
1462 return TypeCode.Int64;
1464 else if (type == u.System_UInt64)
1466 return TypeCode.UInt64;
1468 else if (type == u.System_Single)
1470 return TypeCode.Single;
1472 else if (type == u.System_Double)
1474 return TypeCode.Double;
1476 else if (type == u.System_DateTime)
1478 return TypeCode.DateTime;
1480 else if (type == u.System_DBNull)
1482 return TypeCode.DBNull;
1484 else if (type == u.System_Decimal)
1486 return TypeCode.Decimal;
1488 else if (type == u.System_String)
1490 return TypeCode.String;
1492 else if (type.__IsMissing)
1494 throw new MissingMemberException(type);
1498 return TypeCode.Object;
1502 public Assembly Assembly
1504 get { return Module.Assembly; }
1507 // note that interface/delegate co- and contravariance is not considered
1508 public bool IsAssignableFrom(Type type)
1510 if (this.Equals(type))
1514 else if (type == null)
1518 else if (this.IsArray && type.IsArray)
1520 if (this.GetArrayRank() != type.GetArrayRank())
1524 else if (this.__IsVector && !type.__IsVector)
1528 Type e1 = this.GetElementType();
1529 Type e2 = type.GetElementType();
1530 return e1.IsValueType == e2.IsValueType && e1.IsAssignableFrom(e2);
1532 else if (this.IsSealed)
1536 else if (this.IsInterface)
1538 return Array.IndexOf(type.GetInterfaces(), this) != -1;
1540 else if (type.IsInterface)
1542 return this == this.Module.universe.System_Object;
1544 else if (type.IsPointer)
1546 return this == this.Module.universe.System_Object || this == this.Module.universe.System_ValueType;
1550 return type.IsSubclassOf(this);
1554 public bool IsSubclassOf(Type type)
1556 Type thisType = this.BaseType;
1557 while (thisType != null)
1559 if (thisType.Equals(type))
1563 thisType = thisType.BaseType;
1568 // This returns true if this type directly (i.e. not inherited from the base class) implements the interface.
1569 // Note that a complicating factor is that the interface itself can be implemented by an interface that extends it.
1570 private bool IsDirectlyImplementedInterface(Type interfaceType)
1572 foreach (Type iface in __GetDeclaredInterfaces())
1574 if (interfaceType.IsAssignableFrom(iface))
1582 public InterfaceMapping GetInterfaceMap(Type interfaceType)
1585 InterfaceMapping map = new InterfaceMapping();
1586 if (!IsDirectlyImplementedInterface(interfaceType))
1588 Type baseType = this.BaseType;
1589 if (baseType == null)
1591 throw new ArgumentException();
1595 map = baseType.GetInterfaceMap(interfaceType);
1600 map.InterfaceMethods = interfaceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
1601 map.InterfaceType = interfaceType;
1602 map.TargetMethods = new MethodInfo[map.InterfaceMethods.Length];
1603 FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1604 MethodInfo[] methods = GetMethods(BindingFlags.Instance | BindingFlags.Public);
1605 for (int i = 0; i < map.TargetMethods.Length; i++)
1607 if (map.TargetMethods[i] == null)
1609 // TODO use proper method resolution (also take into account that no implicit base class implementation is used across assembly boundaries)
1610 for (int j = 0; j < methods.Length; j++)
1612 if (methods[j].Name == map.InterfaceMethods[i].Name
1613 && methods[j].MethodSignature.Equals(map.InterfaceMethods[i].MethodSignature))
1615 map.TargetMethods[i] = methods[j];
1620 for (Type baseType = this.BaseType; baseType != null && interfaceType.IsAssignableFrom(baseType); baseType = baseType.BaseType)
1622 baseType.FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1625 map.TargetType = this;
1629 internal void FillInExplicitInterfaceMethods(MethodInfo[] interfaceMethods, MethodInfo[] targetMethods)
1631 __MethodImplMap impl = __GetMethodImplMap();
1632 for (int i = 0; i < impl.MethodDeclarations.Length; i++)
1634 for (int j = 0; j < impl.MethodDeclarations[i].Length; j++)
1636 int index = Array.IndexOf(interfaceMethods, impl.MethodDeclarations[i][j]);
1637 if (index != -1 && targetMethods[index] == null)
1639 targetMethods[index] = impl.MethodBodies[i];
1645 Type IGenericContext.GetGenericTypeArgument(int index)
1647 return GetGenericTypeArgument(index);
1650 Type IGenericContext.GetGenericMethodArgument(int index)
1652 throw new BadImageFormatException();
1655 Type IGenericBinder.BindTypeParameter(Type type)
1657 return GetGenericTypeArgument(type.GenericParameterPosition);
1660 Type IGenericBinder.BindMethodParameter(Type type)
1662 throw new BadImageFormatException();
1665 internal virtual Type BindTypeParameters(IGenericBinder binder)
1667 if (IsGenericTypeDefinition)
1669 Type[] args = GetGenericArguments();
1670 Type.InplaceBindTypeParameters(binder, args);
1671 return GenericTypeInstance.Make(this, args, null);
1679 private static void InplaceBindTypeParameters(IGenericBinder binder, Type[] types)
1681 for (int i = 0; i < types.Length; i++)
1683 types[i] = types[i].BindTypeParameters(binder);
1687 internal virtual MethodBase FindMethod(string name, MethodSignature signature)
1689 foreach (MethodBase method in __GetDeclaredMethods())
1691 if (method.Name == name && method.MethodSignature.Equals(signature))
1699 internal virtual FieldInfo FindField(string name, FieldSignature signature)
1701 foreach (FieldInfo field in __GetDeclaredFields())
1703 if (field.Name == name && field.FieldSignature.Equals(signature))
1711 internal bool IsAllowMultipleCustomAttribute
1715 IList<CustomAttributeData> cad = GetCustomAttributesData(this.Module.universe.System_AttributeUsageAttribute);
1718 foreach (CustomAttributeNamedArgument arg in cad[0].NamedArguments)
1720 if (arg.MemberInfo.Name == "AllowMultiple")
1722 return (bool)arg.TypedValue.Value;
1730 internal bool IsPseudoCustomAttribute
1734 Universe u = this.Module.universe;
1735 return this == u.System_NonSerializedAttribute
1736 || this == u.System_SerializableAttribute
1737 || this == u.System_Runtime_InteropServices_DllImportAttribute
1738 || this == u.System_Runtime_InteropServices_FieldOffsetAttribute
1739 || this == u.System_Runtime_InteropServices_InAttribute
1740 || this == u.System_Runtime_InteropServices_MarshalAsAttribute
1741 || this == u.System_Runtime_InteropServices_OutAttribute
1742 || this == u.System_Runtime_InteropServices_StructLayoutAttribute
1743 || this == u.System_Runtime_InteropServices_OptionalAttribute
1744 || this == u.System_Runtime_InteropServices_PreserveSigAttribute
1745 || this == u.System_Runtime_InteropServices_ComImportAttribute
1746 || this == u.System_Runtime_CompilerServices_SpecialNameAttribute
1747 || this == u.System_Runtime_CompilerServices_MethodImplAttribute
1752 internal Type MarkNotValueType()
1754 typeFlags |= TypeFlags.NotValueType;
1758 internal Type MarkValueType()
1760 typeFlags |= TypeFlags.ValueType;
1764 internal ConstructorInfo GetPseudoCustomAttributeConstructor(params Type[] parameterTypes)
1766 Universe u = this.Module.universe;
1767 MethodSignature methodSig = MethodSignature.MakeFromBuilder(u.System_Void, parameterTypes, new PackedCustomModifiers(), CallingConventions.Standard | CallingConventions.HasThis, 0);
1769 FindMethod(".ctor", methodSig) ??
1770 u.GetMissingMethodOrThrow(this, ".ctor", methodSig);
1771 return (ConstructorInfo)mb;
1774 public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
1776 return CreateMissingMethod(name, callingConvention, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, parameterTypes.Length));
1779 private MethodBase CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, PackedCustomModifiers customModifiers)
1781 MethodSignature sig = new MethodSignature(
1782 returnType ?? this.Module.universe.System_Void,
1783 Util.Copy(parameterTypes),
1787 MethodInfo method = new MissingMethod(this, name, sig);
1788 if (name == ".ctor" || name == ".cctor")
1790 return new ConstructorInfoImpl(method);
1795 [Obsolete("Please use __CreateMissingMethod(string, CallingConventions, Type, CustomModifiers, Type[], CustomModifiers[]) instead")]
1796 public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
1798 return CreateMissingMethod(name, callingConvention, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, parameterTypes.Length));
1801 public FieldInfo __CreateMissingField(string name, Type fieldType, CustomModifiers customModifiers)
1803 return new MissingField(this, name, FieldSignature.Create(fieldType, customModifiers));
1806 [Obsolete("Please use __CreateMissingField(string, Type, CustomModifiers) instead")]
1807 public FieldInfo __CreateMissingField(string name, Type fieldType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1809 return __CreateMissingField(name, fieldType, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1812 internal virtual Type SetMetadataTokenForMissing(int token)
1817 protected void MarkEnumOrValueType(string typeNamespace, string typeName)
1819 // we assume that mscorlib won't have nested types with these names,
1820 // so we don't check that we're not a nested type
1821 if (typeNamespace == "System"
1822 && (typeName == "Enum" || typeName == "ValueType"))
1824 typeFlags |= TypeFlags.PotentialEnumOrValueType;
1828 private bool ResolvePotentialEnumOrValueType()
1830 if (this.Assembly == this.Universe.Mscorlib || this.Assembly.GetName().Name.Equals("mscorlib", StringComparison.OrdinalIgnoreCase))
1832 typeFlags = (typeFlags & ~TypeFlags.PotentialEnumOrValueType) | TypeFlags.EnumOrValueType;
1837 typeFlags &= ~TypeFlags.PotentialEnumOrValueType;
1842 private bool IsEnumOrValueType
1846 return (typeFlags & (TypeFlags.EnumOrValueType | TypeFlags.PotentialEnumOrValueType)) != 0
1847 && ((typeFlags & TypeFlags.EnumOrValueType) != 0 || ResolvePotentialEnumOrValueType());
1851 internal virtual IList<CustomAttributeData> GetInterfaceImplCustomAttributes(Type interfaceType, Type attributeType)
1853 throw new NotSupportedException();
1856 internal virtual Universe Universe
1858 get { return Module.universe; }
1862 abstract class ElementHolderType : Type
1864 protected readonly Type elementType;
1866 private readonly CustomModifiers mods;
1868 protected ElementHolderType(Type elementType, CustomModifiers mods)
1870 this.elementType = elementType;
1874 protected bool EqualsHelper(ElementHolderType other)
1876 return other != null
1877 && other.elementType.Equals(elementType)
1878 && other.mods.Equals(mods);
1881 public override CustomModifiers __GetCustomModifiers()
1886 public sealed override string Name
1888 get { return elementType.Name + GetSuffix(); }
1891 public sealed override string Namespace
1893 get { return elementType.Namespace; }
1896 public sealed override string FullName
1898 get { return elementType.FullName + GetSuffix(); }
1901 public sealed override string ToString()
1903 return elementType.ToString() + GetSuffix();
1906 public sealed override Type GetElementType()
1911 public sealed override bool HasElementType
1913 get { return true; }
1916 public sealed override Module Module
1918 get { return elementType.Module; }
1921 internal sealed override int GetModuleBuilderToken()
1925 token = ((ModuleBuilder)elementType.Module).ImportType(this);
1930 public sealed override bool ContainsGenericParameters
1934 Type type = elementType;
1935 while (type.HasElementType)
1937 type = type.GetElementType();
1939 return type.ContainsGenericParameters;
1943 public sealed override bool __ContainsMissingType
1947 Type type = elementType;
1948 while (type.HasElementType)
1950 type = type.GetElementType();
1952 return type.__ContainsMissingType;
1956 internal sealed override Type BindTypeParameters(IGenericBinder binder)
1958 Type type = elementType.BindTypeParameters(binder);
1959 CustomModifiers mods = this.mods.Bind(binder);
1960 if (ReferenceEquals(type, elementType)
1961 && mods.Equals(this.mods))
1965 return Wrap(type, mods);
1968 internal override void CheckBaked()
1970 elementType.CheckBaked();
1973 internal sealed override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1975 return CustomAttributeData.EmptyList;
1978 internal sealed override Universe Universe
1980 get { return elementType.Universe; }
1983 internal abstract string GetSuffix();
1985 protected abstract Type Wrap(Type type, CustomModifiers mods);
1988 sealed class ArrayType : ElementHolderType
1990 internal static Type Make(Type type, CustomModifiers mods)
1992 return type.Universe.CanonicalizeType(new ArrayType(type, mods));
1995 private ArrayType(Type type, CustomModifiers mods)
2000 public override Type BaseType
2002 get { return elementType.Module.universe.System_Array; }
2005 public override Type[] __GetDeclaredInterfaces()
2008 this.Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
2009 this.Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
2010 this.Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
2014 public override MethodBase[] __GetDeclaredMethods()
2016 Type[] int32 = new Type[] { this.Module.universe.System_Int32 };
2017 List<MethodBase> list = new List<MethodBase>();
2018 list.Add(new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, new Type[] { this.Module.universe.System_Int32, elementType }));
2019 list.Add(new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
2020 list.Add(new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
2021 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
2022 for (Type type = elementType; type.__IsVector; type = type.GetElementType())
2024 Array.Resize(ref int32, int32.Length + 1);
2025 int32[int32.Length - 1] = int32[0];
2026 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
2028 return list.ToArray();
2031 public override TypeAttributes Attributes
2033 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
2036 public override bool IsArray
2038 get { return true; }
2041 public override bool __IsVector
2043 get { return true; }
2046 public override int GetArrayRank()
2051 public override bool Equals(object o)
2053 return EqualsHelper(o as ArrayType);
2056 public override int GetHashCode()
2058 return elementType.GetHashCode() * 5;
2061 internal override string GetSuffix()
2066 protected override Type Wrap(Type type, CustomModifiers mods)
2068 return Make(type, mods);
2072 sealed class MultiArrayType : ElementHolderType
2074 private readonly int rank;
2075 private readonly int[] sizes;
2076 private readonly int[] lobounds;
2078 internal static Type Make(Type type, int rank, int[] sizes, int[] lobounds, CustomModifiers mods)
2080 return type.Universe.CanonicalizeType(new MultiArrayType(type, rank, sizes, lobounds, mods));
2083 private MultiArrayType(Type type, int rank, int[] sizes, int[] lobounds, CustomModifiers mods)
2088 this.lobounds = lobounds;
2091 public override Type BaseType
2093 get { return elementType.Module.universe.System_Array; }
2096 public override MethodBase[] __GetDeclaredMethods()
2098 Type int32 = this.Module.universe.System_Int32;
2099 Type[] setArgs = new Type[rank + 1];
2100 Type[] getArgs = new Type[rank];
2101 Type[] ctorArgs = new Type[rank * 2];
2102 for (int i = 0; i < rank; i++)
2106 ctorArgs[i * 2 + 0] = int32;
2107 ctorArgs[i * 2 + 1] = int32;
2109 setArgs[rank] = elementType;
2110 return new MethodBase[] {
2111 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, getArgs)),
2112 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, ctorArgs)),
2113 new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, setArgs),
2114 new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), getArgs),
2115 new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, getArgs),
2119 public override TypeAttributes Attributes
2121 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
2124 public override bool IsArray
2126 get { return true; }
2129 public override int GetArrayRank()
2134 public override int[] __GetArraySizes()
2136 return Util.Copy(sizes);
2139 public override int[] __GetArrayLowerBounds()
2141 return Util.Copy(lobounds);
2144 public override bool Equals(object o)
2146 MultiArrayType at = o as MultiArrayType;
2147 return EqualsHelper(at)
2149 && ArrayEquals(at.sizes, sizes)
2150 && ArrayEquals(at.lobounds, lobounds);
2153 private static bool ArrayEquals(int[] i1, int[] i2)
2155 if (i1.Length == i2.Length)
2157 for (int i = 0; i < i1.Length; i++)
2169 public override int GetHashCode()
2171 return elementType.GetHashCode() * 9 + rank;
2174 internal override string GetSuffix()
2182 return "[" + new String(',', rank - 1) + "]";
2186 protected override Type Wrap(Type type, CustomModifiers mods)
2188 return Make(type, rank, sizes, lobounds, mods);
2192 sealed class BuiltinArrayMethod : ArrayMethod
2194 internal BuiltinArrayMethod(Module module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
2195 : base(module, arrayClass, methodName, callingConvention, returnType, parameterTypes)
2199 public override MethodAttributes Attributes
2201 get { return this.Name == ".ctor" ? MethodAttributes.RTSpecialName | MethodAttributes.Public : MethodAttributes.Public; }
2204 public override MethodImplAttributes GetMethodImplementationFlags()
2206 return MethodImplAttributes.IL;
2209 public override int MetadataToken
2211 get { return 0x06000000; }
2214 public override MethodBody GetMethodBody()
2219 public override ParameterInfo[] GetParameters()
2221 ParameterInfo[] parameterInfos = new ParameterInfo[parameterTypes.Length];
2222 for (int i = 0; i < parameterInfos.Length; i++)
2224 parameterInfos[i] = new ParameterInfoImpl(this, parameterTypes[i], i);
2226 return parameterInfos;
2229 public override ParameterInfo ReturnParameter
2231 get { return new ParameterInfoImpl(this, this.ReturnType, -1); }
2234 private sealed class ParameterInfoImpl : ParameterInfo
2236 private readonly MethodInfo method;
2237 private readonly Type type;
2238 private readonly int pos;
2240 internal ParameterInfoImpl(MethodInfo method, Type type, int pos)
2242 this.method = method;
2247 public override Type ParameterType
2249 get { return type; }
2252 public override string Name
2254 get { return null; }
2257 public override ParameterAttributes Attributes
2259 get { return ParameterAttributes.None; }
2262 public override int Position
2267 public override object RawDefaultValue
2269 get { return null; }
2272 public override CustomModifiers __GetCustomModifiers()
2274 return new CustomModifiers();
2277 public override MemberInfo Member
2279 get { return method.IsConstructor ? (MethodBase)new ConstructorInfoImpl(method) : method; }
2282 public override int MetadataToken
2284 get { return 0x8000000; }
2287 internal override Module Module
2289 get { return method.Module; }
2294 sealed class ByRefType : ElementHolderType
2296 internal static Type Make(Type type, CustomModifiers mods)
2298 return type.Universe.CanonicalizeType(new ByRefType(type, mods));
2301 private ByRefType(Type type, CustomModifiers mods)
2306 public override bool Equals(object o)
2308 return EqualsHelper(o as ByRefType);
2311 public override int GetHashCode()
2313 return elementType.GetHashCode() * 3;
2316 public override Type BaseType
2318 get { return null; }
2321 public override TypeAttributes Attributes
2326 public override bool IsByRef
2328 get { return true; }
2331 internal override string GetSuffix()
2336 protected override Type Wrap(Type type, CustomModifiers mods)
2338 return Make(type, mods);
2342 sealed class PointerType : ElementHolderType
2344 internal static Type Make(Type type, CustomModifiers mods)
2346 return type.Universe.CanonicalizeType(new PointerType(type, mods));
2349 private PointerType(Type type, CustomModifiers mods)
2354 public override bool Equals(object o)
2356 return EqualsHelper(o as PointerType);
2359 public override int GetHashCode()
2361 return elementType.GetHashCode() * 7;
2364 public override Type BaseType
2366 get { return null; }
2369 public override TypeAttributes Attributes
2374 public override bool IsPointer
2376 get { return true; }
2379 internal override string GetSuffix()
2384 protected override Type Wrap(Type type, CustomModifiers mods)
2386 return Make(type, mods);
2390 sealed class GenericTypeInstance : Type
2392 private readonly Type type;
2393 private readonly Type[] args;
2394 private readonly CustomModifiers[] mods;
2395 private Type baseType;
2398 internal static Type Make(Type type, Type[] typeArguments, CustomModifiers[] mods)
2400 bool identity = true;
2401 if (type is TypeBuilder || type is BakedType || type.__IsMissing)
2403 // a TypeBuiler identity must be instantiated
2408 // we must not instantiate the identity instance, because typeof(Foo<>).MakeGenericType(typeof(Foo<>).GetGenericArguments()) == typeof(Foo<>)
2409 for (int i = 0; i < typeArguments.Length; i++)
2411 if (typeArguments[i] != type.GetGenericTypeArgument(i)
2412 || !IsEmpty(mods, i))
2425 return type.Universe.CanonicalizeType(new GenericTypeInstance(type, typeArguments, mods));
2429 private static bool IsEmpty(CustomModifiers[] mods, int i)
2431 // we need to be extra careful, because mods doesn't not need to be in canonical format
2432 // (Signature.ReadGenericInst() calls Make() directly, without copying the modifier arrays)
2433 return mods == null || mods[i].IsEmpty;
2436 private GenericTypeInstance(Type type, Type[] args, CustomModifiers[] mods)
2443 public override bool Equals(object o)
2445 GenericTypeInstance gt = o as GenericTypeInstance;
2446 return gt != null && gt.type.Equals(type) && Util.ArrayEquals(gt.args, args)
2447 && Util.ArrayEquals(gt.mods, mods);
2450 public override int GetHashCode()
2452 return type.GetHashCode() * 3 ^ Util.GetHashCode(args);
2455 public override string AssemblyQualifiedName
2459 string fn = FullName;
2460 return fn == null ? null : fn + ", " + type.Assembly.FullName;
2464 public override Type BaseType
2468 if (baseType == null)
2470 Type rawBaseType = type.BaseType;
2471 if (rawBaseType == null)
2473 baseType = rawBaseType;
2477 baseType = rawBaseType.BindTypeParameters(this);
2484 public override bool IsValueType
2486 get { return type.IsValueType; }
2489 public override bool IsVisible
2495 foreach (Type arg in args)
2508 public override Type DeclaringType
2510 get { return type.DeclaringType; }
2513 public override TypeAttributes Attributes
2515 get { return type.Attributes; }
2518 internal override void CheckBaked()
2523 public override FieldInfo[] __GetDeclaredFields()
2525 FieldInfo[] fields = type.__GetDeclaredFields();
2526 for (int i = 0; i < fields.Length; i++)
2528 fields[i] = fields[i].BindTypeParameters(this);
2533 public override Type[] __GetDeclaredInterfaces()
2535 Type[] interfaces = type.__GetDeclaredInterfaces();
2536 for (int i = 0; i < interfaces.Length; i++)
2538 interfaces[i] = interfaces[i].BindTypeParameters(this);
2543 public override MethodBase[] __GetDeclaredMethods()
2545 MethodBase[] methods = type.__GetDeclaredMethods();
2546 for (int i = 0; i < methods.Length; i++)
2548 methods[i] = methods[i].BindTypeParameters(this);
2553 public override Type[] __GetDeclaredTypes()
2555 return type.__GetDeclaredTypes();
2558 public override EventInfo[] __GetDeclaredEvents()
2560 EventInfo[] events = type.__GetDeclaredEvents();
2561 for (int i = 0; i < events.Length; i++)
2563 events[i] = events[i].BindTypeParameters(this);
2568 public override PropertyInfo[] __GetDeclaredProperties()
2570 PropertyInfo[] properties = type.__GetDeclaredProperties();
2571 for (int i = 0; i < properties.Length; i++)
2573 properties[i] = properties[i].BindTypeParameters(this);
2578 public override __MethodImplMap __GetMethodImplMap()
2580 __MethodImplMap map = type.__GetMethodImplMap();
2581 map.TargetType = this;
2582 for (int i = 0; i < map.MethodBodies.Length; i++)
2584 map.MethodBodies[i] = (MethodInfo)map.MethodBodies[i].BindTypeParameters(this);
2585 for (int j = 0; j < map.MethodDeclarations[i].Length; j++)
2587 Type interfaceType = map.MethodDeclarations[i][j].DeclaringType;
2588 if (interfaceType.IsGenericType)
2590 map.MethodDeclarations[i][j] = (MethodInfo)map.MethodDeclarations[i][j].BindTypeParameters(this);
2597 public override string Namespace
2599 get { return type.Namespace; }
2602 public override string Name
2604 get { return type.Name; }
2607 public override string FullName
2611 if (!this.__ContainsMissingType && this.ContainsGenericParameters)
2615 StringBuilder sb = new StringBuilder(this.type.FullName);
2618 foreach (Type type in args)
2620 sb.Append(sep).Append('[').Append(type.FullName).Append(", ").Append(type.Assembly.FullName.Replace("]", "\\]")).Append(']');
2624 return sb.ToString();
2628 public override string ToString()
2630 StringBuilder sb = new StringBuilder(type.FullName);
2633 foreach (Type arg in args)
2640 return sb.ToString();
2643 public override Module Module
2645 get { return type.Module; }
2648 public override bool IsGenericType
2650 get { return true; }
2653 internal override bool IsGenericTypeInstance
2655 get { return true; }
2658 public override Type GetGenericTypeDefinition()
2663 public override Type[] GetGenericArguments()
2665 return Util.Copy(args);
2668 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
2670 return mods != null ? (CustomModifiers[])mods.Clone() : new CustomModifiers[args.Length];
2673 internal override Type GetGenericTypeArgument(int index)
2678 public override bool ContainsGenericParameters
2682 foreach (Type type in args)
2684 if (type.ContainsGenericParameters)
2693 public override bool __ContainsMissingType
2697 foreach (Type type in args)
2699 if (type.__ContainsMissingType)
2704 return this.type.__IsMissing;
2708 public override StructLayoutAttribute StructLayoutAttribute
2710 get { return type.StructLayoutAttribute; }
2713 internal override int GetModuleBuilderToken()
2717 token = ((ModuleBuilder)type.Module).ImportType(this);
2722 internal override Type BindTypeParameters(IGenericBinder binder)
2724 for (int i = 0; i < args.Length; i++)
2726 Type xarg = args[i].BindTypeParameters(binder);
2727 if (!ReferenceEquals(xarg, args[i]))
2729 Type[] xargs = new Type[args.Length];
2730 Array.Copy(args, xargs, i);
2732 for (; i < args.Length; i++)
2734 xargs[i] = args[i].BindTypeParameters(binder);
2736 return Make(type, xargs, null);
2742 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
2744 return type.GetCustomAttributesData(attributeType);
2748 sealed class FunctionPointerType : Type
2750 private readonly Universe universe;
2751 private readonly __StandAloneMethodSig sig;
2753 internal static Type Make(Universe universe, __StandAloneMethodSig sig)
2755 return universe.CanonicalizeType(new FunctionPointerType(universe, sig));
2758 private FunctionPointerType(Universe universe, __StandAloneMethodSig sig)
2760 this.universe = universe;
2764 public override bool Equals(object obj)
2766 FunctionPointerType other = obj as FunctionPointerType;
2767 return other != null
2768 && other.universe == universe
2769 && other.sig.Equals(sig);
2772 public override int GetHashCode()
2774 return sig.GetHashCode();
2777 public override bool __IsFunctionPointer
2779 get { return true; }
2782 public override __StandAloneMethodSig __MethodSignature
2787 public override Type BaseType
2789 get { return null; }
2792 public override TypeAttributes Attributes
2797 public override string Name
2799 get { throw new InvalidOperationException(); }
2802 public override string FullName
2804 get { throw new InvalidOperationException(); }
2807 public override Module Module
2809 get { throw new InvalidOperationException(); }
2812 internal override Universe Universe
2814 get { return universe; }
2817 public override string ToString()
2819 return "<FunctionPtr>";