2 // attribute.cs: Attribute Handler
\r
4 // Author: Ravi Pratap (ravi@ximian.com)
\r
6 // Licensed under the terms of the GNU GPL
\r
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
\r
13 using System.Collections;
\r
14 using System.Reflection;
\r
15 using System.Reflection.Emit;
\r
19 public class Attribute {
\r
21 public readonly string Name;
\r
22 public readonly ArrayList Arguments;
\r
26 public Attribute (string name, ArrayList args, Location loc)
\r
33 public CustomAttributeBuilder Resolve (EmitContext ec)
\r
37 if (Name.IndexOf ("Attribute") == -1)
\r
38 name = Name + "Attribute";
\r
39 else if (Name.LastIndexOf ("Attribute") == 0)
\r
40 name = Name + "Attribute";
\r
42 Type attribute_type = ec.TypeContainer.LookupType (name, false);
\r
44 if (attribute_type == null) {
\r
45 Report.Error (246, Location, "Could not find attribute '" + Name + "' (are you" +
\r
46 " missing a using directive or an assembly reference ?)");
\r
50 // Now we extract the positional and named arguments
\r
52 // FIXME : For now we handle only positional arguments
\r
54 if (Arguments != null)
\r
55 if (Arguments.Count != 1)
\r
56 Console.WriteLine ("Warning : Cannot handle named arguments in attributes yet");
\r
59 ArrayList pos_args = new ArrayList ();
\r
61 if (Arguments != null)
\r
62 pos_args = (ArrayList) Arguments [0];
\r
64 object [] values = new object [pos_args.Count];
\r
66 for (int i = 0; i < pos_args.Count; i++) {
\r
68 Argument a = (Argument) pos_args [i];
\r
70 if (!a.Resolve (ec))
\r
73 Expression e = Expression.Reduce (ec, a.Expr);
\r
76 values [i] = ((Literal) e).GetValue ();
\r
78 Report.Error (182, Location,
\r
79 "An attribute argument must be a constant expression, typeof " +
\r
80 "expression or array creation expression");
\r
85 Expression mg = Expression.MemberLookup (ec, attribute_type, ".ctor", false,
\r
86 MemberTypes.Constructor,
\r
87 BindingFlags.Public | BindingFlags.Instance,
\r
91 Report.Error (-6, Location, "Could not find a constructor for this argument list.");
\r
95 MethodBase constructor = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, pos_args, Location);
\r
97 if (constructor == null) {
\r
98 Report.Error (-6, Location, "Could not find a constructor for this argument list.");
\r
102 CustomAttributeBuilder cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, values);
\r
109 public class AttributeSection {
\r
111 public readonly string Target;
\r
112 public readonly ArrayList Attributes;
\r
114 public AttributeSection (string target, ArrayList attrs)
\r
117 Attributes = attrs;
\r
122 public class Attributes {
\r
124 public ArrayList AttributeSections;
\r
126 public Attributes (AttributeSection a)
\r
128 AttributeSections = new ArrayList ();
\r
129 AttributeSections.Add (a);
\r
133 public void AddAttribute (AttributeSection a)
\r
136 AttributeSections.Add (a);
\r