2 // import.cs: System.Reflection conversions
4 // Authors: Marek Safar (marek.safar@gmail.com)
6 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 // Copyright 2009, 2010 Novell, Inc
12 using System.Reflection;
13 using System.Runtime.CompilerServices;
15 using System.Collections.Generic;
16 using System.Diagnostics;
17 using System.Runtime.InteropServices;
21 public static class Import
23 static Dictionary<Type, TypeSpec> import_cache;
24 static Dictionary<Type, PredefinedTypeSpec> type_2_predefined;
26 public static void Initialize ()
28 import_cache = new Dictionary<Type, TypeSpec> (1024, ReferenceEquality<Type>.Default);
30 // Setup mapping for predefined types
31 type_2_predefined = new Dictionary<Type, PredefinedTypeSpec> () {
32 { typeof (object), TypeManager.object_type },
33 { typeof (System.ValueType), TypeManager.value_type },
34 { typeof (System.Attribute), TypeManager.attribute_type },
36 { typeof (int), TypeManager.int32_type },
37 { typeof (long), TypeManager.int64_type },
38 { typeof (uint), TypeManager.uint32_type },
39 { typeof (ulong), TypeManager.uint64_type },
40 { typeof (byte), TypeManager.byte_type },
41 { typeof (sbyte), TypeManager.sbyte_type },
42 { typeof (short), TypeManager.short_type },
43 { typeof (ushort), TypeManager.ushort_type },
45 { typeof (System.Collections.IEnumerator), TypeManager.ienumerator_type },
46 { typeof (System.Collections.IEnumerable), TypeManager.ienumerable_type },
47 { typeof (System.IDisposable), TypeManager.idisposable_type },
49 { typeof (char), TypeManager.char_type },
50 { typeof (string), TypeManager.string_type },
51 { typeof (float), TypeManager.float_type },
52 { typeof (double), TypeManager.double_type },
53 { typeof (decimal), TypeManager.decimal_type },
54 { typeof (bool), TypeManager.bool_type },
55 { typeof (System.IntPtr), TypeManager.intptr_type },
56 { typeof (System.UIntPtr), TypeManager.uintptr_type },
58 { typeof (System.MulticastDelegate), TypeManager.multicast_delegate_type },
59 { typeof (System.Delegate), TypeManager.delegate_type },
60 { typeof (System.Enum), TypeManager.enum_type },
61 { typeof (System.Array), TypeManager.array_type },
62 { typeof (void), TypeManager.void_type },
63 { typeof (System.Type), TypeManager.type_type },
64 { typeof (System.Exception), TypeManager.exception_type },
65 { typeof (System.RuntimeFieldHandle), TypeManager.runtime_field_handle_type },
66 { typeof (System.RuntimeTypeHandle), TypeManager.runtime_handle_type }
70 public static FieldSpec CreateField (FieldInfo fi, TypeSpec declaringType)
73 var fa = fi.Attributes;
74 switch (fa & FieldAttributes.FieldAccessMask) {
75 case FieldAttributes.Public:
76 mod = Modifiers.PUBLIC;
78 case FieldAttributes.Assembly:
79 mod = Modifiers.INTERNAL;
81 case FieldAttributes.Family:
82 mod = Modifiers.PROTECTED;
84 case FieldAttributes.FamORAssem:
85 mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
88 mod = Modifiers.PRIVATE;
92 // Ignore private fields (even for error reporting) to not require extra dependencies
93 if (mod == Modifiers.PRIVATE)
96 var definition = new ImportedMemberDefinition (fi);
98 if ((fa & FieldAttributes.Literal) != 0) {
99 var c = Constant.CreateConstantFromValue (ImportType (fi.FieldType), fi.GetValue (fi), Location.Null);
100 return new ConstSpec (declaringType, definition, ImportType (fi.FieldType), fi, mod, c);
103 if ((fa & FieldAttributes.InitOnly) != 0) {
104 if (fi.FieldType == typeof (decimal)) {
105 var dc = ReadDecimalConstant (fi);
107 return new ConstSpec (declaringType, definition, ImportType (fi.FieldType), fi, mod, dc);
110 mod |= Modifiers.READONLY;
112 var reqs = fi.GetRequiredCustomModifiers ();
113 if (reqs.Length > 0) {
114 foreach (Type t in reqs) {
115 if (t == typeof (System.Runtime.CompilerServices.IsVolatile)) {
116 mod |= Modifiers.VOLATILE;
123 if ((fa & FieldAttributes.Static) != 0)
124 mod |= Modifiers.STATIC;
126 if (fi.FieldType.IsValueType) {
127 if (fi.IsDefined (typeof (FixedBufferAttribute), false)) {
128 var element_field = CreateField (fi.FieldType.GetField (FixedField.FixedElementName), declaringType);
129 return new FixedFieldSpec (declaringType, definition, fi, element_field, mod);
133 return new FieldSpec (declaringType, definition, ImportType (fi.FieldType), fi, mod);
136 public static EventSpec CreateEvent (EventInfo ei, TypeSpec declaringType, MethodSpec add, MethodSpec remove)
138 add.IsAccessor = true;
139 remove.IsAccessor = true;
141 if (add.Modifiers != remove.Modifiers)
142 throw new NotImplementedException ("Different accessor modifiers " + ei.Name);
144 var definition = new ImportedMemberDefinition (ei);
145 return new EventSpec (declaringType, definition, ImportType (ei.EventHandlerType), add.Modifiers, add, remove);
148 static T[] CreateGenericParameters<T> (Type type, TypeSpec declaringType) where T : TypeSpec
150 Type[] tparams = type.GetGenericArguments ();
152 int parent_owned_count;
154 parent_owned_count = type.DeclaringType.GetGenericArguments ().Length;
157 // System.Reflection duplicates parent type parameters for each
158 // nested type with slightly modified properties (eg. different owner)
159 // This just makes things more complicated (think of cloned constraints)
160 // therefore we remap any nested type owned by parent using `type_cache'
161 // to the single TypeParameterSpec
163 if (declaringType != null && parent_owned_count > 0) {
165 while (read_count != parent_owned_count) {
166 var tparams_count = declaringType.Arity;
167 if (tparams_count != 0) {
168 var parent_tp = declaringType.MemberDefinition.TypeParameters;
169 read_count += tparams_count;
170 for (int i = 0; i < tparams_count; i++) {
171 import_cache.Add (tparams[parent_owned_count - read_count + i], parent_tp[i]);
175 declaringType = declaringType.DeclaringType;
179 parent_owned_count = 0;
182 if (tparams.Length - parent_owned_count == 0)
185 return CreateGenericParameters<T> (parent_owned_count, tparams);
188 static T[] CreateGenericParameters<T> (int first, Type[] tparams) where T : TypeSpec
190 var tspec = new T [tparams.Length - first];
191 for (int pos = first; pos < tparams.Length; ++pos) {
192 var type = tparams[pos];
193 if (type.HasElementType) {
194 var element = type.GetElementType ();
195 var spec = CreateType (element);
198 tspec[pos - first] = (T) (TypeSpec) ArrayContainer.MakeType (spec, type.GetArrayRank ());
202 throw new NotImplementedException ("Unknown element type " + type.ToString ());
205 tspec [pos - first] = (T) CreateType (type);
211 public static MethodSpec CreateMethod (MethodBase mb, TypeSpec declaringType)
213 Modifiers mod = ReadMethodModifiers (mb, declaringType);
214 //if (declaringType.IsInterface) {
215 // mod = (mod & ~Modifiers.ABSTRACT) | Modifiers.VIRTUAL;
218 TypeParameterSpec[] tparams;
219 ImportedMethodDefinition definition;
221 var parameters = ParametersImported.Create (declaringType, mb);
223 if (mb.IsGenericMethod) {
224 if (!mb.IsGenericMethodDefinition)
225 throw new NotSupportedException ("assert");
227 tparams = CreateGenericParameters<TypeParameterSpec>(0, mb.GetGenericArguments ());
228 definition = new ImportedGenericMethodDefinition ((MethodInfo) mb, parameters, tparams);
230 definition = new ImportedMethodDefinition (mb, parameters);
236 if (mb.MemberType == MemberTypes.Constructor) {
237 kind = MemberKind.Constructor;
238 returnType = TypeManager.void_type;
241 // Detect operators and destructors
243 string name = mb.Name;
244 kind = MemberKind.Method;
245 if (tparams == null && !mb.DeclaringType.IsInterface && name.Length > 6) {
246 if ((mod & (Modifiers.STATIC | Modifiers.PUBLIC)) == (Modifiers.STATIC | Modifiers.PUBLIC)) {
247 if (name[2] == '_' && name[1] == 'p' && name[0] == 'o') {
248 var op_type = Operator.GetType (name);
249 if (op_type.HasValue && parameters.Count > 0 && parameters.Count < 3) {
250 kind = MemberKind.Operator;
253 } else if (parameters.IsEmpty && name == Destructor.MetadataName) {
254 kind = MemberKind.Destructor;
258 returnType = ImportType (((MethodInfo)mb).ReturnType);
260 // Cannot set to OVERRIDE without full hierarchy checks
261 // this flag indicates that the method could be override
262 // but further validation is needed
263 if ((mod & Modifiers.OVERRIDE) != 0 && kind == MemberKind.Method && declaringType.BaseType != null) {
264 var filter = MemberFilter.Method (name, tparams != null ? tparams.Length : 0, parameters, null);
265 var candidate = MemberCache.FindMember (declaringType.BaseType, filter, BindingRestriction.None);
268 // For imported class method do additional validation to be sure that metadata
269 // override flag was correct
271 // Difference between protected internal and protected is ok
273 const Modifiers conflict_mask = Modifiers.AccessibilityMask & ~Modifiers.INTERNAL;
274 if (candidate == null || (candidate.Modifiers & conflict_mask) != (mod & conflict_mask) || candidate.IsStatic) {
275 mod &= ~Modifiers.OVERRIDE;
280 MethodSpec ms = new MethodSpec (kind, declaringType, definition, returnType, mb, parameters, mod);
288 // Returns null when the property is not valid C# property
290 public static PropertySpec CreateProperty (PropertyInfo pi, TypeSpec declaringType, MethodSpec get, MethodSpec set)
292 var definition = new ImportedMemberDefinition (pi);
295 AParametersCollection param = null;
296 TypeSpec type = null;
299 param = get.Parameters;
300 type = get.ReturnType;
303 bool is_valid_property = true;
305 if (set.ReturnType != TypeManager.void_type)
306 is_valid_property = false;
308 var set_param_count = set.Parameters.Count - 1;
309 if (set_param_count < 0)
310 is_valid_property = false;
312 var data = new IParameterData [set_param_count];
313 var types = new TypeSpec[set_param_count];
314 for (int i = 0; i < set_param_count; ++i ) {
315 data[i] = set.Parameters.FixedParameters[i];
316 types[i] = set.Parameters.Types[i];
319 var set_param = new ParametersImported (data, types);
320 var set_type = set.Parameters.Types[set_param_count];
327 if (set_param_count != get.Parameters.Count)
328 is_valid_property = false;
330 if (get.ReturnType != set_type)
331 is_valid_property = false;
333 // Possible custom accessor modifiers
334 if ((mod & Modifiers.AccessibilityMask) != (set.Modifiers & Modifiers.AccessibilityMask)) {
335 var get_acc = mod & Modifiers.AccessibilityMask;
336 if (get_acc != Modifiers.PUBLIC) {
337 var set_acc = set.Modifiers & Modifiers.AccessibilityMask;
338 // If the accessor modifiers are not same, do extra restriction checks
339 if (get_acc != set_acc) {
340 var get_restr = ModifiersExtensions.IsRestrictedModifier (get_acc, set_acc);
341 var set_restr = ModifiersExtensions.IsRestrictedModifier (set_acc, get_acc);
342 if (get_restr && set_restr) {
343 is_valid_property = false; // Neither is more restrictive
347 mod &= ~Modifiers.AccessibilityMask;
356 PropertySpec spec = null;
357 if (!param.IsEmpty) {
358 var index_name = declaringType.MemberDefinition.GetAttributeDefaultMember ();
359 if (index_name == null) {
360 is_valid_property = false;
364 is_valid_property = false;
365 if (get.Name.IndexOf (index_name, StringComparison.Ordinal) != 4)
366 is_valid_property = false;
370 is_valid_property = false;
371 if (set.Name.IndexOf (index_name, StringComparison.Ordinal) != 4)
372 is_valid_property = false;
376 if (is_valid_property)
377 spec = new IndexerSpec (declaringType, definition, type, param, pi, mod);
381 spec = new PropertySpec (MemberKind.Property, declaringType, definition, type, pi, mod);
383 if (!is_valid_property) {
384 spec.IsNotRealProperty = true;
396 public static TypeSpec CreateType (Type type)
398 return CreateType (type, null);
401 public static TypeSpec CreateType (Type type, TypeSpec declaringType)
404 if (import_cache.TryGetValue (type, out spec))
407 if (type.IsGenericType && !type.IsGenericTypeDefinition) {
408 var type_def = type.GetGenericTypeDefinition ();
409 spec = CreateType (type_def, declaringType);
411 var targs = CreateGenericParameters<TypeSpec> (type, null);
413 InflatedTypeSpec inflated;
415 // Inflating nested non-generic type, same in TypeSpec::InflateMember
416 inflated = new InflatedTypeSpec (spec, declaringType, TypeSpec.EmptyTypes);
418 // CreateGenericParameters constraint could inflate type
419 if (import_cache.ContainsKey (type))
420 return import_cache[type];
422 inflated = spec.MakeGenericType (targs);
424 // Use of reading cache to speed up reading only
425 import_cache.Add (type, inflated);
434 var ma = type.Attributes;
435 switch (ma & TypeAttributes.VisibilityMask) {
436 case TypeAttributes.Public:
437 case TypeAttributes.NestedPublic:
438 mod = Modifiers.PUBLIC;
440 case TypeAttributes.NestedPrivate:
441 mod = Modifiers.PRIVATE;
443 case TypeAttributes.NestedFamily:
444 mod = Modifiers.PROTECTED;
446 case TypeAttributes.NestedFamORAssem:
447 mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
450 mod = Modifiers.INTERNAL;
454 if ((ma & TypeAttributes.Interface) != 0) {
455 kind = MemberKind.Interface;
456 } else if (type.IsGenericParameter) {
457 kind = MemberKind.TypeParameter;
458 } else if (type.IsClass || type.IsAbstract) { // SRE: System.Enum returns false for IsClass
459 if ((ma & TypeAttributes.Sealed) != 0 && type.IsSubclassOf (typeof (MulticastDelegate))) {
460 kind = MemberKind.Delegate;
462 kind = MemberKind.Class;
464 if (type == typeof (object)) {
466 var pa = PredefinedAttributes.Get.Dynamic.Type;
467 if (pa != null && type.IsDefined (typeof (DynamicAttribute), false))
468 return InternalType.Dynamic;
472 if ((ma & TypeAttributes.Sealed) != 0) {
473 mod |= Modifiers.SEALED;
474 if ((ma & TypeAttributes.Abstract) != 0)
475 mod |= Modifiers.STATIC;
476 } else if ((ma & TypeAttributes.Abstract) != 0) {
477 mod |= Modifiers.ABSTRACT;
480 } else if (type.IsEnum) {
481 kind = MemberKind.Enum;
483 kind = MemberKind.Struct;
484 mod |= Modifiers.SEALED;
487 var definition = new ImportedTypeDefinition (type);
488 PredefinedTypeSpec pt;
490 if (kind == MemberKind.Enum) {
491 const BindingFlags underlying_member = BindingFlags.DeclaredOnly |
492 BindingFlags.Instance |
493 BindingFlags.Public | BindingFlags.NonPublic;
495 var type_members = type.GetFields (underlying_member);
496 foreach (var type_member in type_members) {
497 spec = new EnumSpec (declaringType, definition, Import.CreateType (type_member.FieldType), type, mod);
502 kind = MemberKind.Class;
504 } else if (kind == MemberKind.TypeParameter) {
505 // Return as type_cache was updated
506 return CreateTypeParameter (type, declaringType);
507 } else if (type.IsGenericTypeDefinition) {
508 definition.TypeParameters = CreateGenericParameters<TypeParameterSpec>(type, declaringType);
510 // Constraints are not loaded on demand and can reference this type
511 if (import_cache.TryGetValue (type, out spec))
514 } else if (type_2_predefined.TryGetValue (type, out pt)) {
516 pt.SetDefinition (definition, type);
520 spec = new TypeSpec (kind, declaringType, definition, type, mod);
522 import_cache.Add (type, spec);
524 if (kind == MemberKind.Interface)
525 spec.BaseType = TypeManager.object_type;
526 else if (type.BaseType != null)
527 spec.BaseType = CreateType (type.BaseType);
529 var ifaces = type.GetInterfaces ();
530 if (ifaces.Length > 0) {
531 foreach (Type iface in ifaces) {
532 spec.AddInterface (Import.CreateType (iface));
539 static TypeParameterSpec CreateTypeParameter (Type type, TypeSpec declaringType)
542 switch (type.GenericParameterAttributes & GenericParameterAttributes.VarianceMask) {
543 case GenericParameterAttributes.Covariant:
544 variance = Variance.Covariant;
546 case GenericParameterAttributes.Contravariant:
547 variance = Variance.Contravariant;
550 variance = Variance.None;
554 SpecialConstraint special = SpecialConstraint.None;
555 var import_special = type.GenericParameterAttributes & GenericParameterAttributes.SpecialConstraintMask;
557 if ((import_special & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0) {
558 special |= SpecialConstraint.Struct;
559 } else if ((import_special & GenericParameterAttributes.DefaultConstructorConstraint) != 0) {
560 special = SpecialConstraint.Constructor;
563 if ((import_special & GenericParameterAttributes.ReferenceTypeConstraint) != 0) {
564 special |= SpecialConstraint.Class;
567 TypeParameterSpec spec;
568 var def = new ImportedTypeParameterDefinition (type);
569 if (type.DeclaringMethod != null)
570 spec = new TypeParameterSpec (type.GenericParameterPosition, def, special, variance, type);
572 spec = new TypeParameterSpec (declaringType, type.GenericParameterPosition, def, special, variance, type);
574 // Add it now, so any constraint can reference it and get same instance
575 import_cache.Add (type, spec);
577 var constraints = type.GetGenericParameterConstraints ();
578 foreach (var ct in constraints) {
579 // TODO MemberCache: What to do ??
580 if (ct.IsGenericParameter) {
585 if (ct == typeof (ValueType)) {
586 spec.BaseType = TypeManager.value_type;
588 spec.BaseType = CreateType (ct);
594 spec.AddInterface (CreateType (ct));
597 if (spec.BaseType == null)
598 spec.BaseType = TypeManager.object_type;
603 public static TypeSpec ImportType (Type type)
605 if (type.HasElementType) {
606 var element = type.GetElementType ();
607 var spec = ImportType (element);
610 return ArrayContainer.MakeType (spec, type.GetArrayRank ());
612 return ReferenceContainer.MakeType (spec);
614 return PointerContainer.MakeType (spec);
616 throw new NotImplementedException ("Unknown element type " + type.ToString ());
621 dtype = ImportType (type.DeclaringType);
625 return CreateType (type, dtype);
629 // Decimal constants cannot be encoded in the constant blob, and thus are marked
630 // as IsInitOnly ('readonly' in C# parlance). We get its value from the
631 // DecimalConstantAttribute metadata.
633 static Constant ReadDecimalConstant (FieldInfo fi)
635 object[] attrs = fi.GetCustomAttributes (typeof (DecimalConstantAttribute), false);
636 if (attrs.Length != 1)
639 return new DecimalConstant (((DecimalConstantAttribute) attrs [0]).Value, Location.Null);
642 static Modifiers ReadMethodModifiers (MethodBase mb, TypeSpec declaringType)
645 var ma = mb.Attributes;
646 switch (ma & MethodAttributes.MemberAccessMask) {
647 case MethodAttributes.Public:
648 mod = Modifiers.PUBLIC;
650 case MethodAttributes.Assembly:
651 mod = Modifiers.INTERNAL;
653 case MethodAttributes.Family:
654 mod = Modifiers.PROTECTED;
656 case MethodAttributes.FamORAssem:
657 mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
660 mod = Modifiers.PRIVATE;
664 if ((ma & MethodAttributes.Static) != 0) {
665 mod |= Modifiers.STATIC;
666 } else if ((ma & MethodAttributes.Final) != 0) {
667 mod |= Modifiers.SEALED;
668 } else if ((ma & MethodAttributes.Abstract) != 0 && declaringType.IsClass) {
669 mod |= Modifiers.ABSTRACT;
672 // It can be sealed and override
673 if ((ma & MethodAttributes.Virtual) != 0) {
674 if ((ma & MethodAttributes.NewSlot) != 0 || !declaringType.IsClass || mod == Modifiers.PRIVATE) {
675 mod |= Modifiers.VIRTUAL;
677 mod |= Modifiers.OVERRIDE;
685 class ImportedMemberDefinition : IMemberDefinition
687 protected class AttributesBag
689 public static readonly AttributesBag Default = new AttributesBag ();
691 public AttributeUsageAttribute AttributeUsage;
692 public ObsoleteAttribute Obsolete;
693 public string[] Conditionals;
694 public string DefaultIndexerName;
695 public bool IsNotCLSCompliant;
697 public static AttributesBag Read (MemberInfo mi)
699 AttributesBag bag = null;
700 List<string> conditionals = null;
702 var attrs = CustomAttributeData.GetCustomAttributes (mi);
703 foreach (var a in attrs) {
704 var type = a.Constructor.DeclaringType;
705 if (type == typeof (ObsoleteAttribute)) {
707 bag = new AttributesBag ();
709 var args = a.ConstructorArguments;
711 if (args.Count == 1) {
712 bag.Obsolete = new ObsoleteAttribute ((string) args[0].Value);
713 } else if (args.Count == 2) {
714 bag.Obsolete = new ObsoleteAttribute ((string) args[0].Value, (bool) args[1].Value);
716 bag.Obsolete = new ObsoleteAttribute ();
722 if (type == typeof (ConditionalAttribute)) {
724 bag = new AttributesBag ();
726 if (conditionals == null)
727 conditionals = new List<string> (2);
729 conditionals.Add ((string) a.ConstructorArguments[0].Value);
733 if (type == typeof (CLSCompliantAttribute)) {
735 bag = new AttributesBag ();
737 bag.IsNotCLSCompliant = !(bool) a.ConstructorArguments[0].Value;
741 // Type only attributes
742 if (type == typeof (DefaultMemberAttribute)) {
744 bag = new AttributesBag ();
746 bag.DefaultIndexerName = (string) a.ConstructorArguments[0].Value;
750 if (type == typeof (AttributeUsageAttribute)) {
752 bag = new AttributesBag ();
754 bag.AttributeUsage = new AttributeUsageAttribute ((AttributeTargets) a.ConstructorArguments[0].Value);
755 foreach (var named in a.NamedArguments) {
756 if (named.MemberInfo.Name == "AllowMultiple")
757 bag.AttributeUsage.AllowMultiple = (bool) named.TypedValue.Value;
758 else if (named.MemberInfo.Name == "Inherited")
759 bag.AttributeUsage.Inherited = (bool) named.TypedValue.Value;
768 if (conditionals != null)
769 bag.Conditionals = conditionals.ToArray ();
775 protected readonly MemberInfo provider;
776 protected AttributesBag cattrs;
778 public ImportedMemberDefinition (MemberInfo provider)
780 this.provider = provider;
785 public Assembly Assembly {
787 return provider.Module.Assembly;
791 public bool IsImported {
797 public virtual string Name {
799 return provider.Name;
805 public string[] ConditionalConditions ()
810 return cattrs.Conditionals;
813 public ObsoleteAttribute GetAttributeObsolete ()
818 return cattrs.Obsolete;
821 public bool IsNotCLSCompliant ()
826 return cattrs.IsNotCLSCompliant;
829 protected void ReadAttributes ()
831 cattrs = AttributesBag.Read (provider);
834 public void SetIsAssigned ()
836 // Unused for imported members
839 public void SetIsUsed ()
841 // Unused for imported members
845 class ImportedMethodDefinition : ImportedMemberDefinition, IParametersMember
847 readonly AParametersCollection parameters;
849 public ImportedMethodDefinition (MethodBase provider, AParametersCollection parameters)
852 this.parameters = parameters;
857 public AParametersCollection Parameters {
863 public TypeSpec MemberType {
865 throw new NotImplementedException ();
872 class ImportedGenericMethodDefinition : ImportedMethodDefinition, IGenericMethodDefinition
874 TypeParameterSpec[] tparams;
876 public ImportedGenericMethodDefinition (MethodInfo provider, AParametersCollection parameters, TypeParameterSpec[] tparams)
877 : base (provider, parameters)
879 this.tparams = tparams;
884 public TypeParameterSpec[] TypeParameters {
890 public int TypeParametersCount {
892 return tparams.Length;
899 class ImportedTypeDefinition : ImportedMemberDefinition, ITypeDefinition
901 TypeParameterSpec[] tparams;
904 public ImportedTypeDefinition (Type type)
911 public override string Name {
916 name = name.Substring (0, name.IndexOf ('`'));
923 public string Namespace {
925 return ((Type) provider).Namespace;
929 public int TypeParametersCount {
931 return tparams == null ? 0 : tparams.Length;
935 public TypeParameterSpec[] TypeParameters {
946 public TypeSpec GetAttributeCoClass ()
948 // TODO: Use ReadAttributes
949 var attr = provider.GetCustomAttributes (typeof (CoClassAttribute), false);
953 return Import.CreateType (((CoClassAttribute) attr[0]).CoClass);
956 public string GetAttributeDefaultMember ()
961 return cattrs.DefaultIndexerName;
964 public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
969 return cattrs.AttributeUsage;
972 public MemberCache LoadMembers (TypeSpec declaringType)
974 var loading_type = (Type) provider;
975 const BindingFlags all_members = BindingFlags.DeclaredOnly |
976 BindingFlags.Static | BindingFlags.Instance |
977 BindingFlags.Public | BindingFlags.NonPublic;
979 const MethodAttributes explicit_impl = MethodAttributes.NewSlot |
980 MethodAttributes.Virtual | MethodAttributes.HideBySig |
981 MethodAttributes.Final;
983 Dictionary<MethodBase, MethodSpec> possible_accessors = null;
988 // This requires methods to be returned first which seems to work for both Mono and .NET
992 all = loading_type.GetMembers (all_members);
993 } catch (Exception e) {
994 throw new InternalErrorException (e, "Could not import type `{0}' from `{1}'",
995 declaringType.GetSignatureForError (), declaringType.Assembly.Location);
998 var cache = new MemberCache (all.Length);
999 foreach (var member in all) {
1000 switch (member.MemberType) {
1001 case MemberTypes.Constructor:
1002 case MemberTypes.Method:
1003 MethodBase mb = (MethodBase) member;
1005 // Ignore explicitly implemented members
1006 if ((mb.Attributes & explicit_impl) == explicit_impl && (mb.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private)
1009 // Ignore compiler generated methods
1010 if (mb.IsPrivate && mb.IsDefined (typeof (CompilerGeneratedAttribute), false))
1013 imported = Import.CreateMethod (mb, declaringType);
1014 if (imported.Kind == MemberKind.Method && !imported.IsGeneric) {
1015 if (possible_accessors == null)
1016 possible_accessors = new Dictionary<MethodBase, MethodSpec> (ReferenceEquality<MethodBase>.Default);
1018 // There are no metadata rules for accessors, we have to consider any method as possible candidate
1019 possible_accessors.Add (mb, (MethodSpec) imported);
1023 case MemberTypes.Property:
1024 if (possible_accessors == null)
1027 var p = (PropertyInfo) member;
1029 // Links possible accessors with property
1031 MethodSpec get, set;
1032 m = p.GetGetMethod (true);
1033 if (m == null || !possible_accessors.TryGetValue (m, out get))
1036 m = p.GetSetMethod (true);
1037 if (m == null || !possible_accessors.TryGetValue (m, out set))
1040 // No accessors registered (e.g. explicit implementation)
1041 if (get == null && set == null)
1044 imported = Import.CreateProperty (p, declaringType, get, set);
1045 if (imported == null)
1049 case MemberTypes.Event:
1050 if (possible_accessors == null)
1053 var e = (EventInfo) member;
1055 // Links accessors with event
1057 MethodSpec add, remove;
1058 m = e.GetAddMethod (true);
1059 if (m == null || !possible_accessors.TryGetValue (m, out add))
1062 m = e.GetRemoveMethod (true);
1063 if (m == null || !possible_accessors.TryGetValue (m, out remove))
1066 // Both accessors are required
1067 if (add == null || remove == null)
1070 imported = Import.CreateEvent (e, declaringType, add, remove);
1072 case MemberTypes.Field:
1073 var fi = (FieldInfo) member;
1075 // Ignore compiler generated fields
1076 if (fi.IsPrivate && fi.IsDefined (typeof (CompilerGeneratedAttribute), false))
1079 imported = Import.CreateField (fi, declaringType);
1080 if (imported == null)
1084 case MemberTypes.NestedType:
1085 Type t = (Type) member;
1087 // Ignore compiler generated types, mostly lambda containers
1088 if (t.IsNotPublic && t.IsDefined (typeof (CompilerGeneratedAttribute), false))
1091 imported = Import.CreateType (t, declaringType);
1094 throw new NotImplementedException (member.ToString ());
1097 cache.AddMember (imported);
1100 if (declaringType.IsInterface && declaringType.Interfaces != null) {
1101 foreach (var iface in declaringType.Interfaces) {
1102 cache.AddInterface (iface);
1110 class ImportedTypeParameterDefinition : ImportedMemberDefinition, ITypeDefinition
1112 public ImportedTypeParameterDefinition (Type type)
1119 public string Namespace {
1125 public int TypeParametersCount {
1131 public TypeParameterSpec[] TypeParameters {
1139 public TypeSpec GetAttributeCoClass ()
1144 public string GetAttributeDefaultMember ()
1146 throw new NotSupportedException ();
1149 public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
1151 throw new NotSupportedException ();
1154 public MemberCache LoadMembers (TypeSpec declaringType)
1156 throw new NotImplementedException ();