2 // attribute.cs: Attribute Handler
4 // Author: Ravi Pratap (ravi@ximian.com)
5 // Marek Safar (marek.safar@seznam.cz)
7 // Licensed under the terms of the GNU GPL
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
14 using System.Diagnostics;
15 using System.Collections;
16 using System.Collections.Specialized;
17 using System.Reflection;
18 using System.Reflection.Emit;
19 using System.Runtime.InteropServices;
20 using System.Runtime.CompilerServices;
21 using System.Security;
22 using System.Security.Permissions;
26 namespace Mono.CSharp {
29 /// Base class for objects that can have Attributes applied to them.
31 public abstract class Attributable {
33 /// Attributes for this type
35 protected Attributes attributes;
37 public Attributable (Attributes attrs)
40 OptAttributes = attrs;
43 public Attributes OptAttributes
51 if (attributes != null) {
52 attributes.AttachTo (this);
58 /// Use member-specific procedure to apply attribute @a in @cb to the entity being built in @builder
60 public abstract void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb);
63 /// Returns one AttributeTarget for this element.
65 public abstract AttributeTargets AttributeTargets { get; }
67 public abstract IResolveContext ResolveContext { get; }
69 public abstract bool IsClsComplianceRequired ();
72 /// Gets list of valid attribute targets for explicit target declaration.
73 /// The first array item is default target. Don't break this rule.
75 public abstract string[] ValidAttributeTargets { get; }
78 public class Attribute {
79 public readonly string ExplicitTarget;
80 public AttributeTargets Target;
82 // TODO: remove this member
83 public readonly string Name;
84 public readonly Expression LeftExpr;
85 public readonly string Identifier;
87 readonly ArrayList PosArguments;
88 ArrayList NamedArguments;
90 public readonly Location Location;
95 readonly bool nameEscaped;
97 // It can contain more onwers when the attribute is applied to multiple fiels.
98 protected Attributable[] owners;
100 static readonly AttributeUsageAttribute DefaultUsageAttribute = new AttributeUsageAttribute (AttributeTargets.All);
101 static Assembly orig_sec_assembly;
102 public static readonly object[] EmptyObject = new object [0];
104 // non-null if named args present after Resolve () is called
105 PropertyInfo [] prop_info_arr;
106 FieldInfo [] field_info_arr;
107 object [] field_values_arr;
108 object [] prop_values_arr;
109 object [] pos_values;
111 static PtrHashtable usage_attr_cache;
112 // Cache for parameter-less attributes
113 static PtrHashtable att_cache;
115 public Attribute (string target, Expression left_expr, string identifier, object[] args, Location loc, bool nameEscaped)
117 LeftExpr = left_expr;
118 Identifier = identifier;
119 Name = LeftExpr == null ? identifier : LeftExpr + "." + identifier;
121 PosArguments = (ArrayList)args [0];
122 NamedArguments = (ArrayList)args [1];
125 ExplicitTarget = target;
126 this.nameEscaped = nameEscaped;
134 public static void Reset ()
136 usage_attr_cache = new PtrHashtable ();
137 att_cache = new PtrHashtable ();
140 public virtual void AttachTo (Attributable owner)
142 if (this.owners == null) {
143 this.owners = new Attributable[1] { owner };
147 // When the same attribute is attached to multiple fiels
148 // we use this extra_owners as a list of owners. The attribute
149 // then can be removed because will be emitted when first owner
151 Attributable[] new_array = new Attributable [this.owners.Length + 1];
152 owners.CopyTo (new_array, 0);
153 new_array [owners.Length] = owner;
154 this.owners = new_array;
155 owner.OptAttributes = null;
158 void Error_InvalidNamedArgument (string name)
160 Report.Error (617, Location, "`{0}' is not a valid named attribute argument. Named attribute arguments " +
161 "must be fields which are not readonly, static, const or read-write properties which are " +
162 "public and not static",
166 void Error_InvalidNamedAgrumentType (string name)
168 Report.Error (655, Location, "`{0}' is not a valid named attribute argument because it is not a valid " +
169 "attribute parameter type", name);
172 public static void Error_AttributeArgumentNotValid (Location loc)
174 Report.Error (182, loc,
175 "An attribute argument must be a constant expression, typeof " +
176 "expression or array creation expression");
179 static void Error_TypeParameterInAttribute (Location loc)
182 -202, loc, "Can not use a type parameter in an attribute");
185 public void Error_MissingGuidAttribute ()
187 Report.Error (596, Location, "The Guid attribute must be specified with the ComImport attribute");
190 public void Error_MisusedExtensionAttribute ()
192 Report.Error (1112, Location, "Do not use `{0}' directly. Use parameter modifier `this' instead", GetSignatureForError ());
196 /// This is rather hack. We report many emit attribute error with same error to be compatible with
197 /// csc. But because csc has to report them this way because error came from ilasm we needn't.
199 public void Error_AttributeEmitError (string inner)
201 Report.Error (647, Location, "Error during emitting `{0}' attribute. The reason is `{1}'",
202 TypeManager.CSharpName (Type), inner);
205 public void Error_InvalidSecurityParent ()
207 Error_AttributeEmitError ("it is attached to invalid parent");
216 protected virtual TypeExpr ResolveAsTypeTerminal (Expression expr, IResolveContext ec, bool silent)
218 return expr.ResolveAsTypeTerminal (ec, silent);
221 Type ResolvePossibleAttributeType (string name, bool silent, ref bool is_attr)
223 IResolveContext rc = Owner.ResolveContext;
226 if (LeftExpr == null) {
227 te = ResolveAsTypeTerminal (new SimpleName (name, Location), rc, silent);
229 te = ResolveAsTypeTerminal (new MemberAccess (LeftExpr, name), rc, silent);
236 if (TypeManager.IsSubclassOf (t, TypeManager.attribute_type)) {
238 } else if (!silent) {
239 Report.SymbolRelatedToPreviousError (t);
240 Report.Error (616, Location, "`{0}': is not an attribute class", TypeManager.CSharpName (t));
246 /// Tries to resolve the type of the attribute. Flags an error if it can't, and complain is true.
248 void ResolveAttributeType ()
250 bool t1_is_attr = false;
251 Type t1 = ResolvePossibleAttributeType (Identifier, true, ref t1_is_attr);
253 bool t2_is_attr = false;
254 Type t2 = nameEscaped ? null :
255 ResolvePossibleAttributeType (Identifier + "Attribute", true, ref t2_is_attr);
257 if (t1_is_attr && t2_is_attr) {
258 Report.Error (1614, Location, "`{0}' is ambiguous between `{0}' and `{0}Attribute'. " +
259 "Use either `@{0}' or `{0}Attribute'", GetSignatureForError ());
260 resolve_error = true;
274 if (t1 == null && t2 == null)
275 ResolvePossibleAttributeType (Identifier, false, ref t1_is_attr);
277 ResolvePossibleAttributeType (Identifier, false, ref t1_is_attr);
279 ResolvePossibleAttributeType (Identifier + "Attribute", false, ref t2_is_attr);
281 resolve_error = true;
284 public virtual Type ResolveType ()
286 if (Type == null && !resolve_error)
287 ResolveAttributeType ();
291 public string GetSignatureForError ()
294 return TypeManager.CSharpName (Type);
296 return LeftExpr == null ? Identifier : LeftExpr.GetSignatureForError () + "." + Identifier;
299 bool IsValidArgumentType (Type t)
302 t = t.GetElementType ();
304 return TypeManager.IsPrimitiveType (t) ||
305 TypeManager.IsEnumType (t) ||
306 t == TypeManager.string_type ||
307 t == TypeManager.object_type ||
308 t == TypeManager.type_type;
311 [Conditional ("GMCS_SOURCE")]
312 void ApplyModuleCharSet ()
314 if (Type != TypeManager.dllimport_type)
317 if (!CodeGen.Module.HasDefaultCharSet)
320 const string CharSetEnumMember = "CharSet";
321 if (NamedArguments == null) {
322 NamedArguments = new ArrayList (1);
324 foreach (DictionaryEntry de in NamedArguments) {
325 if ((string)de.Key == CharSetEnumMember)
330 NamedArguments.Add (new DictionaryEntry (CharSetEnumMember,
331 new Argument (Constant.CreateConstant (typeof (CharSet), CodeGen.Module.DefaultCharSet, Location))));
334 public CustomAttributeBuilder Resolve ()
339 resolve_error = true;
342 ResolveAttributeType ();
347 if (Type.IsAbstract) {
348 Report.Error (653, Location, "Cannot apply attribute class `{0}' because it is abstract", GetSignatureForError ());
352 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (Type);
353 if (obsolete_attr != null) {
354 AttributeTester.Report_ObsoleteMessage (obsolete_attr, TypeManager.CSharpName (Type), Location);
357 if (PosArguments == null && NamedArguments == null) {
358 object o = att_cache [Type];
360 resolve_error = false;
361 return (CustomAttributeBuilder)o;
365 Attributable owner = Owner;
366 EmitContext ec = new EmitContext (owner.ResolveContext, owner.ResolveContext.DeclContainer, owner.ResolveContext.DeclContainer,
367 Location, null, null, owner.ResolveContext.DeclContainer.ModFlags, false);
368 ec.IsAnonymousMethodAllowed = false;
370 ConstructorInfo ctor = ResolveConstructor (ec);
372 if (Type is TypeBuilder &&
373 TypeManager.LookupDeclSpace (Type).MemberCache == null)
374 // The attribute type has been DefineType'd, but not Defined. Let's not treat it as an error.
375 // It'll be resolved again when the attached-to entity is emitted.
376 resolve_error = false;
380 ApplyModuleCharSet ();
382 CustomAttributeBuilder cb;
384 if (NamedArguments == null) {
385 cb = new CustomAttributeBuilder (ctor, pos_values);
387 if (pos_values.Length == 0)
388 att_cache.Add (Type, cb);
390 resolve_error = false;
394 if (!ResolveNamedArguments (ec)) {
398 cb = new CustomAttributeBuilder (ctor, pos_values,
399 prop_info_arr, prop_values_arr,
400 field_info_arr, field_values_arr);
402 resolve_error = false;
406 Error_AttributeArgumentNotValid (Location);
411 protected virtual ConstructorInfo ResolveConstructor (EmitContext ec)
413 if (PosArguments != null) {
414 for (int i = 0; i < PosArguments.Count; i++) {
415 Argument a = (Argument) PosArguments [i];
417 if (!a.Resolve (ec, Location))
422 MethodGroupExpr mg = Expression.MemberLookup (ec.ContainerType,
423 Type, ".ctor", MemberTypes.Constructor,
424 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
425 Location) as MethodGroupExpr;
430 mg = mg.OverloadResolve (ec, PosArguments, false, Location);
434 ConstructorInfo constructor = (ConstructorInfo)mg;
436 // TODO: move to OverloadResolve
437 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (constructor);
438 if (oa != null && !Owner.ResolveContext.IsInObsoleteScope) {
439 AttributeTester.Report_ObsoleteMessage (oa, mg.GetSignatureForError (), mg.Location);
442 if (PosArguments == null) {
443 pos_values = EmptyObject;
447 ParameterData pd = TypeManager.GetParameterData (constructor);
449 int pos_arg_count = PosArguments.Count;
450 int last_real_param = pd.Count;
452 pos_values = new object [pos_arg_count];
455 // When the params is not filled we need to put one
456 if (last_real_param > pos_arg_count) {
457 object [] new_pos_values = new object [pos_arg_count + 1];
458 pos_values.CopyTo (new_pos_values, 0);
459 new_pos_values [pos_arg_count] = new object [] {} ;
460 pos_values = new_pos_values;
465 for (int j = 0; j < pos_arg_count; ++j) {
466 Argument a = (Argument) PosArguments [j];
468 if (!a.Expr.GetAttributableValue (a.Type, out pos_values [j]))
471 if (j < last_real_param)
474 if (j == last_real_param) {
475 object [] array = new object [pos_arg_count - last_real_param];
476 array [0] = pos_values [j];
477 pos_values [j] = array;
481 object [] params_array = (object []) pos_values [last_real_param];
482 params_array [j - last_real_param] = pos_values [j];
485 // Adjust the size of the pos_values if it had params
486 if (last_real_param != pos_arg_count) {
487 object [] new_pos_values = new object [last_real_param + 1];
488 Array.Copy (pos_values, new_pos_values, last_real_param + 1);
489 pos_values = new_pos_values;
492 // Here we do the checks which should be done by corlib or by runtime.
493 // However Zoltan doesn't like it and every Mono compiler has to do it again.
495 if (Type == TypeManager.guid_attr_type) {
497 new Guid ((string)pos_values [0]);
499 catch (Exception e) {
500 Error_AttributeEmitError (e.Message);
505 if (Type == TypeManager.attribute_usage_type && (int)pos_values [0] == 0) {
506 Report.Error (591, Location, "Invalid value for argument to `System.AttributeUsage' attribute");
510 if (Type == TypeManager.indexer_name_type || Type == TypeManager.conditional_attribute_type) {
511 if (!Tokenizer.IsValidIdentifier ((string)pos_values [0])) {
512 Report.Error (633, ((Argument)PosArguments[0]).Expr.Location,
513 "The argument to the `{0}' attribute must be a valid identifier", GetSignatureForError ());
518 if (Type == TypeManager.methodimpl_attr_type && pos_values.Length == 1 &&
519 pd.ParameterType (0) == TypeManager.short_type &&
520 !System.Enum.IsDefined (typeof (MethodImplOptions), pos_values [0].ToString ())) {
521 Error_AttributeEmitError ("Incorrect argument value.");
525 return (ConstructorInfo)constructor;
528 protected virtual bool ResolveNamedArguments (EmitContext ec)
530 int named_arg_count = NamedArguments.Count;
532 ArrayList field_infos = new ArrayList (named_arg_count);
533 ArrayList prop_infos = new ArrayList (named_arg_count);
534 ArrayList field_values = new ArrayList (named_arg_count);
535 ArrayList prop_values = new ArrayList (named_arg_count);
537 ArrayList seen_names = new ArrayList(named_arg_count);
539 foreach (DictionaryEntry de in NamedArguments) {
540 string member_name = (string) de.Key;
542 if (seen_names.Contains(member_name)) {
543 Report.Error(643, Location, "'" + member_name + "' duplicate named attribute argument");
546 seen_names.Add(member_name);
548 Argument a = (Argument) de.Value;
549 if (!a.Resolve (ec, Location))
552 Expression member = Expression.MemberLookup (
553 ec.ContainerType, Type, member_name,
554 MemberTypes.Field | MemberTypes.Property,
555 BindingFlags.Public | BindingFlags.Instance,
558 if (member == null) {
559 member = Expression.MemberLookup (ec.ContainerType, Type, member_name,
560 MemberTypes.Field | MemberTypes.Property, BindingFlags.NonPublic | BindingFlags.Instance,
563 if (member != null) {
564 Report.SymbolRelatedToPreviousError (member.Type);
565 Expression.ErrorIsInaccesible (Location, member.GetSignatureForError ());
571 Expression.Error_TypeDoesNotContainDefinition (Location, Type, member_name);
575 if (!(member is PropertyExpr || member is FieldExpr)) {
576 Error_InvalidNamedArgument (member_name);
580 if (a.Expr is TypeParameterExpr){
581 Error_TypeParameterInAttribute (Location);
585 ObsoleteAttribute obsolete_attr;
587 if (member is PropertyExpr) {
588 PropertyInfo pi = ((PropertyExpr) member).PropertyInfo;
590 if (!pi.CanWrite || !pi.CanRead) {
591 Report.SymbolRelatedToPreviousError (pi);
592 Error_InvalidNamedArgument (member_name);
596 if (!IsValidArgumentType (pi.PropertyType)) {
597 Report.SymbolRelatedToPreviousError (pi);
598 Error_InvalidNamedAgrumentType (member_name);
603 if (!a.Expr.GetAttributableValue (pi.PropertyType, out value))
606 PropertyBase pb = TypeManager.GetProperty (pi);
608 obsolete_attr = pb.GetObsoleteAttribute ();
610 obsolete_attr = AttributeTester.GetMemberObsoleteAttribute (pi);
612 prop_values.Add (value);
616 FieldInfo fi = ((FieldExpr) member).FieldInfo;
619 Error_InvalidNamedArgument (member_name);
623 if (!IsValidArgumentType (fi.FieldType)) {
624 Report.SymbolRelatedToPreviousError (fi);
625 Error_InvalidNamedAgrumentType (member_name);
630 if (!a.Expr.GetAttributableValue (fi.FieldType, out value))
633 FieldBase fb = TypeManager.GetField (fi);
635 obsolete_attr = fb.GetObsoleteAttribute ();
637 obsolete_attr = AttributeTester.GetMemberObsoleteAttribute (fi);
639 field_values.Add (value);
640 field_infos.Add (fi);
643 if (obsolete_attr != null && !Owner.ResolveContext.IsInObsoleteScope)
644 AttributeTester.Report_ObsoleteMessage (obsolete_attr, member.GetSignatureForError (), member.Location);
647 prop_info_arr = new PropertyInfo [prop_infos.Count];
648 field_info_arr = new FieldInfo [field_infos.Count];
649 field_values_arr = new object [field_values.Count];
650 prop_values_arr = new object [prop_values.Count];
652 field_infos.CopyTo (field_info_arr, 0);
653 field_values.CopyTo (field_values_arr, 0);
655 prop_values.CopyTo (prop_values_arr, 0);
656 prop_infos.CopyTo (prop_info_arr, 0);
662 /// Get a string containing a list of valid targets for the attribute 'attr'
664 public string GetValidTargets ()
666 StringBuilder sb = new StringBuilder ();
667 AttributeTargets targets = GetAttributeUsage (Type).ValidOn;
669 if ((targets & AttributeTargets.Assembly) != 0)
670 sb.Append ("assembly, ");
672 if ((targets & AttributeTargets.Module) != 0)
673 sb.Append ("module, ");
675 if ((targets & AttributeTargets.Class) != 0)
676 sb.Append ("class, ");
678 if ((targets & AttributeTargets.Struct) != 0)
679 sb.Append ("struct, ");
681 if ((targets & AttributeTargets.Enum) != 0)
682 sb.Append ("enum, ");
684 if ((targets & AttributeTargets.Constructor) != 0)
685 sb.Append ("constructor, ");
687 if ((targets & AttributeTargets.Method) != 0)
688 sb.Append ("method, ");
690 if ((targets & AttributeTargets.Property) != 0)
691 sb.Append ("property, indexer, ");
693 if ((targets & AttributeTargets.Field) != 0)
694 sb.Append ("field, ");
696 if ((targets & AttributeTargets.Event) != 0)
697 sb.Append ("event, ");
699 if ((targets & AttributeTargets.Interface) != 0)
700 sb.Append ("interface, ");
702 if ((targets & AttributeTargets.Parameter) != 0)
703 sb.Append ("parameter, ");
705 if ((targets & AttributeTargets.Delegate) != 0)
706 sb.Append ("delegate, ");
708 if ((targets & AttributeTargets.ReturnValue) != 0)
709 sb.Append ("return, ");
712 if ((targets & AttributeTargets.GenericParameter) != 0)
713 sb.Append ("type parameter, ");
715 return sb.Remove (sb.Length - 2, 2).ToString ();
719 /// Returns AttributeUsage attribute based on types hierarchy
721 static AttributeUsageAttribute GetAttributeUsage (Type type)
723 AttributeUsageAttribute ua = usage_attr_cache [type] as AttributeUsageAttribute;
727 Class attr_class = TypeManager.LookupClass (type);
729 if (attr_class == null) {
730 object[] usage_attr = type.GetCustomAttributes (TypeManager.attribute_usage_type, true);
731 ua = (AttributeUsageAttribute)usage_attr [0];
732 usage_attr_cache.Add (type, ua);
737 if (attr_class.OptAttributes != null)
738 a = attr_class.OptAttributes.Search (TypeManager.attribute_usage_type);
741 if (attr_class.TypeBuilder.BaseType != TypeManager.attribute_type)
742 ua = GetAttributeUsage (attr_class.TypeBuilder.BaseType);
744 ua = DefaultUsageAttribute;
746 ua = a.GetAttributeUsageAttribute ();
749 usage_attr_cache.Add (type, ua);
753 AttributeUsageAttribute GetAttributeUsageAttribute ()
755 if (pos_values == null)
756 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
757 // But because a lot of attribute class code must be rewritten will be better to wait...
761 return DefaultUsageAttribute;
763 AttributeUsageAttribute usage_attribute = new AttributeUsageAttribute ((AttributeTargets)pos_values [0]);
765 object field = GetPropertyValue ("AllowMultiple");
767 usage_attribute.AllowMultiple = (bool)field;
769 field = GetPropertyValue ("Inherited");
771 usage_attribute.Inherited = (bool)field;
773 return usage_attribute;
777 /// Returns custom name of indexer
779 public string GetIndexerAttributeValue ()
781 if (pos_values == null)
782 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
783 // But because a lot of attribute class code must be rewritten will be better to wait...
789 return pos_values [0] as string;
793 /// Returns condition of ConditionalAttribute
795 public string GetConditionalAttributeValue ()
797 if (pos_values == null)
798 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
799 // But because a lot of attribute class code must be rewritten will be better to wait...
805 return (string)pos_values [0];
809 /// Creates the instance of ObsoleteAttribute from this attribute instance
811 public ObsoleteAttribute GetObsoleteAttribute ()
813 if (pos_values == null)
814 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
815 // But because a lot of attribute class code must be rewritten will be better to wait...
821 if (pos_values == null || pos_values.Length == 0)
822 return new ObsoleteAttribute ();
824 if (pos_values.Length == 1)
825 return new ObsoleteAttribute ((string)pos_values [0]);
827 return new ObsoleteAttribute ((string)pos_values [0], (bool)pos_values [1]);
831 /// Returns value of CLSCompliantAttribute contructor parameter but because the method can be called
832 /// before ApplyAttribute. We need to resolve the arguments.
833 /// This situation occurs when class deps is differs from Emit order.
835 public bool GetClsCompliantAttributeValue ()
837 if (pos_values == null)
838 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
839 // But because a lot of attribute class code must be rewritten will be better to wait...
845 return (bool)pos_values [0];
848 public Type GetCoClassAttributeValue ()
850 if (pos_values == null)
856 return (Type)pos_values [0];
859 public bool CheckTarget ()
861 string[] valid_targets = Owner.ValidAttributeTargets;
862 if (ExplicitTarget == null || ExplicitTarget == valid_targets [0]) {
863 Target = Owner.AttributeTargets;
867 // TODO: we can skip the first item
868 if (((IList) valid_targets).Contains (ExplicitTarget)) {
869 switch (ExplicitTarget) {
870 case "return": Target = AttributeTargets.ReturnValue; return true;
871 case "param": Target = AttributeTargets.Parameter; return true;
872 case "field": Target = AttributeTargets.Field; return true;
873 case "method": Target = AttributeTargets.Method; return true;
874 case "property": Target = AttributeTargets.Property; return true;
876 throw new InternalErrorException ("Unknown explicit target: " + ExplicitTarget);
879 StringBuilder sb = new StringBuilder ();
880 foreach (string s in valid_targets) {
884 sb.Remove (sb.Length - 2, 2);
885 Report.Error (657, Location, "`{0}' is not a valid attribute location for this declaration. " +
886 "Valid attribute locations for this declaration are `{1}'", ExplicitTarget, sb.ToString ());
891 /// Tests permitted SecurityAction for assembly or other types
893 public bool CheckSecurityActionValidity (bool for_assembly)
895 SecurityAction action = GetSecurityActionValue ();
898 case SecurityAction.Demand:
899 case SecurityAction.Assert:
900 case SecurityAction.Deny:
901 case SecurityAction.PermitOnly:
902 case SecurityAction.LinkDemand:
903 case SecurityAction.InheritanceDemand:
908 case SecurityAction.RequestMinimum:
909 case SecurityAction.RequestOptional:
910 case SecurityAction.RequestRefuse:
916 Error_AttributeEmitError ("SecurityAction is out of range");
920 Error_AttributeEmitError (String.Concat ("SecurityAction `", action, "' is not valid for this declaration"));
924 System.Security.Permissions.SecurityAction GetSecurityActionValue ()
926 return (SecurityAction)pos_values [0];
930 /// Creates instance of SecurityAttribute class and add result of CreatePermission method to permission table.
932 /// <returns></returns>
933 public void ExtractSecurityPermissionSet (ListDictionary permissions)
935 Type orig_assembly_type = null;
937 if (TypeManager.LookupDeclSpace (Type) != null) {
938 if (!RootContext.StdLib) {
939 orig_assembly_type = Type.GetType (Type.FullName);
941 string orig_version_path = Environment.GetEnvironmentVariable ("__SECURITY_BOOTSTRAP_DB");
942 if (orig_version_path == null) {
943 Error_AttributeEmitError ("security custom attributes can not be referenced from defining assembly");
947 if (orig_sec_assembly == null) {
948 string file = Path.Combine (orig_version_path, Driver.OutputFile);
949 orig_sec_assembly = Assembly.LoadFile (file);
952 orig_assembly_type = orig_sec_assembly.GetType (Type.FullName, true);
953 if (orig_assembly_type == null) {
954 Report.Warning (-112, 1, Location, "Self-referenced security attribute `{0}' " +
955 "was not found in previous version of assembly");
961 SecurityAttribute sa;
962 // For all non-selfreferencing security attributes we can avoid all hacks
963 if (orig_assembly_type == null) {
964 sa = (SecurityAttribute) Activator.CreateInstance (Type, pos_values);
966 if (prop_info_arr != null) {
967 for (int i = 0; i < prop_info_arr.Length; ++i) {
968 PropertyInfo pi = prop_info_arr [i];
969 pi.SetValue (sa, prop_values_arr [i], null);
973 // HACK: All security attributes have same ctor syntax
974 sa = (SecurityAttribute) Activator.CreateInstance (orig_assembly_type, new object[] { GetSecurityActionValue () } );
976 // All types are from newly created assembly but for invocation with old one we need to convert them
977 if (prop_info_arr != null) {
978 for (int i = 0; i < prop_info_arr.Length; ++i) {
979 PropertyInfo emited_pi = prop_info_arr [i];
980 PropertyInfo pi = orig_assembly_type.GetProperty (emited_pi.Name, emited_pi.PropertyType);
982 object old_instance = pi.PropertyType.IsEnum ?
983 System.Enum.ToObject (pi.PropertyType, prop_values_arr [i]) :
986 pi.SetValue (sa, old_instance, null);
992 perm = sa.CreatePermission ();
993 SecurityAction action = GetSecurityActionValue ();
995 // IS is correct because for corlib we are using an instance from old corlib
996 if (!(perm is System.Security.CodeAccessPermission)) {
998 case SecurityAction.Demand:
999 action = (SecurityAction)13;
1001 case SecurityAction.LinkDemand:
1002 action = (SecurityAction)14;
1004 case SecurityAction.InheritanceDemand:
1005 action = (SecurityAction)15;
1010 PermissionSet ps = (PermissionSet)permissions [action];
1012 if (sa is PermissionSetAttribute)
1013 ps = new PermissionSet (sa.Unrestricted ? PermissionState.Unrestricted : PermissionState.None);
1015 ps = new PermissionSet (PermissionState.None);
1017 permissions.Add (action, ps);
1018 } else if (!ps.IsUnrestricted () && (sa is PermissionSetAttribute) && sa.Unrestricted) {
1019 ps = ps.Union (new PermissionSet (PermissionState.Unrestricted));
1020 permissions [action] = ps;
1022 ps.AddPermission (perm);
1025 static object GetValue (object value)
1027 if (value is EnumConstant)
1028 return ((EnumConstant) value).GetValue ();
1033 public object GetPropertyValue (string name)
1035 if (prop_info_arr == null)
1038 for (int i = 0; i < prop_info_arr.Length; ++i) {
1039 if (prop_info_arr [i].Name == name)
1040 return prop_values_arr [i];
1046 object GetFieldValue (string name)
1049 if (field_info_arr == null)
1052 foreach (FieldInfo fi in field_info_arr) {
1053 if (fi.Name == name)
1054 return GetValue (field_values_arr [i]);
1061 // Theoretically, we can get rid of this, since FieldBuilder.SetCustomAttribute()
1062 // and ParameterBuilder.SetCustomAttribute() are supposed to handle this attribute.
1063 // However, we can't, since it appears that the .NET 1.1 SRE hangs when given a MarshalAsAttribute.
1065 public UnmanagedMarshal GetMarshal (Attributable attr)
1067 UnmanagedType UnmanagedType;
1068 if (!RootContext.StdLib || pos_values [0].GetType () != typeof (UnmanagedType))
1069 UnmanagedType = (UnmanagedType) System.Enum.ToObject (typeof (UnmanagedType), pos_values [0]);
1071 UnmanagedType = (UnmanagedType) pos_values [0];
1073 object value = GetFieldValue ("SizeParamIndex");
1074 if (value != null && UnmanagedType != UnmanagedType.LPArray) {
1075 Error_AttributeEmitError ("SizeParamIndex field is not valid for the specified unmanaged type");
1079 object o = GetFieldValue ("ArraySubType");
1080 UnmanagedType array_sub_type = o == null ? (UnmanagedType) 0x50 /* NATIVE_MAX */ : (UnmanagedType) o;
1082 switch (UnmanagedType) {
1083 case UnmanagedType.CustomMarshaler: {
1084 MethodInfo define_custom = typeof (UnmanagedMarshal).GetMethod ("DefineCustom",
1085 BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1086 if (define_custom == null) {
1087 Report.RuntimeMissingSupport (Location, "set marshal info");
1091 object [] args = new object [4];
1092 args [0] = GetFieldValue ("MarshalTypeRef");
1093 args [1] = GetFieldValue ("MarshalCookie");
1094 args [2] = GetFieldValue ("MarshalType");
1095 args [3] = Guid.Empty;
1096 return (UnmanagedMarshal) define_custom.Invoke (null, args);
1098 case UnmanagedType.LPArray: {
1099 object size_const = GetFieldValue ("SizeConst");
1100 object size_param_index = GetFieldValue ("SizeParamIndex");
1102 if ((size_const != null) || (size_param_index != null)) {
1103 MethodInfo define_array = typeof (UnmanagedMarshal).GetMethod ("DefineLPArrayInternal",
1104 BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1105 if (define_array == null) {
1106 Report.RuntimeMissingSupport (Location, "set marshal info");
1110 object [] args = new object [3];
1111 args [0] = array_sub_type;
1112 args [1] = size_const == null ? -1 : size_const;
1113 args [2] = size_param_index == null ? -1 : size_param_index;
1114 return (UnmanagedMarshal) define_array.Invoke (null, args);
1117 return UnmanagedMarshal.DefineLPArray (array_sub_type);
1119 case UnmanagedType.SafeArray:
1120 return UnmanagedMarshal.DefineSafeArray (array_sub_type);
1122 case UnmanagedType.ByValArray:
1123 FieldBase fm = attr as FieldBase;
1125 Error_AttributeEmitError ("Specified unmanaged type is only valid on fields");
1128 return UnmanagedMarshal.DefineByValArray ((int) GetFieldValue ("SizeConst"));
1130 case UnmanagedType.ByValTStr:
1131 return UnmanagedMarshal.DefineByValTStr ((int) GetFieldValue ("SizeConst"));
1134 return UnmanagedMarshal.DefineUnmanagedMarshal (UnmanagedType);
1138 public CharSet GetCharSetValue ()
1140 return (CharSet)System.Enum.Parse (typeof (CharSet), pos_values [0].ToString ());
1143 public bool IsInternalMethodImplAttribute {
1145 if (Type != TypeManager.methodimpl_attr_type)
1148 MethodImplOptions options;
1149 if (pos_values[0].GetType () != typeof (MethodImplOptions))
1150 options = (MethodImplOptions)System.Enum.ToObject (typeof (MethodImplOptions), pos_values[0]);
1152 options = (MethodImplOptions)pos_values[0];
1154 return (options & MethodImplOptions.InternalCall) != 0;
1158 public LayoutKind GetLayoutKindValue ()
1160 if (!RootContext.StdLib || pos_values [0].GetType () != typeof (LayoutKind))
1161 return (LayoutKind)System.Enum.ToObject (typeof (LayoutKind), pos_values [0]);
1163 return (LayoutKind)pos_values [0];
1166 public object GetParameterDefaultValue ()
1168 return pos_values [0];
1171 public override bool Equals (object obj)
1173 Attribute a = obj as Attribute;
1177 return Type == a.Type && Target == a.Target;
1180 public override int GetHashCode ()
1182 return base.GetHashCode ();
1186 /// Emit attribute for Attributable symbol
1188 public void Emit (ListDictionary allEmitted)
1190 CustomAttributeBuilder cb = Resolve ();
1194 AttributeUsageAttribute usage_attr = GetAttributeUsage (Type);
1195 if ((usage_attr.ValidOn & Target) == 0) {
1196 Report.Error (592, Location, "The attribute `{0}' is not valid on this declaration type. " +
1197 "It is valid on `{1}' declarations only",
1198 GetSignatureForError (), GetValidTargets ());
1203 foreach (Attributable owner in owners)
1204 owner.ApplyAttributeBuilder (this, cb);
1206 catch (Exception e) {
1207 Error_AttributeEmitError (e.Message);
1211 if (!usage_attr.AllowMultiple && allEmitted != null) {
1212 if (allEmitted.Contains (this)) {
1213 ArrayList a = allEmitted [this] as ArrayList;
1215 a = new ArrayList (2);
1216 allEmitted [this] = a;
1220 allEmitted.Add (this, null);
1224 if (!RootContext.VerifyClsCompliance)
1227 // Here we are testing attribute arguments for array usage (error 3016)
1228 if (Owner.IsClsComplianceRequired ()) {
1229 if (PosArguments != null) {
1230 foreach (Argument arg in PosArguments) {
1231 // Type is undefined (was error 246)
1232 if (arg.Type == null)
1235 if (arg.Type.IsArray) {
1236 Report.Error (3016, Location, "Arrays as attribute arguments are not CLS-compliant");
1242 if (NamedArguments == null)
1245 foreach (DictionaryEntry de in NamedArguments) {
1246 Argument arg = (Argument) de.Value;
1248 // Type is undefined (was error 246)
1249 if (arg.Type == null)
1252 if (arg.Type.IsArray) {
1253 Report.Error (3016, Location, "Arrays as attribute arguments are not CLS-compliant");
1260 private Expression GetValue ()
1262 if (PosArguments == null || PosArguments.Count < 1)
1265 return ((Argument) PosArguments [0]).Expr;
1268 public string GetString ()
1270 Expression e = GetValue ();
1271 if (e is StringLiteral)
1272 return (e as StringLiteral).Value;
1276 public bool GetBoolean ()
1278 Expression e = GetValue ();
1279 if (e is BoolLiteral)
1280 return (e as BoolLiteral).Value;
1284 public Type GetArgumentType ()
1286 TypeOf e = GetValue () as TypeOf;
1289 return e.TypeArgument;
1295 /// For global attributes (assembly, module) we need special handling.
1296 /// Attributes can be located in the several files
1298 public class GlobalAttribute : Attribute
1300 public readonly NamespaceEntry ns;
1302 public GlobalAttribute (NamespaceEntry ns, string target,
1303 Expression left_expr, string identifier, object[] args, Location loc, bool nameEscaped):
1304 base (target, left_expr, identifier, args, loc, nameEscaped)
1307 this.owners = new Attributable[1];
1310 public override void AttachTo (Attributable owner)
1312 if (ExplicitTarget == "assembly") {
1313 owners [0] = CodeGen.Assembly;
1316 if (ExplicitTarget == "module") {
1317 owners [0] = CodeGen.Module;
1320 throw new NotImplementedException ("Unknown global explicit target " + ExplicitTarget);
1325 // RootContext.ToplevelTypes has a single NamespaceEntry which gets overwritten
1326 // each time a new file is parsed. However, we need to use the NamespaceEntry
1327 // in effect where the attribute was used. Since code elsewhere cannot assume
1328 // that the NamespaceEntry is right, just overwrite it.
1330 // Precondition: RootContext.ToplevelTypes == null
1332 if (RootContext.ToplevelTypes.NamespaceEntry != null)
1333 throw new InternalErrorException (Location + " non-null NamespaceEntry");
1335 RootContext.ToplevelTypes.NamespaceEntry = ns;
1340 RootContext.ToplevelTypes.NamespaceEntry = null;
1343 protected override TypeExpr ResolveAsTypeTerminal (Expression expr, IResolveContext ec, bool silent)
1347 return base.ResolveAsTypeTerminal (expr, ec, silent);
1354 protected override ConstructorInfo ResolveConstructor (EmitContext ec)
1358 return base.ResolveConstructor (ec);
1365 protected override bool ResolveNamedArguments (EmitContext ec)
1369 return base.ResolveNamedArguments (ec);
1377 public class Attributes {
1378 public readonly ArrayList Attrs;
1380 public Attributes (Attribute a)
1382 Attrs = new ArrayList ();
1386 public Attributes (ArrayList attrs)
1391 public void AddAttributes (ArrayList attrs)
1393 Attrs.AddRange (attrs);
1396 public void AttachTo (Attributable attributable)
1398 foreach (Attribute a in Attrs)
1399 a.AttachTo (attributable);
1403 /// Checks whether attribute target is valid for the current element
1405 public bool CheckTargets ()
1407 foreach (Attribute a in Attrs) {
1408 if (!a.CheckTarget ())
1414 public Attribute Search (Type t)
1416 foreach (Attribute a in Attrs) {
1417 if (a.ResolveType () == t)
1424 /// Returns all attributes of type 't'. Use it when attribute is AllowMultiple = true
1426 public Attribute[] SearchMulti (Type t)
1428 ArrayList ar = null;
1430 foreach (Attribute a in Attrs) {
1431 if (a.ResolveType () == t) {
1433 ar = new ArrayList ();
1438 return ar == null ? null : ar.ToArray (typeof (Attribute)) as Attribute[];
1445 ListDictionary ld = Attrs.Count > 1 ? new ListDictionary () : null;
1447 foreach (Attribute a in Attrs)
1450 if (ld == null || ld.Count == 0)
1453 foreach (DictionaryEntry d in ld) {
1454 if (d.Value == null)
1457 foreach (Attribute collision in (ArrayList)d.Value)
1458 Report.SymbolRelatedToPreviousError (collision.Location, "");
1460 Attribute a = (Attribute)d.Key;
1461 Report.Error (579, a.Location, "The attribute `{0}' cannot be applied multiple times",
1462 a.GetSignatureForError ());
1466 public bool Contains (Type t)
1468 return Search (t) != null;
1473 /// Helper class for attribute verification routine.
1475 sealed class AttributeTester
1477 static PtrHashtable analyzed_types;
1478 static PtrHashtable analyzed_types_obsolete;
1479 static PtrHashtable analyzed_member_obsolete;
1480 static PtrHashtable analyzed_method_excluded;
1483 static PtrHashtable fixed_buffer_cache;
1486 static object TRUE = new object ();
1487 static object FALSE = new object ();
1489 static AttributeTester ()
1494 private AttributeTester ()
1498 public static void Reset ()
1500 analyzed_types = new PtrHashtable ();
1501 analyzed_types_obsolete = new PtrHashtable ();
1502 analyzed_member_obsolete = new PtrHashtable ();
1503 analyzed_method_excluded = new PtrHashtable ();
1505 fixed_buffer_cache = new PtrHashtable ();
1509 public enum Result {
1516 /// Returns true if parameters of two compared methods are CLS-Compliant.
1517 /// It tests differing only in ref or out, or in array rank.
1519 public static Result AreOverloadedMethodParamsClsCompliant (Type[] types_a, Type[] types_b)
1521 if (types_a == null || types_b == null)
1524 if (types_a.Length != types_b.Length)
1527 Result result = Result.Ok;
1528 for (int i = 0; i < types_b.Length; ++i) {
1529 Type aType = types_a [i];
1530 Type bType = types_b [i];
1532 if (aType.IsArray && bType.IsArray) {
1533 Type a_el_type = aType.GetElementType ();
1534 Type b_el_type = bType.GetElementType ();
1535 if (aType.GetArrayRank () != bType.GetArrayRank () && a_el_type == b_el_type) {
1536 result = Result.RefOutArrayError;
1540 if (a_el_type.IsArray || b_el_type.IsArray) {
1541 result = Result.ArrayArrayError;
1546 Type aBaseType = aType;
1547 bool is_either_ref_or_out = false;
1549 if (aType.IsByRef || aType.IsPointer) {
1550 aBaseType = aType.GetElementType ();
1551 is_either_ref_or_out = true;
1554 Type bBaseType = bType;
1555 if (bType.IsByRef || bType.IsPointer)
1557 bBaseType = bType.GetElementType ();
1558 is_either_ref_or_out = !is_either_ref_or_out;
1561 if (aBaseType != bBaseType)
1564 if (is_either_ref_or_out)
1565 result = Result.RefOutArrayError;
1571 /// This method tests the CLS compliance of external types. It doesn't test type visibility.
1573 public static bool IsClsCompliant (Type type)
1578 object type_compliance = analyzed_types[type];
1579 if (type_compliance != null)
1580 return type_compliance == TRUE;
1582 if (type.IsPointer) {
1583 analyzed_types.Add (type, null);
1588 if (type.IsArray || type.IsByRef) {
1589 result = IsClsCompliant (TypeManager.GetElementType (type));
1591 result = AnalyzeTypeCompliance (type);
1593 analyzed_types.Add (type, result ? TRUE : FALSE);
1598 /// Returns IFixedBuffer implementation if field is fixed buffer else null.
1600 public static IFixedBuffer GetFixedBuffer (FieldInfo fi)
1602 // Fixed buffer helper type is generated as value type
1603 if (!fi.FieldType.IsValueType)
1606 FieldBase fb = TypeManager.GetField (fi);
1608 return fb as IFixedBuffer;
1612 object o = fixed_buffer_cache [fi];
1614 if (System.Attribute.GetCustomAttribute (fi, TypeManager.fixed_buffer_attr_type) == null) {
1615 fixed_buffer_cache.Add (fi, FALSE);
1619 IFixedBuffer iff = new FixedFieldExternal (fi);
1620 fixed_buffer_cache.Add (fi, iff);
1627 return (IFixedBuffer)o;
1633 public static void VerifyModulesClsCompliance ()
1635 Module[] modules = RootNamespace.Global.Modules;
1636 if (modules == null)
1639 // The first module is generated assembly
1640 for (int i = 1; i < modules.Length; ++i) {
1641 Module module = modules [i];
1642 if (!IsClsCompliant (module)) {
1643 Report.Error (3013, "Added modules must be marked with the CLSCompliant attribute " +
1644 "to match the assembly", module.Name);
1650 public static Type GetImportedIgnoreCaseClsType (string name)
1652 foreach (Assembly a in RootNamespace.Global.Assemblies) {
1653 Type t = a.GetType (name, false, true);
1657 if (IsClsCompliant (t))
1663 static bool IsClsCompliant (ICustomAttributeProvider attribute_provider)
1665 object[] CompliantAttribute = attribute_provider.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
1666 if (CompliantAttribute.Length == 0)
1669 return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
1672 static bool AnalyzeTypeCompliance (Type type)
1674 type = TypeManager.DropGenericTypeArguments (type);
1675 DeclSpace ds = TypeManager.LookupDeclSpace (type);
1677 return ds.IsClsComplianceRequired ();
1680 if (TypeManager.IsGenericParameter (type))
1683 object[] CompliantAttribute = type.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
1684 if (CompliantAttribute.Length == 0)
1685 return IsClsCompliant (type.Assembly);
1687 return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
1690 // Registers the core type as we assume that they will never be obsolete which
1691 // makes things easier for bootstrap and faster (we don't need to query Obsolete attribute).
1692 public static void RegisterNonObsoleteType (Type type)
1694 analyzed_types_obsolete [type] = FALSE;
1698 /// Returns instance of ObsoleteAttribute when type is obsolete
1700 public static ObsoleteAttribute GetObsoleteAttribute (Type type)
1702 object type_obsolete = analyzed_types_obsolete [type];
1703 if (type_obsolete == FALSE)
1706 if (type_obsolete != null)
1707 return (ObsoleteAttribute)type_obsolete;
1709 ObsoleteAttribute result = null;
1710 if (TypeManager.HasElementType (type)) {
1711 result = GetObsoleteAttribute (TypeManager.GetElementType (type));
1712 } else if (TypeManager.IsGenericParameter (type) || TypeManager.IsGenericType (type))
1715 DeclSpace type_ds = TypeManager.LookupDeclSpace (type);
1717 // Type is external, we can get attribute directly
1718 if (type_ds == null) {
1719 object[] attribute = type.GetCustomAttributes (TypeManager.obsolete_attribute_type, false);
1720 if (attribute.Length == 1)
1721 result = (ObsoleteAttribute)attribute [0];
1723 result = type_ds.GetObsoleteAttribute ();
1727 // Cannot use .Add because of corlib bootstrap
1728 analyzed_types_obsolete [type] = result == null ? FALSE : result;
1733 /// Returns instance of ObsoleteAttribute when method is obsolete
1735 public static ObsoleteAttribute GetMethodObsoleteAttribute (MethodBase mb)
1737 IMethodData mc = TypeManager.GetMethod (mb);
1739 return mc.GetObsoleteAttribute ();
1741 // compiler generated methods are not registered by AddMethod
1742 if (mb.DeclaringType is TypeBuilder)
1745 MemberInfo mi = TypeManager.GetPropertyFromAccessor (mb);
1747 return GetMemberObsoleteAttribute (mi);
1749 mi = TypeManager.GetEventFromAccessor (mb);
1751 return GetMemberObsoleteAttribute (mi);
1753 return GetMemberObsoleteAttribute (mb);
1757 /// Returns instance of ObsoleteAttribute when member is obsolete
1759 public static ObsoleteAttribute GetMemberObsoleteAttribute (MemberInfo mi)
1761 object type_obsolete = analyzed_member_obsolete [mi];
1762 if (type_obsolete == FALSE)
1765 if (type_obsolete != null)
1766 return (ObsoleteAttribute)type_obsolete;
1768 if ((mi.DeclaringType is TypeBuilder) || TypeManager.IsGenericType (mi.DeclaringType))
1771 ObsoleteAttribute oa = System.Attribute.GetCustomAttribute (mi, TypeManager.obsolete_attribute_type, false)
1772 as ObsoleteAttribute;
1773 analyzed_member_obsolete.Add (mi, oa == null ? FALSE : oa);
1778 /// Common method for Obsolete error/warning reporting.
1780 public static void Report_ObsoleteMessage (ObsoleteAttribute oa, string member, Location loc)
1783 Report.Error (619, loc, "`{0}' is obsolete: `{1}'", member, oa.Message);
1787 if (oa.Message == null) {
1788 Report.Warning (612, 1, loc, "`{0}' is obsolete", member);
1791 Report.Warning (618, 2, loc, "`{0}' is obsolete: `{1}'", member, oa.Message);
1794 public static bool IsConditionalMethodExcluded (MethodBase mb)
1796 mb = TypeManager.DropGenericMethodArguments (mb);
1797 // TODO: Has to be fixed for partial methods
1798 if ((mb is MethodBuilder) || (mb is ConstructorBuilder))
1801 object excluded = analyzed_method_excluded [mb];
1802 if (excluded != null)
1803 return excluded == TRUE ? true : false;
1805 ConditionalAttribute[] attrs = mb.GetCustomAttributes (TypeManager.conditional_attribute_type, true)
1806 as ConditionalAttribute[];
1807 if (attrs.Length == 0) {
1808 analyzed_method_excluded.Add (mb, FALSE);
1812 foreach (ConditionalAttribute a in attrs) {
1813 if (RootContext.AllDefines.Contains (a.ConditionString)) {
1814 analyzed_method_excluded.Add (mb, FALSE);
1818 analyzed_method_excluded.Add (mb, TRUE);
1823 /// Analyzes class whether it has attribute which has ConditionalAttribute
1824 /// and its condition is not defined.
1826 public static bool IsAttributeExcluded (Type type)
1831 Class class_decl = TypeManager.LookupDeclSpace (type) as Class;
1833 // TODO: add caching
1834 // TODO: merge all Type bases attribute caching to one cache to save memory
1835 if (class_decl == null) {
1836 object[] attributes = type.GetCustomAttributes (TypeManager.conditional_attribute_type, false);
1837 foreach (ConditionalAttribute ca in attributes) {
1838 if (RootContext.AllDefines.Contains (ca.ConditionString))
1841 return attributes.Length > 0;
1844 return class_decl.IsExcluded ();
1847 public static Type GetCoClassAttribute (Type type)
1849 TypeContainer tc = TypeManager.LookupInterface (type);
1851 object[] o = type.GetCustomAttributes (TypeManager.coclass_attr_type, false);
1854 return ((System.Runtime.InteropServices.CoClassAttribute)o[0]).CoClass;
1857 if (tc.OptAttributes == null)
1860 Attribute a = tc.OptAttributes.Search (TypeManager.coclass_attr_type);
1864 return a.GetCoClassAttributeValue ();