| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- //
- // System.Reflection/CustomAttributeData.cs
- //
- // Author:
- // Zoltan Varga ([email protected])
- // Carlos Alberto Cortez ([email protected])
- //
- // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- //
- // 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.
- //
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace System.Reflection {
- [ComVisible (true)]
- [Serializable]
- #if NET_4_0
- public
- #else
- public sealed
- #endif
- class CustomAttributeData {
- ConstructorInfo ctorInfo;
- IList<CustomAttributeTypedArgument> ctorArgs;
- IList<CustomAttributeNamedArgument> namedArgs;
- #if NET_4_0
- protected CustomAttributeData ()
- {
- }
- #endif
- internal CustomAttributeData (ConstructorInfo ctorInfo, object [] ctorArgs, object [] namedArgs)
- {
- this.ctorInfo = ctorInfo;
-
- this.ctorArgs = Array.AsReadOnly<CustomAttributeTypedArgument>
- (ctorArgs != null ? UnboxValues<CustomAttributeTypedArgument> (ctorArgs) : new CustomAttributeTypedArgument [0]);
-
- this.namedArgs = Array.AsReadOnly<CustomAttributeNamedArgument>
- (namedArgs != null ? UnboxValues<CustomAttributeNamedArgument> (namedArgs) : new CustomAttributeNamedArgument [0]);
- }
- [ComVisible (true)]
- public
- #if NET_4_0
- virtual
- #endif
- ConstructorInfo Constructor {
- get {
- return ctorInfo;
- }
- }
- [ComVisible (true)]
- public
- #if NET_4_0
- virtual
- #endif
- IList<CustomAttributeTypedArgument> ConstructorArguments {
- get {
- return ctorArgs;
- }
- }
- public
- #if NET_4_0
- virtual
- #endif
- IList<CustomAttributeNamedArgument> NamedArguments {
- get {
- return namedArgs;
- }
- }
- public static IList<CustomAttributeData> GetCustomAttributes (Assembly target) {
- return MonoCustomAttrs.GetCustomAttributesData (target);
- }
- public static IList<CustomAttributeData> GetCustomAttributes (MemberInfo target) {
- return MonoCustomAttrs.GetCustomAttributesData (target);
- }
- public static IList<CustomAttributeData> GetCustomAttributes (Module target) {
- return MonoCustomAttrs.GetCustomAttributesData (target);
- }
- public static IList<CustomAttributeData> GetCustomAttributes (ParameterInfo target) {
- return MonoCustomAttrs.GetCustomAttributesData (target);
- }
- public override string ToString ()
- {
- StringBuilder sb = new StringBuilder ();
- sb.Append ("[" + ctorInfo.DeclaringType.FullName + "(");
- for (int i = 0; i < ctorArgs.Count; i++) {
- sb.Append (ctorArgs [i].ToString ());
- if (i + 1 < ctorArgs.Count)
- sb.Append (", ");
- }
- if (namedArgs.Count > 0)
- sb.Append (", ");
-
- for (int j = 0; j < namedArgs.Count; j++) {
- sb.Append (namedArgs [j].ToString ());
- if (j + 1 < namedArgs.Count)
- sb.Append (", ");
- }
- sb.AppendFormat (")]");
- return sb.ToString ();
- }
- static T [] UnboxValues<T> (object [] values)
- {
- T [] retval = new T [values.Length];
- for (int i = 0; i < values.Length; i++)
- retval [i] = (T) values [i];
- return retval;
- }
- public override bool Equals (object obj)
- {
- CustomAttributeData other = obj as CustomAttributeData;
- if (other == null || other.ctorInfo != ctorInfo ||
- other.ctorArgs.Count != ctorArgs.Count ||
- other.namedArgs.Count != namedArgs.Count)
- return false;
- for (int i = 0; i < ctorArgs.Count; i++)
- if (ctorArgs [i].Equals (other.ctorArgs [i]))
- return false;
- for (int i = 0; i < namedArgs.Count; i++) {
- bool matched = false;
- for (int j = 0; j < other.namedArgs.Count; j++)
- if (namedArgs [i].Equals (other.namedArgs [j])) {
- matched = true;
- break;
- }
- if (!matched)
- return false;
- }
- return true;
- }
- public override int GetHashCode ()
- {
- int ret = ctorInfo.GetHashCode () << 16;
- // argument order-dependent
- for (int i = 0; i < ctorArgs.Count; i++)
- ret += ret ^ 7 + ctorArgs [i].GetHashCode () << (i * 4);
- // argument order-independent
- for (int i = 0; i < namedArgs.Count; i++)
- ret += (namedArgs [i].GetHashCode () << 5);
- return ret;
- }
- }
- }
|