+
+ public class PredefinedAttributes
+ {
+ // Core types
+ public readonly PredefinedAttribute ParamArray;
+ public readonly PredefinedAttribute Out;
+
+ // Optional types
+ public readonly PredefinedAttribute Obsolete;
+ public readonly PredefinedAttribute DllImport;
+ public readonly PredefinedAttribute MethodImpl;
+ public readonly PredefinedAttribute MarshalAs;
+ public readonly PredefinedAttribute In;
+ public readonly PredefinedAttribute IndexerName;
+ public readonly PredefinedAttribute Conditional;
+ public readonly PredefinedAttribute CLSCompliant;
+ public readonly PredefinedAttribute Security;
+ public readonly PredefinedAttribute Required;
+ public readonly PredefinedAttribute Guid;
+ public readonly PredefinedAttribute AssemblyCulture;
+ public readonly PredefinedAttribute AssemblyVersion;
+ public readonly PredefinedAttribute ComImport;
+ public readonly PredefinedAttribute CoClass;
+ public readonly PredefinedAttribute AttributeUsage;
+ public readonly PredefinedAttribute DefaultParameterValue;
+
+ // New in .NET 2.0
+ public readonly PredefinedAttribute DefaultCharset;
+ public readonly PredefinedAttribute TypeForwarder;
+ public readonly PredefinedAttribute FixedBuffer;
+ public readonly PredefinedAttribute CompilerGenerated;
+ public readonly PredefinedAttribute InternalsVisibleTo;
+ public readonly PredefinedAttribute RuntimeCompatibility;
+ public readonly PredefinedAttribute DebuggerHidden;
+ public readonly PredefinedAttribute UnsafeValueType;
+
+ // New in .NET 3.5
+ public readonly PredefinedAttribute Extension;
+
+ //
+ // Optional types which are used as types and for member lookup
+ //
+ public readonly PredefinedAttribute DefaultMember;
+ public readonly PredefinedAttribute DecimalConstant;
+ public readonly PredefinedAttribute StructLayout;
+ public readonly PredefinedAttribute FieldOffset;
+
+ public static PredefinedAttributes Get = new PredefinedAttributes ();
+
+ private PredefinedAttributes ()
+ {
+ ParamArray = new PredefinedAttribute ("System", "ParamArrayAttribute");
+ Out = new PredefinedAttribute ("System.Runtime.InteropServices", "OutAttribute");
+
+ Obsolete = new PredefinedAttribute ("System", "ObsoleteAttribute");
+ DllImport = new PredefinedAttribute ("System.Runtime.InteropServices", "DllImportAttribute");
+ MethodImpl = new PredefinedAttribute ("System.Runtime.CompilerServices", "MethodImplAttribute");
+ MarshalAs = new PredefinedAttribute ("System.Runtime.InteropServices", "MarshalAsAttribute");
+ In = new PredefinedAttribute ("System.Runtime.InteropServices", "InAttribute");
+ IndexerName = new PredefinedAttribute ("System.Runtime.CompilerServices", "IndexerNameAttribute");
+ Conditional = new PredefinedAttribute ("System.Diagnostics", "ConditionalAttribute");
+ CLSCompliant = new PredefinedAttribute ("System", "CLSCompliantAttribute");
+ Security = new PredefinedAttribute ("System.Security.Permissions", "SecurityAttribute");
+ Required = new PredefinedAttribute ("System.Runtime.CompilerServices", "RequiredAttributeAttribute");
+ Guid = new PredefinedAttribute ("System.Runtime.InteropServices", "GuidAttribute");
+ AssemblyCulture = new PredefinedAttribute ("System.Reflection", "AssemblyCultureAttribute");
+ AssemblyVersion = new PredefinedAttribute ("System.Reflection", "AssemblyVersionAttribute");
+ ComImport = new PredefinedAttribute ("System.Runtime.InteropServices", "ComImportAttribute");
+ CoClass = new PredefinedAttribute ("System.Runtime.InteropServices", "CoClassAttribute");
+ AttributeUsage = new PredefinedAttribute ("System", "AttributeUsageAttribute");
+ DefaultParameterValue = new PredefinedAttribute ("System.Runtime.InteropServices", "DefaultParameterValueAttribute");
+
+ DefaultCharset = new PredefinedAttribute ("System.Runtime.InteropServices", "DefaultCharSetAttribute");
+ TypeForwarder = new PredefinedAttribute ("System.Runtime.CompilerServices", "TypeForwardedToAttribute");
+ FixedBuffer = new PredefinedAttribute ("System.Runtime.CompilerServices", "FixedBufferAttribute");
+ CompilerGenerated = new PredefinedAttribute ("System.Runtime.CompilerServices", "CompilerGeneratedAttribute");
+ InternalsVisibleTo = new PredefinedAttribute ("System.Runtime.CompilerServices", "InternalsVisibleToAttribute");
+ RuntimeCompatibility = new PredefinedAttribute ("System.Runtime.CompilerServices", "RuntimeCompatibilityAttribute");
+ DebuggerHidden = new PredefinedAttribute ("System.Diagnostics", "DebuggerHiddenAttribute");
+ UnsafeValueType = new PredefinedAttribute ("System.Runtime.CompilerServices", "UnsafeValueTypeAttribute");
+
+ Extension = new PredefinedAttribute ("System.Runtime.CompilerServices", "ExtensionAttribute");
+
+ DefaultMember = new PredefinedAttribute ("System.Reflection", "DefaultMemberAttribute");
+ DecimalConstant = new PredefinedAttribute ("System.Runtime.CompilerServices", "DecimalConstantAttribute");
+ StructLayout = new PredefinedAttribute ("System.Runtime.InteropServices", "StructLayoutAttribute");
+ FieldOffset = new PredefinedAttribute ("System.Runtime.InteropServices", "FieldOffsetAttribute");
+ }
+
+ public void Initialize ()
+ {
+ foreach (FieldInfo fi in GetType ().GetFields (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
+ ((PredefinedAttribute) fi.GetValue (this)).Resolve (true);
+ }
+ }
+
+ public static void Reset ()
+ {
+ Get = new PredefinedAttributes ();
+ }
+ }
+
+ public class PredefinedAttribute
+ {
+ Type type;
+ CustomAttributeBuilder cab;
+ ConstructorInfo ctor;
+ readonly string ns, name;
+
+ public PredefinedAttribute (string ns, string name)
+ {
+ this.ns = ns;
+ this.name = name;
+ }
+
+ public static bool operator == (Type type, PredefinedAttribute pa)
+ {
+ return type == pa.type;
+ }
+
+ public static bool operator != (Type type, PredefinedAttribute pa)
+ {
+ return type != pa.type;
+ }
+
+ public ConstructorInfo Constructor {
+ get { return ctor; }
+ }
+
+ public override int GetHashCode ()
+ {
+ return base.GetHashCode ();
+ }
+
+ public override bool Equals (object obj)
+ {
+ throw new NotSupportedException ();
+ }
+
+ public void EmitAttribute (ConstructorBuilder builder)
+ {
+ if (ResolveBuilder ())
+ builder.SetCustomAttribute (cab);
+ }
+
+ public void EmitAttribute (MethodBuilder builder)
+ {
+ if (ResolveBuilder ())
+ builder.SetCustomAttribute (cab);
+ }
+
+ public void EmitAttribute (FieldBuilder builder)
+ {
+ if (ResolveBuilder ())
+ builder.SetCustomAttribute (cab);
+ }
+
+ public void EmitAttribute (TypeBuilder builder)
+ {
+ if (ResolveBuilder ())
+ builder.SetCustomAttribute (cab);
+ }
+
+ public void EmitAttribute (AssemblyBuilder builder)
+ {
+ if (ResolveBuilder ())
+ builder.SetCustomAttribute (cab);
+ }
+
+ public void EmitAttribute (ParameterBuilder builder, Location loc)
+ {
+ if (ResolveBuilder ())
+ builder.SetCustomAttribute (cab);
+ }
+
+ public bool IsDefined {
+ get { return type != null && type != typeof (PredefinedAttribute); }
+ }
+
+ public bool Resolve (bool canFail)
+ {
+ if (type != null) {
+ if (IsDefined)
+ return true;
+ if (canFail)
+ return false;
+ }
+
+ type = TypeManager.CoreLookupType (ns, name, Kind.Class, !canFail);
+ if (type == null) {
+ type = typeof (PredefinedAttribute);
+ return false;
+ }
+
+ return true;
+ }
+
+ bool ResolveBuilder ()
+ {
+ if (cab != null)
+ return true;
+
+ //
+ // Handle all parameter-less attributes as optional
+ //
+ if (!Resolve (true))
+ return false;
+
+ ConstructorInfo ci = TypeManager.GetPredefinedConstructor (type, Location.Null, Type.EmptyTypes);
+ if (ci == null)
+ return false;
+
+ cab = new CustomAttributeBuilder (ci, new object[0]);
+ return true;
+ }
+
+ public bool ResolveConstructor (Location loc, params Type[] argType)
+ {
+ if (ctor != null)
+ throw new InternalErrorException ("Predefined ctor redefined");
+
+ if (!Resolve (false))
+ return false;
+
+ ctor = TypeManager.GetPredefinedConstructor (type, loc, argType);
+ return ctor != null;
+ }
+
+ public Type Type {
+ get { return type; }
+ }
+ }