Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Reflection / CustomAttribute.cs / 1305376 / CustomAttribute.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// [....]
//
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;
using System.Resources;
using System.Diagnostics;
using System.Globalization;
using System.Security;
using System.Security.Permissions;
using System.Runtime.ConstrainedExecution;
using System.Runtime.Versioning;
using System.Diagnostics.Contracts;
namespace System.Reflection
{
[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class CustomAttributeData
{
#region Public Static Members
[System.Security.SecuritySafeCritical] // auto-generated
public static IList GetCustomAttributes(MemberInfo target)
{
if (target == null)
throw new ArgumentNullException("target");
return target.GetCustomAttributesData();
}
[System.Security.SecuritySafeCritical] // auto-generated
public static IList GetCustomAttributes(Module target)
{
if (target == null)
throw new ArgumentNullException("target");
Contract.EndContractBlock();
return target.GetCustomAttributesData();
}
[System.Security.SecuritySafeCritical] // auto-generated
public static IList GetCustomAttributes(Assembly target)
{
if (target == null)
throw new ArgumentNullException("target");
Contract.EndContractBlock();
return target.GetCustomAttributesData();
}
[System.Security.SecuritySafeCritical] // auto-generated
public static IList GetCustomAttributes(ParameterInfo target)
{
if (target == null)
throw new ArgumentNullException("target");
Contract.EndContractBlock();
return target.GetCustomAttributesData();
}
#endregion
#region Internal Static Members
[System.Security.SecuritySafeCritical] // auto-generated
internal static IList GetCustomAttributesInternal(RuntimeType target)
{
Contract.Assert(target != null);
IList cad = GetCustomAttributes(target.GetRuntimeModule(), target.MetadataToken);
int pcaCount = 0;
Attribute[] a = PseudoCustomAttribute.GetCustomAttributes((RuntimeType)target, typeof(object) as RuntimeType, true, out pcaCount);
if (pcaCount == 0)
return cad;
CustomAttributeData[] pca = new CustomAttributeData[cad.Count + pcaCount];
cad.CopyTo(pca, pcaCount);
for (int i = 0; i < pcaCount; i++)
{
pca[i] = new CustomAttributeData(a[i]);
}
return Array.AsReadOnly(pca);
}
[System.Security.SecuritySafeCritical] // auto-generated
internal static IList GetCustomAttributesInternal(RuntimeFieldInfo target)
{
Contract.Assert(target != null);
IList cad = GetCustomAttributes(target.GetRuntimeModule(), target.MetadataToken);
int pcaCount = 0;
Attribute[] a = PseudoCustomAttribute.GetCustomAttributes((RuntimeFieldInfo)target, typeof(object) as RuntimeType, out pcaCount);
if (pcaCount == 0)
return cad;
CustomAttributeData[] pca = new CustomAttributeData[cad.Count + pcaCount];
cad.CopyTo(pca, pcaCount);
for (int i = 0; i < pcaCount; i++)
{
pca[i] = new CustomAttributeData(a[i]);
}
return Array.AsReadOnly(pca);
}
[System.Security.SecuritySafeCritical] // auto-generated
internal static IList GetCustomAttributesInternal(RuntimeMethodInfo target)
{
Contract.Assert(target != null);
IList cad = GetCustomAttributes(target.GetRuntimeModule(), target.MetadataToken);
int pcaCount = 0;
Attribute[] a = PseudoCustomAttribute.GetCustomAttributes((RuntimeMethodInfo)target, typeof(object) as RuntimeType, true, out pcaCount);
if (pcaCount == 0)
return cad;
CustomAttributeData[] pca = new CustomAttributeData[cad.Count + pcaCount];
cad.CopyTo(pca, pcaCount);
for (int i = 0; i < pcaCount; i++)
{
pca[i] = new CustomAttributeData(a[i]);
}
return Array.AsReadOnly(pca);
}
[System.Security.SecuritySafeCritical] // auto-generated
internal static IList GetCustomAttributesInternal(RuntimeConstructorInfo target)
{
Contract.Assert(target != null);
return GetCustomAttributes(target.GetRuntimeModule(), target.MetadataToken);
}
[System.Security.SecuritySafeCritical] // auto-generated
internal static IList GetCustomAttributesInternal(RuntimeEventInfo target)
{
Contract.Assert(target != null);
return GetCustomAttributes(target.GetRuntimeModule(), target.MetadataToken);
}
[System.Security.SecuritySafeCritical] // auto-generated
internal static IList GetCustomAttributesInternal(RuntimePropertyInfo target)
{
Contract.Assert(target != null);
return GetCustomAttributes(target.GetRuntimeModule(), target.MetadataToken);
}
[System.Security.SecuritySafeCritical] // auto-generated
internal static IList GetCustomAttributesInternal(RuntimeModule target)
{
Contract.Assert(target != null);
if (target.IsResource())
return new List();
return GetCustomAttributes(target, target.MetadataToken);
}
[System.Security.SecuritySafeCritical] // auto-generated
internal static IList GetCustomAttributesInternal(RuntimeAssembly target)
{
Contract.Assert(target != null);
IList cad = GetCustomAttributes((RuntimeModule)target.ManifestModule, RuntimeAssembly.GetToken(target.GetNativeHandle()));
int pcaCount = 0;
Attribute[] a = PseudoCustomAttribute.GetCustomAttributes(target, typeof(object) as RuntimeType, false, out pcaCount);
if (pcaCount == 0)
return cad;
CustomAttributeData[] pca = new CustomAttributeData[cad.Count + pcaCount];
cad.CopyTo(pca, pcaCount);
for (int i = 0; i < pcaCount; i++)
{
pca[i] = new CustomAttributeData(a[i]);
}
return Array.AsReadOnly(pca);
}
[System.Security.SecuritySafeCritical] // auto-generated
internal static IList GetCustomAttributesInternal(RuntimeParameterInfo target)
{
Contract.Assert(target != null);
IList cad = GetCustomAttributes(target.GetRuntimeModule(), target.MetadataToken);
int pcaCount = 0;
Attribute[] a = PseudoCustomAttribute.GetCustomAttributes(target, typeof(object) as RuntimeType, out pcaCount);
if (pcaCount == 0)
return cad;
CustomAttributeData[] pca = new CustomAttributeData[cad.Count + pcaCount];
cad.CopyTo(pca, pcaCount);
for (int i = 0; i < pcaCount; i++)
pca[i] = new CustomAttributeData(a[i]);
return Array.AsReadOnly(pca);
}
#endregion
#region Private Static Methods
private static CustomAttributeEncoding TypeToCustomAttributeEncoding(RuntimeType type)
{
if (type == (RuntimeType)typeof(int))
return CustomAttributeEncoding.Int32;
if (type.IsEnum)
return CustomAttributeEncoding.Enum;
if (type == (RuntimeType)typeof(string))
return CustomAttributeEncoding.String;
if (type == (RuntimeType)typeof(Type))
return CustomAttributeEncoding.Type;
if (type == (RuntimeType)typeof(object))
return CustomAttributeEncoding.Object;
if (type.IsArray)
return CustomAttributeEncoding.Array;
if (type == (RuntimeType)typeof(char))
return CustomAttributeEncoding.Char;
if (type == (RuntimeType)typeof(bool))
return CustomAttributeEncoding.Boolean;
if (type == (RuntimeType)typeof(byte))
return CustomAttributeEncoding.Byte;
if (type == (RuntimeType)typeof(sbyte))
return CustomAttributeEncoding.SByte;
if (type == (RuntimeType)typeof(short))
return CustomAttributeEncoding.Int16;
if (type == (RuntimeType)typeof(ushort))
return CustomAttributeEncoding.UInt16;
if (type == (RuntimeType)typeof(uint))
return CustomAttributeEncoding.UInt32;
if (type == (RuntimeType)typeof(long))
return CustomAttributeEncoding.Int64;
if (type == (RuntimeType)typeof(ulong))
return CustomAttributeEncoding.UInt64;
if (type == (RuntimeType)typeof(float))
return CustomAttributeEncoding.Float;
if (type == (RuntimeType)typeof(double))
return CustomAttributeEncoding.Double;
// System.Enum is neither an Enum nor a Class
if (type == (RuntimeType)typeof(Enum))
return CustomAttributeEncoding.Object;
if (type.IsClass)
return CustomAttributeEncoding.Object;
if (type.IsInterface)
return CustomAttributeEncoding.Object;
if (type.IsValueType)
return CustomAttributeEncoding.Undefined;
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidKindOfTypeForCA"), "type");
}
private static CustomAttributeType InitCustomAttributeType(RuntimeType parameterType)
{
CustomAttributeEncoding encodedType = CustomAttributeData.TypeToCustomAttributeEncoding(parameterType);
CustomAttributeEncoding encodedArrayType = CustomAttributeEncoding.Undefined;
CustomAttributeEncoding encodedEnumType = CustomAttributeEncoding.Undefined;
string enumName = null;
if (encodedType == CustomAttributeEncoding.Array)
{
parameterType = (RuntimeType)parameterType.GetElementType();
encodedArrayType = CustomAttributeData.TypeToCustomAttributeEncoding(parameterType);
}
if (encodedType == CustomAttributeEncoding.Enum || encodedArrayType == CustomAttributeEncoding.Enum)
{
encodedEnumType = TypeToCustomAttributeEncoding((RuntimeType)Enum.GetUnderlyingType(parameterType));
enumName = parameterType.AssemblyQualifiedName;
}
return new CustomAttributeType(encodedType, encodedArrayType, encodedEnumType, enumName);
}
[System.Security.SecurityCritical] // auto-generated
private static IList GetCustomAttributes(RuntimeModule module, int tkTarget)
{
CustomAttributeRecord[] records = GetCustomAttributeRecords(module, tkTarget);
CustomAttributeData[] customAttributes = new CustomAttributeData[records.Length];
for (int i = 0; i < records.Length; i++)
customAttributes[i] = new CustomAttributeData(module, records[i]);
return Array.AsReadOnly(customAttributes);
}
#endregion
#region Internal Static Members
[System.Security.SecurityCritical] // auto-generated
internal unsafe static CustomAttributeRecord[] GetCustomAttributeRecords(RuntimeModule module, int targetToken)
{
MetadataImport scope = module.MetadataImport;
int cCustomAttributeTokens = scope.EnumCustomAttributesCount(targetToken);
int* tkCustomAttributeTokens = stackalloc int[cCustomAttributeTokens];
scope.EnumCustomAttributes(targetToken, tkCustomAttributeTokens, cCustomAttributeTokens);
CustomAttributeRecord[] records = new CustomAttributeRecord[cCustomAttributeTokens];
for (int i = 0; i < cCustomAttributeTokens; i++)
{
scope.GetCustomAttributeProps(
tkCustomAttributeTokens[i], out records[i].tkCtor.Value, out records[i].blob);
}
return records;
}
internal static CustomAttributeTypedArgument Filter(IList attrs, Type caType, string name)
{
for (int i = 0; i < attrs.Count; i++)
{
if (attrs[i].Constructor.DeclaringType == caType)
{
IList namedArguments = attrs[i].NamedArguments;
for (int j = 0; j < namedArguments.Count; j++)
{
if (namedArguments[j].MemberInfo.Name.Equals(name))
return namedArguments[j].TypedValue;
}
}
}
return new CustomAttributeTypedArgument();
}
internal static CustomAttributeTypedArgument Filter(IList attrs, Type caType, int parameter)
{
for (int i = 0; i < attrs.Count; i++)
{
if (attrs[i].Constructor.DeclaringType == caType)
{
return attrs[i].ConstructorArguments[parameter];
}
}
return new CustomAttributeTypedArgument();
}
#endregion
#region Private Data Members
private ConstructorInfo m_ctor;
private RuntimeModule m_scope;
private MemberInfo[] m_members;
private CustomAttributeCtorParameter[] m_ctorParams;
private CustomAttributeNamedParameter[] m_namedParams;
private IList m_typedCtorArgs;
private IList m_namedArgs;
#endregion
#region Constructor
[System.Security.SecurityCritical] // auto-generated
protected CustomAttributeData()
{
}
[System.Security.SecuritySafeCritical] // auto-generated
private CustomAttributeData(RuntimeModule scope, CustomAttributeRecord caRecord)
{
m_scope = scope;
m_ctor = (RuntimeConstructorInfo)RuntimeType.GetMethodBase(scope, caRecord.tkCtor);
ParameterInfo[] parameters = m_ctor.GetParametersNoCopy();
m_ctorParams = new CustomAttributeCtorParameter[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
m_ctorParams[i] = new CustomAttributeCtorParameter(InitCustomAttributeType((RuntimeType)parameters[i].ParameterType));
FieldInfo[] fields = m_ctor.DeclaringType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
PropertyInfo[] properties = m_ctor.DeclaringType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
m_namedParams = new CustomAttributeNamedParameter[properties.Length + fields.Length];
for (int i = 0; i < fields.Length; i++)
m_namedParams[i] = new CustomAttributeNamedParameter(
fields[i].Name, CustomAttributeEncoding.Field, InitCustomAttributeType((RuntimeType)fields[i].FieldType));
for (int i = 0; i < properties.Length; i++)
m_namedParams[i + fields.Length] = new CustomAttributeNamedParameter(
properties[i].Name, CustomAttributeEncoding.Property, InitCustomAttributeType((RuntimeType)properties[i].PropertyType));
m_members = new MemberInfo[fields.Length + properties.Length];
fields.CopyTo(m_members, 0);
properties.CopyTo(m_members, fields.Length);
CustomAttributeEncodedArgument.ParseAttributeArguments(caRecord.blob, ref m_ctorParams, ref m_namedParams, m_scope);
}
#endregion
#region Pseudo Custom Attribute Constructor
internal CustomAttributeData(Attribute attribute)
{
if (attribute is DllImportAttribute)
Init((DllImportAttribute)attribute);
else if (attribute is FieldOffsetAttribute)
Init((FieldOffsetAttribute)attribute);
else if (attribute is MarshalAsAttribute)
Init((MarshalAsAttribute)attribute);
else if (attribute is TypeForwardedToAttribute)
Init((TypeForwardedToAttribute)attribute);
else
Init(attribute);
}
private void Init(DllImportAttribute dllImport)
{
Type type = typeof(DllImportAttribute);
m_ctor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance)[0];
m_typedCtorArgs = Array.AsReadOnly(new CustomAttributeTypedArgument[]
{
new CustomAttributeTypedArgument(dllImport.Value),
});
m_namedArgs = Array.AsReadOnly(new CustomAttributeNamedArgument[]
{
new CustomAttributeNamedArgument(type.GetField("EntryPoint"), dllImport.EntryPoint),
new CustomAttributeNamedArgument(type.GetField("CharSet"), dllImport.CharSet),
new CustomAttributeNamedArgument(type.GetField("ExactSpelling"), dllImport.ExactSpelling),
new CustomAttributeNamedArgument(type.GetField("SetLastError"), dllImport.SetLastError),
new CustomAttributeNamedArgument(type.GetField("PreserveSig"), dllImport.PreserveSig),
new CustomAttributeNamedArgument(type.GetField("CallingConvention"), dllImport.CallingConvention),
new CustomAttributeNamedArgument(type.GetField("BestFitMapping"), dllImport.BestFitMapping),
new CustomAttributeNamedArgument(type.GetField("ThrowOnUnmappableChar"), dllImport.ThrowOnUnmappableChar)
});
}
private void Init(FieldOffsetAttribute fieldOffset)
{
m_ctor = typeof(FieldOffsetAttribute).GetConstructors(BindingFlags.Public | BindingFlags.Instance)[0];
m_typedCtorArgs = Array.AsReadOnly(new CustomAttributeTypedArgument[] {
new CustomAttributeTypedArgument(fieldOffset.Value)
});
m_namedArgs = Array.AsReadOnly(new CustomAttributeNamedArgument[0]);
}
private void Init(MarshalAsAttribute marshalAs)
{
Type type = typeof(MarshalAsAttribute);
m_ctor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance)[0];
m_typedCtorArgs = Array.AsReadOnly(new CustomAttributeTypedArgument[]
{
new CustomAttributeTypedArgument(marshalAs.Value),
});
int i = 3; // ArraySubType, SizeParamIndex, SizeConst
if (marshalAs.MarshalType != null) i++;
if (marshalAs.MarshalTypeRef != null) i++;
if (marshalAs.MarshalCookie != null) i++;
#if FEATURE_COMINTEROP
i++; // IidParameterIndex
i++; // SafeArraySubType
if (marshalAs.SafeArrayUserDefinedSubType != null) i++;
#endif // FEATURE_COMINTEROP
CustomAttributeNamedArgument[] namedArgs = new CustomAttributeNamedArgument[i];
i = 0;
namedArgs[i++] = new CustomAttributeNamedArgument(type.GetField("ArraySubType"), marshalAs.ArraySubType);
namedArgs[i++] = new CustomAttributeNamedArgument(type.GetField("SizeParamIndex"), marshalAs.SizeParamIndex);
namedArgs[i++] = new CustomAttributeNamedArgument(type.GetField("SizeConst"), marshalAs.SizeConst);
#if FEATURE_COMINTEROP
namedArgs[i++] = new CustomAttributeNamedArgument(type.GetField("IidParameterIndex"), marshalAs.IidParameterIndex);
namedArgs[i++] = new CustomAttributeNamedArgument(type.GetField("SafeArraySubType"), marshalAs.SafeArraySubType);
#endif // FEATURE_COMINTEROP
if (marshalAs.MarshalType != null)
namedArgs[i++] = new CustomAttributeNamedArgument(type.GetField("MarshalType"), marshalAs.MarshalType);
if (marshalAs.MarshalTypeRef != null)
namedArgs[i++] = new CustomAttributeNamedArgument(type.GetField("MarshalTypeRef"), marshalAs.MarshalTypeRef);
if (marshalAs.MarshalCookie != null)
namedArgs[i++] = new CustomAttributeNamedArgument(type.GetField("MarshalCookie"), marshalAs.MarshalCookie);
#if FEATURE_COMINTEROP
if (marshalAs.SafeArrayUserDefinedSubType != null)
namedArgs[i++] = new CustomAttributeNamedArgument(type.GetField("SafeArrayUserDefinedSubType"), marshalAs.SafeArrayUserDefinedSubType);
#endif // FEATURE_COMINTEROP
m_namedArgs = Array.AsReadOnly(namedArgs);
}
private void Init(TypeForwardedToAttribute forwardedTo)
{
Type type = typeof(TypeForwardedToAttribute);
Type[] sig = new Type[] { typeof(Type) };
m_ctor = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, sig, null);
CustomAttributeTypedArgument[] typedArgs = new CustomAttributeTypedArgument[1];
typedArgs[0] = new CustomAttributeTypedArgument(typeof(Type), forwardedTo.Destination);
m_typedCtorArgs = Array.AsReadOnly(typedArgs);
CustomAttributeNamedArgument[] namedArgs = new CustomAttributeNamedArgument[0];
m_namedArgs = Array.AsReadOnly(namedArgs);
}
private void Init(object pca)
{
m_ctor = pca.GetType().GetConstructors(BindingFlags.Public | BindingFlags.Instance)[0];
m_typedCtorArgs = Array.AsReadOnly(new CustomAttributeTypedArgument[0]);
m_namedArgs = Array.AsReadOnly(new CustomAttributeNamedArgument[0]);
}
#endregion
#region Object Override
public override string ToString()
{
string ctorArgs = "";
for (int i = 0; i < ConstructorArguments.Count; i ++)
ctorArgs += String.Format(CultureInfo.CurrentCulture, i == 0 ? "{0}" : ", {0}", ConstructorArguments[i]);
string namedArgs = "";
for (int i = 0; i < NamedArguments.Count; i ++)
namedArgs += String.Format(CultureInfo.CurrentCulture, i == 0 && ctorArgs.Length == 0 ? "{0}" : ", {0}", NamedArguments[i]);
return String.Format(CultureInfo.CurrentCulture, "[{0}({1}{2})]", Constructor.DeclaringType.FullName, ctorArgs, namedArgs);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override bool Equals(object obj)
{
return obj == (object)this;
}
#endregion
#region Public Members
[System.Runtime.InteropServices.ComVisible(true)]
public virtual ConstructorInfo Constructor { get { return m_ctor; } }
[System.Runtime.InteropServices.ComVisible(true)]
public virtual IList ConstructorArguments
{
get
{
if (m_typedCtorArgs == null)
{
CustomAttributeTypedArgument[] typedCtorArgs = new CustomAttributeTypedArgument[m_ctorParams.Length];
for (int i = 0; i < typedCtorArgs.Length; i++)
{
CustomAttributeEncodedArgument encodedArg = m_ctorParams[i].CustomAttributeEncodedArgument;
typedCtorArgs[i] = new CustomAttributeTypedArgument(m_scope, m_ctorParams[i].CustomAttributeEncodedArgument);
}
m_typedCtorArgs = Array.AsReadOnly(typedCtorArgs);
}
return m_typedCtorArgs;
}
}
public virtual IList NamedArguments
{
get
{
if (m_namedArgs == null)
{
if (m_namedParams == null)
return null;
int cNamedArgs = 0;
for (int i = 0; i < m_namedParams.Length; i++)
{
if (m_namedParams[i].EncodedArgument.CustomAttributeType.EncodedType != CustomAttributeEncoding.Undefined)
cNamedArgs++;
}
CustomAttributeNamedArgument[] namedArgs = new CustomAttributeNamedArgument[cNamedArgs];
for (int i = 0, j = 0; i < m_namedParams.Length; i++)
{
if (m_namedParams[i].EncodedArgument.CustomAttributeType.EncodedType != CustomAttributeEncoding.Undefined)
namedArgs[j++] = new CustomAttributeNamedArgument(
m_members[i], new CustomAttributeTypedArgument(m_scope, m_namedParams[i].EncodedArgument));
}
m_namedArgs = Array.AsReadOnly(namedArgs);
}
return m_namedArgs;
}
}
#endregion
}
[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct CustomAttributeNamedArgument
{
#region Public Static Members
public static bool operator ==(CustomAttributeNamedArgument left, CustomAttributeNamedArgument right)
{
return left.Equals(right);
}
public static bool operator !=(CustomAttributeNamedArgument left, CustomAttributeNamedArgument right)
{
return !left.Equals(right);
}
#endregion
#region Private Data Members
private MemberInfo m_memberInfo;
private CustomAttributeTypedArgument m_value;
#endregion
#region Constructor
public CustomAttributeNamedArgument(MemberInfo memberInfo, object value)
{
if (memberInfo == null)
throw new ArgumentNullException("memberInfo");
Type type = null;
FieldInfo field = memberInfo as FieldInfo;
PropertyInfo property = memberInfo as PropertyInfo;
if (field != null)
type = field.FieldType;
else if (property != null)
type = property.PropertyType;
else
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidMemberForNamedArgument"));
m_memberInfo = memberInfo;
m_value = new CustomAttributeTypedArgument(type, value);
}
public CustomAttributeNamedArgument(MemberInfo memberInfo, CustomAttributeTypedArgument typedArgument)
{
if (memberInfo == null)
throw new ArgumentNullException("memberInfo");
m_memberInfo = memberInfo;
m_value = typedArgument;
}
#endregion
#region Object Override
public override string ToString()
{
return String.Format(CultureInfo.CurrentCulture, "{0} = {1}", MemberInfo.Name, TypedValue.ToString(ArgumentType != typeof(object)));
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override bool Equals(object obj)
{
return obj == (object)this;
}
#endregion
#region Internal Members
internal Type ArgumentType
{
get
{
return m_memberInfo is FieldInfo ?
((FieldInfo)m_memberInfo).FieldType :
((PropertyInfo)m_memberInfo).PropertyType;
}
}
#endregion
#region Public Members
public MemberInfo MemberInfo { get { return m_memberInfo; } }
public CustomAttributeTypedArgument TypedValue { get { return m_value; } }
#endregion
}
[Serializable]
[ComVisible(true)]
public struct CustomAttributeTypedArgument
{
#region Public Static Members
public static bool operator ==(CustomAttributeTypedArgument left, CustomAttributeTypedArgument right)
{
return left.Equals(right);
}
public static bool operator !=(CustomAttributeTypedArgument left, CustomAttributeTypedArgument right)
{
return !left.Equals(right);
}
#endregion
#region Private Static Methods
private static Type CustomAttributeEncodingToType(CustomAttributeEncoding encodedType)
{
switch (encodedType)
{
case (CustomAttributeEncoding.Enum):
return typeof(Enum);
case (CustomAttributeEncoding.Int32):
return typeof(int);
case (CustomAttributeEncoding.String):
return typeof(string);
case (CustomAttributeEncoding.Type):
return typeof(Type);
case (CustomAttributeEncoding.Array):
return typeof(Array);
case (CustomAttributeEncoding.Char):
return typeof(char);
case (CustomAttributeEncoding.Boolean):
return typeof(bool);
case (CustomAttributeEncoding.SByte):
return typeof(sbyte);
case (CustomAttributeEncoding.Byte):
return typeof(byte);
case (CustomAttributeEncoding.Int16):
return typeof(short);
case (CustomAttributeEncoding.UInt16):
return typeof(ushort);
case (CustomAttributeEncoding.UInt32):
return typeof(uint);
case (CustomAttributeEncoding.Int64):
return typeof(long);
case (CustomAttributeEncoding.UInt64):
return typeof(ulong);
case (CustomAttributeEncoding.Float):
return typeof(float);
case (CustomAttributeEncoding.Double):
return typeof(double);
case (CustomAttributeEncoding.Object):
return typeof(object);
default :
throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)encodedType), "encodedType");
}
}
[System.Security.SecuritySafeCritical] // auto-generated
private static object EncodedValueToRawValue(long val, CustomAttributeEncoding encodedType)
{
switch (encodedType)
{
case CustomAttributeEncoding.Boolean:
return (byte)val != 0;
case CustomAttributeEncoding.Char:
return (char)val;
case CustomAttributeEncoding.Byte:
return (byte)val;
case CustomAttributeEncoding.SByte:
return (sbyte)val;
case CustomAttributeEncoding.Int16:
return (short)val;
case CustomAttributeEncoding.UInt16:
return (ushort)val;
case CustomAttributeEncoding.Int32:
return (int)val;
case CustomAttributeEncoding.UInt32:
return (uint)val;
case CustomAttributeEncoding.Int64:
return (long)val;
case CustomAttributeEncoding.UInt64:
return (ulong)val;
case CustomAttributeEncoding.Float:
unsafe { return *(float*)&val; }
case CustomAttributeEncoding.Double:
unsafe { return *(double*)&val; }
default:
throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)val), "val");
}
}
private static RuntimeType ResolveType(RuntimeModule scope, string typeName)
{
RuntimeType type = RuntimeTypeHandle.GetTypeByNameUsingCARules(typeName, scope);
if (type == null)
throw new InvalidOperationException(
String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Arg_CATypeResolutionFailed"), typeName));
return type;
}
#endregion
#region Private Data Members
private object m_value;
private Type m_argumentType;
#endregion
#region Constructor
public CustomAttributeTypedArgument(Type argumentType, object value)
{
// value can be null.
if (argumentType == null)
throw new ArgumentNullException("argumentType");
m_value = (value == null) ? null : CanonicalizeValue(value);
m_argumentType = argumentType;
}
public CustomAttributeTypedArgument(object value)
{
// value cannot be null.
if (value == null)
throw new ArgumentNullException("value");
m_value = CanonicalizeValue(value);
m_argumentType = value.GetType();
}
private static object CanonicalizeValue(object value)
{
Contract.Assert(value != null);
if (value.GetType().IsEnum)
{
return ((Enum)value).GetValue();
}
return value;
}
[System.Security.SecuritySafeCritical] // auto-generated
internal CustomAttributeTypedArgument(RuntimeModule scope, CustomAttributeEncodedArgument encodedArg)
{
CustomAttributeEncoding encodedType = encodedArg.CustomAttributeType.EncodedType;
if (encodedType == CustomAttributeEncoding.Undefined)
throw new ArgumentException("encodedArg");
else if (encodedType == CustomAttributeEncoding.Enum)
{
m_argumentType = ResolveType(scope, encodedArg.CustomAttributeType.EnumName);
m_value = EncodedValueToRawValue(encodedArg.PrimitiveValue, encodedArg.CustomAttributeType.EncodedEnumType);
}
else if (encodedType == CustomAttributeEncoding.String)
{
m_argumentType = typeof(string);
m_value = encodedArg.StringValue;
}
else if (encodedType == CustomAttributeEncoding.Type)
{
m_argumentType = typeof(Type);
m_value = null;
if (encodedArg.StringValue != null)
m_value = ResolveType(scope, encodedArg.StringValue);
}
else if (encodedType == CustomAttributeEncoding.Array)
{
encodedType = encodedArg.CustomAttributeType.EncodedArrayType;
Type elementType;
if (encodedType == CustomAttributeEncoding.Enum)
{
elementType = ResolveType(scope, encodedArg.CustomAttributeType.EnumName);
}
else
{
elementType = CustomAttributeEncodingToType(encodedType);
}
m_argumentType = elementType.MakeArrayType();
if (encodedArg.ArrayValue == null)
{
m_value = null;
}
else
{
CustomAttributeTypedArgument[] arrayValue = new CustomAttributeTypedArgument[encodedArg.ArrayValue.Length];
for (int i = 0; i < arrayValue.Length; i++)
arrayValue[i] = new CustomAttributeTypedArgument(scope, encodedArg.ArrayValue[i]);
m_value = Array.AsReadOnly(arrayValue);
}
}
else
{
m_argumentType = CustomAttributeEncodingToType(encodedType);
m_value = EncodedValueToRawValue(encodedArg.PrimitiveValue, encodedType);
}
}
#endregion
#region Object Overrides
public override string ToString() { return ToString(false); }
[System.Security.SecuritySafeCritical] // auto-generated
internal string ToString(bool typed)
{
if (ArgumentType.IsEnum)
return String.Format(CultureInfo.CurrentCulture, typed ? "{0}" : "({1}){0}", Value, ArgumentType.FullName);
else if (Value == null)
return String.Format(CultureInfo.CurrentCulture, typed ? "null" : "({0})null", ArgumentType.Name);
else if (ArgumentType == typeof(string))
return String.Format(CultureInfo.CurrentCulture, "\"{0}\"", Value);
else if (ArgumentType == typeof(char))
return String.Format(CultureInfo.CurrentCulture, "'{0}'", Value);
else if (ArgumentType == typeof(Type))
return String.Format(CultureInfo.CurrentCulture, "typeof({0})", ((Type)Value).FullName);
else if (ArgumentType.IsArray)
{
string result = null;
IList array = Value as IList;
Type elementType = ArgumentType.GetElementType();
result = String.Format(CultureInfo.CurrentCulture, @"new {0}[{1}] {{ ", elementType.IsEnum ? elementType.FullName : elementType.Name, array.Count);
for (int i = 0; i < array.Count; i++)
result += String.Format(CultureInfo.CurrentCulture, i == 0 ? "{0}" : ", {0}", array[i].ToString(elementType != typeof(object)));
return result += " }";
}
return String.Format(CultureInfo.CurrentCulture, typed ? "{0}" : "({1}){0}", Value, ArgumentType.Name);
}
[System.Security.SecuritySafeCritical] // auto-generated
public override int GetHashCode()
{
return base.GetHashCode();
}
public override bool Equals(object obj)
{
return obj == (object)this;
}
#endregion
#region Public Members
public Type ArgumentType
{
get
{
return m_argumentType;
}
}
public object Value
{
get
{
return m_value;
}
}
#endregion
}
[Serializable]
internal struct CustomAttributeRecord
{
internal ConstArray blob;
internal MetadataToken tkCtor;
}
[Serializable]
internal enum CustomAttributeEncoding : int
{
Undefined = 0,
Boolean = CorElementType.Boolean,
Char = CorElementType.Char,
SByte = CorElementType.I1,
Byte = CorElementType.U1,
Int16 = CorElementType.I2,
UInt16 = CorElementType.U2,
Int32 = CorElementType.I4,
UInt32 = CorElementType.U4,
Int64 = CorElementType.I8,
UInt64 = CorElementType.U8,
Float = CorElementType.R4,
Double = CorElementType.R8,
String = CorElementType.String,
Array = CorElementType.SzArray,
Type = 0x50,
Object = 0x51,
Field = 0x53,
Property = 0x54,
Enum = 0x55
}
[Serializable]
[StructLayout(LayoutKind.Auto)]
internal struct CustomAttributeEncodedArgument
{
#region Parser
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void ParseAttributeArguments(
IntPtr pCa,
int cCa,
ref CustomAttributeCtorParameter[] CustomAttributeCtorParameters,
ref CustomAttributeNamedParameter[] CustomAttributeTypedArgument,
RuntimeAssembly assembly);
[System.Security.SecurityCritical] // auto-generated
internal static void ParseAttributeArguments(ConstArray attributeBlob,
ref CustomAttributeCtorParameter[] customAttributeCtorParameters,
ref CustomAttributeNamedParameter[] customAttributeNamedParameters,
RuntimeModule customAttributeModule)
{
if (customAttributeModule == null)
throw new ArgumentNullException("customAttributeModule");
Contract.EndContractBlock();
Contract.Assert(customAttributeCtorParameters != null);
Contract.Assert(customAttributeNamedParameters != null);
if (customAttributeCtorParameters.Length != 0 || customAttributeNamedParameters.Length != 0)
{
unsafe
{
ParseAttributeArguments(
attributeBlob.Signature,
(int)attributeBlob.Length,
ref customAttributeCtorParameters,
ref customAttributeNamedParameters,
(RuntimeAssembly)customAttributeModule.Assembly);
}
}
}
#endregion
#region Private Data Members
private long m_primitiveValue;
private CustomAttributeEncodedArgument[] m_arrayValue;
private string m_stringValue;
private CustomAttributeType m_type;
#endregion
#region Public Members
public CustomAttributeType CustomAttributeType { get { return m_type; } }
public long PrimitiveValue { get { return m_primitiveValue; } }
public CustomAttributeEncodedArgument[] ArrayValue { get { return m_arrayValue; } }
public string StringValue { get { return m_stringValue; } }
#endregion
}
[Serializable]
[StructLayout(LayoutKind.Auto)]
internal struct CustomAttributeNamedParameter
{
#region Private Data Members
private string m_argumentName;
private CustomAttributeEncoding m_fieldOrProperty;
private CustomAttributeEncoding m_padding;
private CustomAttributeType m_type;
private CustomAttributeEncodedArgument m_encodedArgument;
#endregion
#region Constructor
public CustomAttributeNamedParameter(string argumentName, CustomAttributeEncoding fieldOrProperty, CustomAttributeType type)
{
if (argumentName == null)
throw new ArgumentNullException("argumentName");
Contract.EndContractBlock();
m_argumentName = argumentName;
m_fieldOrProperty = fieldOrProperty;
m_padding = fieldOrProperty;
m_type = type;
m_encodedArgument = new CustomAttributeEncodedArgument();
}
#endregion
#region Public Members
public CustomAttributeEncodedArgument EncodedArgument { get { return m_encodedArgument; } }
#endregion
}
[Serializable]
[StructLayout(LayoutKind.Auto)]
internal struct CustomAttributeCtorParameter
{
#region Private Data Members
private CustomAttributeType m_type;
private CustomAttributeEncodedArgument m_encodedArgument;
#endregion
#region Constructor
public CustomAttributeCtorParameter(CustomAttributeType type)
{
m_type = type;
m_encodedArgument = new CustomAttributeEncodedArgument();
}
#endregion
#region Public Members
public CustomAttributeEncodedArgument CustomAttributeEncodedArgument { get { return m_encodedArgument; } }
#endregion
}
// Note: This is a managed representation of a frame type defined in vm\frames.h; please ensure the layout remains
// synchronized.
[StructLayout(LayoutKind.Sequential)]
internal struct SecurityContextFrame
{
IntPtr m_GSCookie; // This is actually at a negative offset in the real frame definition
IntPtr __VFN_table; // This is the real start of the SecurityContextFrame
IntPtr m_Next;
IntPtr m_Assembly;
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public extern void Push(RuntimeAssembly assembly);
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
public extern void Pop();
}
[Serializable]
[StructLayout(LayoutKind.Auto)]
internal struct CustomAttributeType
{
#region Private Data Members
/// The most complicated type is an enum[] in which case...
private string m_enumName; // ...enum name
private CustomAttributeEncoding m_encodedType; // ...array
private CustomAttributeEncoding m_encodedEnumType; // ...enum
private CustomAttributeEncoding m_encodedArrayType; // ...enum type
private CustomAttributeEncoding m_padding;
#endregion
#region Constructor
public CustomAttributeType(CustomAttributeEncoding encodedType, CustomAttributeEncoding encodedArrayType,
CustomAttributeEncoding encodedEnumType, string enumName)
{
m_encodedType = encodedType;
m_encodedArrayType = encodedArrayType;
m_encodedEnumType = encodedEnumType;
m_enumName = enumName;
m_padding = m_encodedType;
}
#endregion
#region Public Members
public CustomAttributeEncoding EncodedType { get { return m_encodedType; } }
public CustomAttributeEncoding EncodedEnumType { get { return m_encodedEnumType; } }
public CustomAttributeEncoding EncodedArrayType { get { return m_encodedArrayType; } }
[System.Runtime.InteropServices.ComVisible(true)]
public string EnumName { get { return m_enumName; } }
#endregion
}
internal unsafe static class CustomAttribute
{
#region Private Data Members
private static RuntimeType Type_RuntimeType = (RuntimeType)typeof(RuntimeType);
private static RuntimeType Type_Type = (RuntimeType)typeof(Type);
#endregion
#region Internal Static Members
[System.Security.SecurityCritical] // auto-generated
internal static bool IsDefined(RuntimeType type, RuntimeType caType, bool inherit)
{
Contract.Requires(type != null);
if (type.GetElementType() != null)
return false;
if (PseudoCustomAttribute.IsDefined(type, caType))
return true;
if (IsCustomAttributeDefined(type.GetRuntimeModule(), type.MetadataToken, caType))
return true;
if (!inherit)
return false;
type = type.BaseType as RuntimeType;
while (type != null)
{
if (IsCustomAttributeDefined(type.GetRuntimeModule(), type.MetadataToken, caType, inherit))
return true;
type = type.BaseType as RuntimeType;
}
return false;
}
[System.Security.SecuritySafeCritical] // auto-generated
internal static bool IsDefined(RuntimeMethodInfo method, RuntimeType caType, bool inherit)
{
Contract.Requires(method != null);
Contract.Requires(caType != null);
if (PseudoCustomAttribute.IsDefined(method, caType))
return true;
if (IsCustomAttributeDefined(method.GetRuntimeModule(), method.MetadataToken, caType))
return true;
if (!inherit)
return false;
method = method.GetParentDefinition();
while (method != null)
{
if (IsCustomAttributeDefined(method.GetRuntimeModule(), method.MetadataToken, caType, inherit))
return true;
method = method.GetParentDefinition();
}
return false;
}
[System.Security.SecurityCritical] // auto-generated
internal static bool IsDefined(RuntimeConstructorInfo ctor, RuntimeType caType)
{
Contract.Requires(ctor != null);
Contract.Requires(caType != null);
if (PseudoCustomAttribute.IsDefined(ctor, caType))
return true;
return IsCustomAttributeDefined(ctor.GetRuntimeModule(), ctor.MetadataToken, caType);
}
[System.Security.SecurityCritical] // auto-generated
internal static bool IsDefined(RuntimePropertyInfo property, RuntimeType caType)
{
Contract.Requires(property != null);
Contract.Requires(caType != null);
if (PseudoCustomAttribute.IsDefined(property, caType))
return true;
return IsCustomAttributeDefined(property.GetRuntimeModule(), property.MetadataToken, caType);
}
[System.Security.SecurityCritical] // auto-generated
internal static bool IsDefined(RuntimeEventInfo e, RuntimeType caType)
{
Contract.Requires(e != null);
Contract.Requires(caType != null);
if (PseudoCustomAttribute.IsDefined(e, caType))
return true;
return IsCustomAttributeDefined(e.GetRuntimeModule(), e.MetadataToken, caType);
}
[System.Security.SecurityCritical] // auto-generated
internal static bool IsDefined(RuntimeFieldInfo field, RuntimeType caType)
{
Contract.Requires(field != null);
Contract.Requires(caType != null);
if (PseudoCustomAttribute.IsDefined(field, caType))
return true;
return IsCustomAttributeDefined(field.GetRuntimeModule(), field.MetadataToken, caType);
}
[System.Security.SecurityCritical] // auto-generated
internal static bool IsDefined(RuntimeParameterInfo parameter, RuntimeType caType)
{
Contract.Requires(parameter != null);
Contract.Requires(caType != null);
if (PseudoCustomAttribute.IsDefined(parameter, caType))
return true;
return IsCustomAttributeDefined(parameter.GetRuntimeModule(), parameter.MetadataToken, caType);
}
[System.Security.SecuritySafeCritical] // auto-generated
internal static bool IsDefined(RuntimeAssembly assembly, RuntimeType caType)
{
Contract.Requires(assembly != null);
Contract.Requires(caType != null);
if (PseudoCustomAttribute.IsDefined(assembly, caType))
return true;
return IsCustomAttributeDefined(assembly.ManifestModule as RuntimeModule, RuntimeAssembly.GetToken(assembly.GetNativeHandle()), caType);
}
[System.Security.SecurityCritical] // auto-generated
internal static bool IsDefined(RuntimeModule module, RuntimeType caType)
{
Contract.Requires(module != null);
Contract.Requires(caType != null);
if (PseudoCustomAttribute.IsDefined(module, caType))
return true;
return IsCustomAttributeDefined(module, module.MetadataToken, caType);
}
[System.Security.SecurityCritical] // auto-generated
internal static Object[] GetCustomAttributes(RuntimeType type, RuntimeType caType, bool inherit)
{
Contract.Requires(type != null);
Contract.Requires(caType != null);
if (type.GetElementType() != null)
return (caType.IsValueType) ? new object[0] : CreateAttributeArrayHelper(caType, 0);
if (type.IsGenericType && !type.IsGenericTypeDefinition)
type = type.GetGenericTypeDefinition() as RuntimeType;
int pcaCount = 0;
Attribute[] pca = PseudoCustomAttribute.GetCustomAttributes(type, caType, true, out pcaCount);
// if we are asked to go up the hierarchy chain we have to do it now and regardless of the
// attribute usage for the specific attribute because a derived attribute may override the usage...
// ... however if the attribute is sealed we can rely on the attribute usage
if (!inherit || (caType.IsSealed && !CustomAttribute.GetAttributeUsage(caType).Inherited))
{
object[] attributes = GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, pcaCount, caType, !AllowCriticalCustomAttributes(type));
if (pcaCount > 0) Array.Copy(pca, 0, attributes, attributes.Length - pcaCount, pcaCount);
return attributes;
}
List