| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- #if !FULL_AOT_RUNTIME
- using System;
- using System.IO;
- using System.Collections;
- using System.Reflection;
- using System.Reflection.Emit;
- namespace Mono.CodeGeneration
- {
- public class CodeCustomAttribute
- {
- public static CodeCustomAttribute Create (Type attributeType)
- {
- return Create (attributeType, Type.EmptyTypes, new object [0], new string [0], new object [0]);
- }
- public static CodeCustomAttribute Create (Type attributeType, Type [] ctorArgTypes, object [] ctorArgs, string [] namedArgNames, object [] namedArgValues)
- {
- MemberInfo [] members = new MemberInfo [namedArgNames.Length];
- for (int i = 0; i < namedArgNames.Length; i++) {
- members [i] = attributeType.GetField (namedArgNames [i]);
- if (members [i] == null)
- members [i] = attributeType.GetProperty (namedArgNames [i]);
- if (members [i] == null)
- throw new ArgumentException (String.Format ("Named argument {0} was not found in attribute type {1}", namedArgNames [i], attributeType));
- }
- CodeLiteral [] args = new CodeLiteral [ctorArgs.Length];
- for (int i = 0; i < args.Length; i++)
- args [i] = new CodeLiteral (ctorArgs [i]);
- CodeLiteral [] nargs = new CodeLiteral [namedArgValues.Length];
- for (int i = 0; i < nargs.Length; i++)
- nargs [i] = new CodeLiteral (namedArgValues [i]);
- return Create (attributeType, ctorArgTypes, args, members, nargs);
- }
- public static CodeCustomAttribute Create (Type attributeType, Type [] ctorArgTypes, CodeLiteral [] ctorArgs, MemberInfo [] members, CodeLiteral [] values)
- {
- ArrayList props = new ArrayList ();
- ArrayList pvalues = new ArrayList ();
- ArrayList fields = new ArrayList ();
- ArrayList fvalues = new ArrayList ();
- for (int i = 0; i < members.Length; i++) {
- if (members [i] == null)
- throw new ArgumentException (String.Format ("MemberInfo at {0} was null for type {1}.", i, attributeType));
- if (members [i] is PropertyInfo) {
- props.Add ((PropertyInfo) members [i]);
- pvalues.Add (values [i].Value);
- } else {
- fields.Add ((FieldInfo) members [i]);
- fvalues.Add (values [i].Value);
- }
- }
- ConstructorInfo ci = attributeType.GetConstructor (ctorArgTypes);
- CustomAttributeBuilder cab = new CustomAttributeBuilder (
- ci, ctorArgs,
- (PropertyInfo []) props.ToArray (typeof (PropertyInfo)), pvalues.ToArray (),
- (FieldInfo []) fields.ToArray (typeof (FieldInfo)), fvalues.ToArray ());
- CodeCustomAttribute attr = new CodeCustomAttribute (
- cab, attributeType, ci, ctorArgs, members, values);
- return attr;
- }
- CustomAttributeBuilder customAttributeBuilder;
- Type type;
- ConstructorInfo constructor;
- CodeLiteral [] ctorArgs;
- MemberInfo [] members;
- CodeLiteral [] namedArgValues;
- public CodeCustomAttribute (
- CustomAttributeBuilder attributeBuilder,
- Type type,
- ConstructorInfo constructor,
- CodeLiteral [] ctorArgs,
- MemberInfo [] namedArgMembers,
- CodeLiteral [] namedArgValues)
- {
- this.type = type;
- this.constructor = constructor;
- this.customAttributeBuilder = attributeBuilder;
- this.ctorArgs = ctorArgs;
- this.members = namedArgMembers;
- this.namedArgValues = namedArgValues;
- }
- public CustomAttributeBuilder Builder {
- get { return customAttributeBuilder; }
- }
- public string PrintCode ()
- {
- StringWriter sw = new StringWriter ();
- CodeWriter cw = new CodeWriter (sw);
- PrintCode (cw);
- return sw.ToString ();
- }
-
- public void PrintCode (CodeWriter cw)
- {
- cw.Write ("[").Write (type.Name).Write ("(");
- if (ctorArgs.Length > 0) {
- for (int i = 0; i < ctorArgs.Length - 1; i++) {
- ctorArgs [i].PrintCode (cw);
- cw.Write (", ");
- }
- ctorArgs [ctorArgs.Length - 1].PrintCode (cw);
- }
- if (members.Length > 0) {
- if (ctorArgs.Length > 0)
- cw.Write (", ");
- for (int i = 0; i < members.Length; i++) {
- cw.Write (members [i].Name).Write (" = ");
- namedArgValues [i].PrintCode (cw);
- if (i < members.Length - 1)
- cw.Write (", ");
- }
- }
- cw.Write (" )]");
- cw.EndLine ();
- }
- }
- }
- #endif
|