internal static AttributeUsageAttribute GetAttributeUsage(RuntimeType decoratedAttribute) { RuntimeModule runtimeModule = decoratedAttribute.GetRuntimeModule(); MetadataImport metadataImport = runtimeModule.MetadataImport; CustomAttributeRecord[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(runtimeModule, decoratedAttribute.MetadataToken); AttributeUsageAttribute attributeUsageAttribute = null; foreach (CustomAttributeRecord customAttributeRecord in customAttributeRecords) { RuntimeType runtimeType = runtimeModule.ResolveType(metadataImport.GetParentToken(customAttributeRecord.tkCtor), null, null) as RuntimeType; if (!(runtimeType != (RuntimeType)typeof(AttributeUsageAttribute))) { if (attributeUsageAttribute != null) { throw new FormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Format_AttributeUsage"), runtimeType)); } AttributeTargets validOn; bool inherited; bool allowMultiple; CustomAttribute.ParseAttributeUsageAttribute(customAttributeRecord.blob, out validOn, out inherited, out allowMultiple); attributeUsageAttribute = new AttributeUsageAttribute(validOn, allowMultiple, inherited); } } if (attributeUsageAttribute == null) { return(AttributeUsageAttribute.Default); } return(attributeUsageAttribute); }
internal static AttributeUsageAttribute GetAttributeUsage(RuntimeType decoratedAttribute) { RuntimeModule runtimeModule = decoratedAttribute.GetRuntimeModule(); MetadataImport metadataImport = runtimeModule.MetadataImport; CustomAttributeRecord[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(runtimeModule, decoratedAttribute.MetadataToken); AttributeUsageAttribute attribute = null; for (int i = 0; i < customAttributeRecords.Length; i++) { CustomAttributeRecord record = customAttributeRecords[i]; RuntimeType type = runtimeModule.ResolveType(metadataImport.GetParentToken((int)record.tkCtor), null, null) as RuntimeType; if (type == ((RuntimeType)typeof(AttributeUsageAttribute))) { AttributeTargets targets; bool flag; bool flag2; if (attribute != null) { throw new FormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Format_AttributeUsage"), new object[] { type })); } ParseAttributeUsageAttribute(record.blob, out targets, out flag, out flag2); attribute = new AttributeUsageAttribute(targets, flag2, flag); } } if (attribute == null) { return(AttributeUsageAttribute.Default); } return(attribute); }
public virtual Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { RuntimeModule module = this as RuntimeModule; if (module == null) { throw new NotImplementedException(); } return(module.ResolveType(metadataToken, genericTypeArguments, genericMethodArguments)); }
public virtual Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { // This API was made virtual in V4. Code compiled against V2 might use // "call" rather than "callvirt" to call it. // This makes sure those code still works. RuntimeModule rtModule = this as RuntimeModule; if (rtModule != null) { return(rtModule.ResolveType(metadataToken, genericTypeArguments, genericMethodArguments)); } throw new NotImplementedException(); }
private static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = (IRuntimeMethodInfo)null; attributeType = (RuntimeType)null; ctorHasParameters = false; isVarArg = false; IntPtr num = (IntPtr)((void *)((IntPtr)(void *)caRecord.blob.Signature + caRecord.blob.Length)); attributeType = decoratedModule.ResolveType(scope.GetParentToken((int)caRecord.tkCtor), (Type[])null, (Type[])null) as RuntimeType; if (!attributeFilterType.IsAssignableFrom((TypeInfo)attributeType) || !CustomAttribute.AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes) || (attributeType.Attributes & TypeAttributes.WindowsRuntime) == TypeAttributes.WindowsRuntime) { return(false); } RuntimeAssembly targetAssembly = (RuntimeAssembly)attributeType.Assembly; RuntimeAssembly sourceAssembly = (RuntimeAssembly)decoratedModule.Assembly; if ((Assembly)targetAssembly != lastAptcaOkAssembly && !RuntimeAssembly.AptcaCheck(targetAssembly, sourceAssembly)) { return(false); } lastAptcaOkAssembly = (Assembly)sourceAssembly; ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = ((uint)methodSignature[0] & 5U) > 0U; ctorHasParameters = (uint)methodSignature[1] > 0U; RuntimeTypeHandle runtimeTypeHandle1; if (ctorHasParameters) { ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), (int)caRecord.tkCtor); } else { // ISSUE: explicit reference operation // ISSUE: variable of a reference type IRuntimeMethodInfo& local = @ctor; runtimeTypeHandle1 = attributeType.GetTypeHandleInternal(); IRuntimeMethodInfo defaultConstructor = runtimeTypeHandle1.GetDefaultConstructor();
[System.Security.SecurityCritical] // auto-generated private unsafe static bool FilterCustomAttributeRecord( CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = null; attributeType = null; ctorHasParameters = false; isVarArg = false; IntPtr blobStart = caRecord.blob.Signature; IntPtr blobEnd = (IntPtr)((byte*)blobStart + caRecord.blob.Length); #if FEATURE_LEGACYNETCF if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) { try { // Resolve attribute type from ctor parent token found in decorated decoratedModule scope attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType; } catch(Exception) { return false; } } else #endif // Resolve attribute type from ctor parent token found in decorated decoratedModule scope attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType; // Test attribute type against user provided attribute type filter if (!(attributeFilterType.IsAssignableFrom(attributeType))) return false; // Ensure if attribute type must be inheritable that it is inhertiable // Ensure that to consider a duplicate attribute type AllowMultiple is true if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes)) return false; // Windows Runtime attributes aren't real types - they exist to be read as metadata only, and as such // should be filtered out of the GetCustomAttributes path. if ((attributeType.Attributes & TypeAttributes.WindowsRuntime) == TypeAttributes.WindowsRuntime) { return false; } #if FEATURE_APTCA // APTCA checks RuntimeAssembly attributeAssembly = (RuntimeAssembly)attributeType.Assembly; RuntimeAssembly decoratedModuleAssembly = (RuntimeAssembly)decoratedModule.Assembly; if (attributeAssembly != lastAptcaOkAssembly && !RuntimeAssembly.AptcaCheck(attributeAssembly, decoratedModuleAssembly)) return false; // Cache last successful APTCA check (optimization) lastAptcaOkAssembly = decoratedModuleAssembly; #endif // FEATURE_APTCA // Resolve the attribute ctor ConstArray ctorSig = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = (ctorSig[0] & 0x05) != 0; ctorHasParameters = ctorSig[1] != 0; if (ctorHasParameters) { // Resolve method ctor token found in decorated decoratedModule scope ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), caRecord.tkCtor); } else { // Resolve method ctor token from decorated decoratedModule scope ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor(); if (ctor == null && !attributeType.IsValueType) throw new MissingMethodException(".ctor"); } // Visibility checks MetadataToken tkParent = new MetadataToken(); if (decoratedToken.IsParamDef) { tkParent = new MetadataToken(scope.GetParentToken(decoratedToken)); tkParent = new MetadataToken(scope.GetParentToken(tkParent)); } else if (decoratedToken.IsMethodDef || decoratedToken.IsProperty || decoratedToken.IsEvent || decoratedToken.IsFieldDef) { tkParent = new MetadataToken(scope.GetParentToken(decoratedToken)); } else if (decoratedToken.IsTypeDef) { tkParent = decoratedToken; } else if (decoratedToken.IsGenericPar) { tkParent = new MetadataToken(scope.GetParentToken(decoratedToken)); // decoratedToken is a generic parameter on a method. Get the declaring Type of the method. if (tkParent.IsMethodDef) tkParent = new MetadataToken(scope.GetParentToken(tkParent)); } else { // We need to relax this when we add support for other types of decorated tokens. Contract.Assert(decoratedToken.IsModule || decoratedToken.IsAssembly, "The decoratedToken must be either an assembly, a module, a type, or a member."); } // If the attribute is on a type, member, or parameter we check access against the (declaring) type, // otherwise we check access against the module. RuntimeTypeHandle parentTypeHandle = tkParent.IsTypeDef ? decoratedModule.ModuleHandle.ResolveTypeHandle(tkParent) : new RuntimeTypeHandle(); return RuntimeMethodHandle.IsCAVisibleFromDecoratedType(attributeType.TypeHandle, ctor, parentTypeHandle, decoratedModule); }
private unsafe static bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = null; attributeType = null; ctorHasParameters = false; isVarArg = false; IntPtr signature = caRecord.blob.Signature; IntPtr intPtr = (IntPtr)((void *)((byte *)((void *)signature) + caRecord.blob.Length)); attributeType = (decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType); if (!attributeFilterType.IsAssignableFrom(attributeType)) { return(false); } if (!CustomAttribute.AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes)) { return(false); } if ((attributeType.Attributes & TypeAttributes.WindowsRuntime) == TypeAttributes.WindowsRuntime) { return(false); } RuntimeAssembly runtimeAssembly = (RuntimeAssembly)attributeType.Assembly; RuntimeAssembly runtimeAssembly2 = (RuntimeAssembly)decoratedModule.Assembly; if (runtimeAssembly != lastAptcaOkAssembly && !RuntimeAssembly.AptcaCheck(runtimeAssembly, runtimeAssembly2)) { return(false); } lastAptcaOkAssembly = runtimeAssembly2; ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = ((methodSignature[0] & 5) > 0); ctorHasParameters = (methodSignature[1] > 0); if (ctorHasParameters) { ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), caRecord.tkCtor); } else { ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor(); if (ctor == null && !attributeType.IsValueType) { throw new MissingMethodException(".ctor"); } } MetadataToken token = default(MetadataToken); if (decoratedToken.IsParamDef) { token = new MetadataToken(scope.GetParentToken(decoratedToken)); token = new MetadataToken(scope.GetParentToken(token)); } else if (decoratedToken.IsMethodDef || decoratedToken.IsProperty || decoratedToken.IsEvent || decoratedToken.IsFieldDef) { token = new MetadataToken(scope.GetParentToken(decoratedToken)); } else if (decoratedToken.IsTypeDef) { token = decoratedToken; } else if (decoratedToken.IsGenericPar) { token = new MetadataToken(scope.GetParentToken(decoratedToken)); if (token.IsMethodDef) { token = new MetadataToken(scope.GetParentToken(token)); } } RuntimeTypeHandle sourceTypeHandle = token.IsTypeDef ? decoratedModule.ModuleHandle.ResolveTypeHandle(token) : default(RuntimeTypeHandle); return(RuntimeMethodHandle.IsCAVisibleFromDecoratedType(attributeType.TypeHandle, ctor, sourceTypeHandle, decoratedModule)); }
private static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = null; attributeType = null; ctorHasParameters = false; isVarArg = false; IntPtr ptr1 = (IntPtr)(((void *)caRecord.blob.Signature) + caRecord.blob.Length); attributeType = decoratedModule.ResolveType(scope.GetParentToken((int)caRecord.tkCtor), null, null) as RuntimeType; if (!attributeFilterType.IsAssignableFrom(attributeType)) { return(false); } if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes)) { return(false); } RuntimeAssembly targetAssembly = (RuntimeAssembly)attributeType.Assembly; RuntimeAssembly assembly = (RuntimeAssembly)decoratedModule.Assembly; if ((targetAssembly != lastAptcaOkAssembly) && !RuntimeAssembly.AptcaCheck(targetAssembly, assembly)) { return(false); } lastAptcaOkAssembly = assembly; ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = (methodSignature[0] & 5) != 0; ctorHasParameters = methodSignature[1] != 0; if (ctorHasParameters) { ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), (int)caRecord.tkCtor); } else { ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor(); if ((ctor == null) && !attributeType.IsValueType) { throw new MissingMethodException(".ctor"); } } if (ctor == null) { if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule)) { return(false); } return(true); } if (RuntimeMethodHandle.IsVisibleFromModule(ctor, decoratedModule)) { return(true); } MetadataToken token = new MetadataToken(); if (decoratedToken.IsParamDef) { token = new MetadataToken(scope.GetParentToken((int)decoratedToken)); token = new MetadataToken(scope.GetParentToken((int)token)); } else if ((decoratedToken.IsMethodDef || decoratedToken.IsProperty) || (decoratedToken.IsEvent || decoratedToken.IsFieldDef)) { token = new MetadataToken(scope.GetParentToken((int)decoratedToken)); } else if (decoratedToken.IsTypeDef) { token = decoratedToken; } return(token.IsTypeDef && RuntimeMethodHandle.IsVisibleFromType(ctor, decoratedModule.ModuleHandle.ResolveTypeHandle((int)token))); }
private static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = null; attributeType = null; ctorHasParameters = false; isVarArg = false; IntPtr ptr1 = (IntPtr) (((void*) caRecord.blob.Signature) + caRecord.blob.Length); attributeType = decoratedModule.ResolveType(scope.GetParentToken((int) caRecord.tkCtor), null, null) as RuntimeType; if (!attributeFilterType.IsAssignableFrom(attributeType)) { return false; } if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes)) { return false; } RuntimeAssembly targetAssembly = (RuntimeAssembly) attributeType.Assembly; RuntimeAssembly assembly = (RuntimeAssembly) decoratedModule.Assembly; if ((targetAssembly != lastAptcaOkAssembly) && !RuntimeAssembly.AptcaCheck(targetAssembly, assembly)) { return false; } lastAptcaOkAssembly = assembly; ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = (methodSignature[0] & 5) != 0; ctorHasParameters = methodSignature[1] != 0; if (ctorHasParameters) { ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), (int) caRecord.tkCtor); } else { ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor(); if ((ctor == null) && !attributeType.IsValueType) { throw new MissingMethodException(".ctor"); } } if (ctor == null) { if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule)) { return false; } return true; } if (RuntimeMethodHandle.IsVisibleFromModule(ctor, decoratedModule)) { return true; } MetadataToken token = new MetadataToken(); if (decoratedToken.IsParamDef) { token = new MetadataToken(scope.GetParentToken((int) decoratedToken)); token = new MetadataToken(scope.GetParentToken((int) token)); } else if ((decoratedToken.IsMethodDef || decoratedToken.IsProperty) || (decoratedToken.IsEvent || decoratedToken.IsFieldDef)) { token = new MetadataToken(scope.GetParentToken((int) decoratedToken)); } else if (decoratedToken.IsTypeDef) { token = decoratedToken; } return (token.IsTypeDef && RuntimeMethodHandle.IsVisibleFromType(ctor, decoratedModule.ModuleHandle.ResolveTypeHandle((int) token))); }
[System.Security.SecurityCritical] // auto-generated private unsafe static bool FilterCustomAttributeRecord( CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = null; attributeType = null; ctorHasParameters = false; isVarArg = false; IntPtr blobStart = caRecord.blob.Signature; IntPtr blobEnd = (IntPtr)((byte*)blobStart + caRecord.blob.Length); // Resolve attribute type from ctor parent token found in decorated decoratedModule scope attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType; // Test attribute type against user provided attribute type filter if (!(attributeFilterType.IsAssignableFrom(attributeType))) return false; // Ensure if attribute type must be inheritable that it is inhertiable // Ensure that to consider a duplicate attribute type AllowMultiple is true if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes)) return false; #if FEATURE_APTCA // APTCA checks RuntimeAssembly attributeAssembly = (RuntimeAssembly)attributeType.Assembly; RuntimeAssembly decoratedModuleAssembly = (RuntimeAssembly)decoratedModule.Assembly; if (attributeAssembly != lastAptcaOkAssembly && !RuntimeAssembly.AptcaCheck(attributeAssembly, decoratedModuleAssembly)) return false; // Cache last successful APTCA check (optimization) lastAptcaOkAssembly = decoratedModuleAssembly; #endif // FEATURE_APTCA // Resolve the attribute ctor ConstArray ctorSig = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = (ctorSig[0] & 0x05) != 0; ctorHasParameters = ctorSig[1] != 0; if (ctorHasParameters) { // Resolve method ctor token found in decorated decoratedModule scope ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), caRecord.tkCtor); } else { // Resolve method ctor token from decorated decoratedModule scope ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor(); if (ctor == null && !attributeType.IsValueType) throw new MissingMethodException(".ctor"); } // Visibility checks if (ctor == null) { if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule)) return false; return true; } if (RuntimeMethodHandle.IsVisibleFromModule(ctor, decoratedModule)) return true; MetadataToken tkParent = new MetadataToken(); if (decoratedToken.IsParamDef) { tkParent = new MetadataToken(scope.GetParentToken(decoratedToken)); tkParent = new MetadataToken(scope.GetParentToken(tkParent)); } else if (decoratedToken.IsMethodDef || decoratedToken.IsProperty || decoratedToken.IsEvent || decoratedToken.IsFieldDef) { tkParent = new MetadataToken(scope.GetParentToken(decoratedToken)); } else if (decoratedToken.IsTypeDef) { tkParent = decoratedToken; } if (tkParent.IsTypeDef) return RuntimeMethodHandle.IsVisibleFromType(ctor, decoratedModule.ModuleHandle.ResolveTypeHandle(tkParent)); return false; }