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 Novell, Inc
12 using System.Reflection;
13 using System.Runtime.CompilerServices;
20 public static FieldSpec CreateField (FieldInfo fi)
22 // TODO: remove after MemberCache fix
23 var cs = TypeManager.GetConstant (fi);
26 var fb = TypeManager.GetFieldCore (fi);
32 var fa = fi.Attributes;
33 switch (fa & FieldAttributes.FieldAccessMask) {
34 case FieldAttributes.Public:
35 mod = Modifiers.PUBLIC;
37 case FieldAttributes.Assembly:
38 mod = Modifiers.INTERNAL;
40 case FieldAttributes.Family:
41 mod = Modifiers.PROTECTED;
43 case FieldAttributes.FamORAssem:
44 mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
47 mod = Modifiers.PRIVATE;
51 // TODO: Remove completely
52 IMemberDetails details;
53 var gfd = TypeManager.GetGenericFieldDefinition (fi);
54 fb = TypeManager.GetFieldCore (gfd);
58 cs = TypeManager.GetConstant (gfd);
60 details = cs.MemberDetails;
62 details = new ImportedMemberDetails (fi);
65 if ((fa & FieldAttributes.Literal) != 0) {
67 if (gfd is System.Reflection.Emit.FieldBuilder) {
68 // TODO: Remove after MemberCache
69 c = TypeManager.GetConstant (gfd).Value;
71 c = Constant.CreateConstantFromValue (fi.FieldType, gfd.GetValue (gfd), Location.Null);
74 return new ConstSpec (details, fi, mod, c);
77 if ((fa & FieldAttributes.InitOnly) != 0) {
78 if (fi.FieldType == TypeManager.decimal_type) {
79 var dc = ReadDecimalConstant (gfd);
81 return new ConstSpec (details, fi, mod, dc);
84 mod |= Modifiers.READONLY;
87 if ((fa & FieldAttributes.Static) != 0)
88 mod |= Modifiers.STATIC;
90 if (!TypeManager.IsReferenceType (fi.FieldType)) {
91 PredefinedAttribute pa = PredefinedAttributes.Get.FixedBuffer;
93 if (gfd is System.Reflection.Emit.FieldBuilder) {
94 // TODO: Remove this after MemberCache fix
95 } else if (gfd.IsDefined (pa.Type, false)) {
96 var element_field = fi.FieldType.GetField (FixedField.FixedElementName);
97 return new FixedFieldSpec (details, fi, element_field, mod);
104 return new FieldSpec (details, fi, mod);
108 // Decimal constants cannot be encoded in the constant blob, and thus are marked
109 // as IsInitOnly ('readonly' in C# parlance). We get its value from the
110 // DecimalConstantAttribute metadata.
112 static Constant ReadDecimalConstant (FieldInfo fi)
114 PredefinedAttribute pa = PredefinedAttributes.Get.DecimalConstant;
118 object[] attrs = fi.GetCustomAttributes (pa.Type, false);
119 if (attrs.Length != 1)
122 return new DecimalConstant (((DecimalConstantAttribute) attrs [0]).Value, Location.Null);
126 class ImportedMemberDetails : IMemberDetails
128 readonly ICustomAttributeProvider provider;
130 public ImportedMemberDetails (ICustomAttributeProvider provider)
132 this.provider = provider;
135 public ObsoleteAttribute GetObsoleteAttribute ()
137 var res = provider.GetCustomAttributes (typeof (ObsoleteAttribute), false);
138 if (res == null || res.Length < 1)
141 return res [0] as ObsoleteAttribute;