private void ImportCustomAttributesNames() { _customAttributes = new List <string>(); AssemblyDefinition def = _reader.GetAssemblyDefinition(); CustomAttributeHandleCollection col = def.GetCustomAttributes(); foreach (CustomAttributeHandle handle in col) { EntityHandle ctorHandle = _reader.GetCustomAttribute(handle).Constructor; if (ctorHandle.Kind != HandleKind.MemberReference) { continue; } EntityHandle mHandle = _reader.GetMemberReference((MemberReferenceHandle)ctorHandle).Parent; if (mHandle.Kind != HandleKind.TypeReference) { continue; } string type = GetTypeName((TypeReferenceHandle)mHandle); _customAttributes.Add(type); } }
private List <CustomAttribute> HandleCustomAttributes(Cts.Ecma.EcmaModule module, Ecma.CustomAttributeHandleCollection attributes) { List <CustomAttribute> customAttributes = new List <CustomAttribute>(attributes.Count); var attributeTypeProvider = new Cts.Ecma.CustomAttributeTypeProvider(module); foreach (var attributeHandle in attributes) { Ecma.MetadataReader reader = module.MetadataReader; Ecma.CustomAttribute attribute = reader.GetCustomAttribute(attributeHandle); // TODO-NICE: We can intern the attributes based on the CA constructor and blob bytes try { Cts.MethodDesc constructor = module.GetMethod(attribute.Constructor); var decodedValue = attribute.DecodeValue(attributeTypeProvider); if (IsBlockedCustomAttribute(constructor, decodedValue)) { continue; } customAttributes.Add(HandleCustomAttribute(constructor, decodedValue)); } catch (Cts.TypeSystemException) { // TODO: We should emit unresolvable custom attributes instead of skipping these } } return(customAttributes); }
private List <CustomAttribute> HandleCustomAttributes(Cts.Ecma.EcmaModule module, Ecma.CustomAttributeHandleCollection attributes) { List <CustomAttribute> customAttributes = new List <CustomAttribute>(attributes.Count); var attributeTypeProvider = new Cts.Ecma.CustomAttributeTypeProvider(module); Ecma.MetadataReader reader = module.MetadataReader; foreach (var attributeHandle in attributes) { if (!_policy.GeneratesMetadata(module, attributeHandle)) { continue; } Ecma.CustomAttribute attribute = reader.GetCustomAttribute(attributeHandle); // TODO-NICE: We can intern the attributes based on the CA constructor and blob bytes Cts.MethodDesc constructor = module.GetMethod(attribute.Constructor); var decodedValue = attribute.DecodeValue(attributeTypeProvider); customAttributes.Add(HandleCustomAttribute(constructor, decodedValue)); } return(customAttributes); }
/// <summary> /// Returns the name of the attribute class /// </summary> internal static string GetAttributeName(MetadataReader metadataReader, CustomAttributeHandle customAttribute) { var ctorHandle = metadataReader.GetCustomAttribute(customAttribute).Constructor; if (ctorHandle.Kind == HandleKind.MemberReference) // MemberRef { var container = metadataReader.GetMemberReference((MemberReferenceHandle)ctorHandle).Parent; var name = metadataReader.GetTypeReference((TypeReferenceHandle)container).Name; return metadataReader.GetString(name); } else { Assert.True(false, "not impl"); return null; } }
/// <summary>Load our fields from the metadata of the file as represented by the provided metadata reader.</summary> /// <param name="metadataReader">The metadata reader for the CLI file this represents.</param> private void LoadManagedAssemblyMetadata(MetadataReader metadataReader) { AssemblyDefinition assemblyDefinition = metadataReader.GetAssemblyDefinition(); // Set the internal and original names based on the file name. _internalName = _originalFilename = Path.GetFileName(_fileName); // Set the product version based on the assembly's version (this may be overwritten // later in the method). Version productVersion = assemblyDefinition.Version; _productVersion = productVersion.ToString(); _productMajor = productVersion.Major; _productMinor = productVersion.Minor; _productBuild = productVersion.Build != -1 ? productVersion.Build : 0; _productPrivate = productVersion.Revision != -1 ? productVersion.Revision : 0; // "Language Neutral" is used on Win32 for unknown language identifiers. _language = "Language Neutral"; // Set other fields to default values in case they're not overwritten by attributes _companyName = string.Empty; _comments = string.Empty; _fileDescription = " "; // this is what the managed compiler outputs when value isn't set _fileVersion = string.Empty; _legalCopyright = " "; // this is what the managed compiler outputs when value isn't set _legalTrademarks = string.Empty; _productName = string.Empty; _privateBuild = string.Empty; _specialBuild = string.Empty; // Be explicit about initialization to suppress warning about fields not being set _isDebug = false; _isPatched = false; _isPreRelease = false; _isPrivateBuild = false; _isSpecialBuild = false; bool sawAssemblyInformationalVersionAttribute = false; // Everything else is parsed from assembly attributes MetadataStringComparer comparer = metadataReader.StringComparer; foreach (CustomAttributeHandle attrHandle in assemblyDefinition.GetCustomAttributes()) { CustomAttribute attr = metadataReader.GetCustomAttribute(attrHandle); StringHandle typeNamespaceHandle = default(StringHandle), typeNameHandle = default(StringHandle); if (TryGetAttributeName(metadataReader, attr, out typeNamespaceHandle, out typeNameHandle) && comparer.Equals(typeNamespaceHandle, "System.Reflection")) { if (comparer.Equals(typeNameHandle, "AssemblyCompanyAttribute")) { GetStringAttributeArgumentValue(metadataReader, attr, ref _companyName); } else if (comparer.Equals(typeNameHandle, "AssemblyCopyrightAttribute")) { GetStringAttributeArgumentValue(metadataReader, attr, ref _legalCopyright); } else if (comparer.Equals(typeNameHandle, "AssemblyDescriptionAttribute")) { GetStringAttributeArgumentValue(metadataReader, attr, ref _comments); } else if (comparer.Equals(typeNameHandle, "AssemblyFileVersionAttribute")) { GetStringAttributeArgumentValue(metadataReader, attr, ref _fileVersion); ParseVersion(_fileVersion, out _fileMajor, out _fileMinor, out _fileBuild, out _filePrivate); } else if (comparer.Equals(typeNameHandle, "AssemblyInformationalVersionAttribute")) { GetStringAttributeArgumentValue(metadataReader, attr, ref _productVersion); ParseVersion(_productVersion, out _productMajor, out _productMinor, out _productBuild, out _productPrivate); sawAssemblyInformationalVersionAttribute = true; } else if (comparer.Equals(typeNameHandle, "AssemblyProductAttribute")) { GetStringAttributeArgumentValue(metadataReader, attr, ref _productName); } else if (comparer.Equals(typeNameHandle, "AssemblyTrademarkAttribute")) { GetStringAttributeArgumentValue(metadataReader, attr, ref _legalTrademarks); } else if (comparer.Equals(typeNameHandle, "AssemblyTitleAttribute")) { GetStringAttributeArgumentValue(metadataReader, attr, ref _fileDescription); } } } // When the managed compiler sees an [AssemblyVersion(...)] attribute, it uses that to set // both the assembly version and the product version in the Win32 resources. If it doesn't // see an [AssemblyVersion(...)], then it sets the assembly version to 0.0.0.0, however it // sets the product version in the Win32 resources to whatever was defined in the // [AssemblyFileVersionAttribute(...)] if there was one (unless there is an AssemblyInformationalVersionAttribute, // in which case it always uses that for the product version). Without parsing the Win32 resources, // we can't differentiate these two cases, so given the rarity of explicitly setting an // assembly's version number to 0.0.0.0, we assume that if it is 0.0.0.0 then the attribute // wasn't specified and we use the file version. if (!sawAssemblyInformationalVersionAttribute && _productVersion == "0.0.0.0") { _productVersion = _fileVersion; _productMajor = _fileMajor; _productMinor = _fileMinor; _productBuild = _fileBuild; _productPrivate = _filePrivate; } }
public static IType ApplyAttributesToType( IType inputType, ICompilation compilation, SRM.CustomAttributeHandleCollection?attributes, SRM.MetadataReader metadata, TypeSystemOptions options, bool typeChildrenOnly = false) { bool useDynamicType = (options & TypeSystemOptions.Dynamic) != 0; bool useTupleTypes = (options & TypeSystemOptions.Tuple) != 0; bool hasDynamicAttribute = false; bool[] dynamicAttributeData = null; string[] tupleElementNames = null; if (attributes != null && (useDynamicType || useTupleTypes)) { foreach (var attrHandle in attributes.Value) { var attr = metadata.GetCustomAttribute(attrHandle); var attrType = attr.GetAttributeType(metadata); if (useDynamicType && attrType.IsKnownType(metadata, KnownAttribute.Dynamic)) { hasDynamicAttribute = true; var ctor = attr.DecodeValue(Metadata.MetadataExtensions.minimalCorlibTypeProvider); if (ctor.FixedArguments.Length == 1) { var arg = ctor.FixedArguments[0]; if (arg.Value is ImmutableArray <SRM.CustomAttributeTypedArgument <IType> > values && values.All(v => v.Value is bool)) { dynamicAttributeData = values.SelectArray(v => (bool)v.Value); } } } else if (useTupleTypes && attrType.IsKnownType(metadata, KnownAttribute.TupleElementNames)) { var ctor = attr.DecodeValue(Metadata.MetadataExtensions.minimalCorlibTypeProvider); if (ctor.FixedArguments.Length == 1) { var arg = ctor.FixedArguments[0]; if (arg.Value is ImmutableArray <SRM.CustomAttributeTypedArgument <IType> > values && values.All(v => v.Value is string || v.Value == null)) { tupleElementNames = values.SelectArray(v => (string)v.Value); } } } } } if (hasDynamicAttribute || (options & (TypeSystemOptions.Tuple | TypeSystemOptions.KeepModifiers)) != TypeSystemOptions.KeepModifiers) { var visitor = new ApplyAttributeTypeVisitor( compilation, hasDynamicAttribute, dynamicAttributeData, options, tupleElementNames ); if (typeChildrenOnly) { return(inputType.VisitChildren(visitor)); } else { return(inputType.AcceptVisitor(visitor)); } } else { return(inputType); } }
public static IType ApplyAttributesToType( IType inputType, ICompilation compilation, SRM.CustomAttributeHandleCollection?attributes, SRM.MetadataReader metadata, TypeSystemOptions options, Nullability nullableContext, bool typeChildrenOnly = false) { bool hasDynamicAttribute = false; bool[] dynamicAttributeData = null; bool hasNativeIntegersAttribute = false; bool[] nativeIntegersAttributeData = null; string[] tupleElementNames = null; Nullability nullability; Nullability[] nullableAttributeData = null; if ((options & TypeSystemOptions.NullabilityAnnotations) != 0) { nullability = nullableContext; } else { nullability = Nullability.Oblivious; } const TypeSystemOptions relevantOptions = TypeSystemOptions.Dynamic | TypeSystemOptions.Tuple | TypeSystemOptions.NullabilityAnnotations | TypeSystemOptions.NativeIntegers; if (attributes != null && (options & relevantOptions) != 0) { foreach (var attrHandle in attributes.Value) { var attr = metadata.GetCustomAttribute(attrHandle); var attrType = attr.GetAttributeType(metadata); if ((options & TypeSystemOptions.Dynamic) != 0 && attrType.IsKnownType(metadata, KnownAttribute.Dynamic)) { hasDynamicAttribute = true; var ctor = attr.DecodeValue(Metadata.MetadataExtensions.minimalCorlibTypeProvider); if (ctor.FixedArguments.Length == 1) { var arg = ctor.FixedArguments[0]; if (arg.Value is ImmutableArray <SRM.CustomAttributeTypedArgument <IType> > values && values.All(v => v.Value is bool)) { dynamicAttributeData = values.SelectArray(v => (bool)v.Value); } } } else if ((options & TypeSystemOptions.NativeIntegers) != 0 && attrType.IsKnownType(metadata, KnownAttribute.NativeInteger)) { hasNativeIntegersAttribute = true; var ctor = attr.DecodeValue(Metadata.MetadataExtensions.minimalCorlibTypeProvider); if (ctor.FixedArguments.Length == 1) { var arg = ctor.FixedArguments[0]; if (arg.Value is ImmutableArray <SRM.CustomAttributeTypedArgument <IType> > values && values.All(v => v.Value is bool)) { nativeIntegersAttributeData = values.SelectArray(v => (bool)v.Value); } } } else if ((options & TypeSystemOptions.Tuple) != 0 && attrType.IsKnownType(metadata, KnownAttribute.TupleElementNames)) { var ctor = attr.DecodeValue(Metadata.MetadataExtensions.minimalCorlibTypeProvider); if (ctor.FixedArguments.Length == 1) { var arg = ctor.FixedArguments[0]; if (arg.Value is ImmutableArray <SRM.CustomAttributeTypedArgument <IType> > values && values.All(v => v.Value is string || v.Value == null)) { tupleElementNames = values.SelectArray(v => (string)v.Value); } } } else if ((options & TypeSystemOptions.NullabilityAnnotations) != 0 && attrType.IsKnownType(metadata, KnownAttribute.Nullable)) { var ctor = attr.DecodeValue(Metadata.MetadataExtensions.minimalCorlibTypeProvider); if (ctor.FixedArguments.Length == 1) { var arg = ctor.FixedArguments[0]; if (arg.Value is ImmutableArray <SRM.CustomAttributeTypedArgument <IType> > values && values.All(v => v.Value is byte b && b <= 2)) { nullableAttributeData = values.SelectArray(v => (Nullability)(byte)v.Value); } else if (arg.Value is byte b && b <= 2) { nullability = (Nullability)b; } } } } }