src/Microsoft/Windows/ComponentObjectModel/IO/_Library/Metadata/ComponentLibraryMetadataReader.cs
/// <summary>
/// Reads <see cref="Attribute"/> instances from a given <see cref="ComponentAttributeCollection"/> for a specific <see cref="HandleKind"/>
/// </summary>
/// <param name="reader">The <see cref="MetadataReader"/> instance to read from.</param>
/// <param name="customAttributeHandles">The <see cref="ComponentAttributeCollection"/> instance to read from.</param>
/// <param name="handleKind">The <see cref="HandleKind"/> to read.</param>
/// <returns>A <see cref="ComponentAttributeCollection"/> instance, containing <see cref="Attribute"/> instances, as supported and read from the given HandleCollection</returns>
Reads  instances from a given  for a specific
Reads instances from a given for a specific
ComponentAttributeCollection ReadCustomAttributes( MetadataReader reader, CustomAttributeHandleCollection customAttributeHandles, HandleKind handleKind ) {
            ComponentAttributeCollection result = new ComponentAttributeCollection();
            foreach ( CustomAttributeHandle handle in customAttributeHandles ) {
                CustomAttribute customAttribute = reader.GetCustomAttribute(handle);
                if ( customAttribute.Parent.Kind != handleKind ) {
                    continue;
                }
                MemberReference constructorReference = reader.GetMemberReference((MemberReferenceHandle) customAttribute.Constructor);
                TypeReference   typeReference        = reader.GetTypeReference((TypeReferenceHandle) constructorReference.Parent);
                string typeNamespace = reader.GetString(typeReference.Namespace);
                string typeName      = reader.GetString(typeReference.Name);
                string fullTypeName  = typeNamespace + "." + typeName;
                if ( false == _supportedCustomAttributes.TryGetValue( fullTypeName, out Type attributeType ) ) {
                    continue;
                }
                CustomAttributeValue<Type> customAttributeValue = customAttribute.DecodeValue(_customAttributeTypeProvider);
                object[] args = new object[customAttributeValue.FixedArguments.Length];
                for ( int i = 0 ; i < args.Length ; i++ ) {
                    Type argType = customAttributeValue.FixedArguments[i].Type;
                    if ( argType.IsEnum ) {
                        args[i] = Enum.Parse( argType, customAttributeValue.FixedArguments[i].Value.ToString() );
                        continue;
                    }
                    if ( argType.IsArray ) {
                        ImmutableArray<CustomAttributeTypedArgument<Type>> source = (ImmutableArray<CustomAttributeTypedArgument<Type>>)customAttributeValue.FixedArguments[i].Value;
                        object[] target = (object[])Array.CreateInstance( argType.GetElementType(), source.Length );
                        for ( int j = 0 ; i < target.Length ; j++ ) {
                            target[j] = source[j].Value;
                        }
                        args[i] = target;
                        continue;
                    }
                    args[i] = customAttributeValue.FixedArguments[i].Value;
                }
                Attribute attribute = (Attribute)Activator.CreateInstance( attributeType, args );
                for ( int i = 0 ; i < customAttributeValue.NamedArguments.Length ; i++ ) {
                    if ( CustomAttributeNamedArgumentKind.Property == customAttributeValue.NamedArguments[i].Kind ) {
                        PropertyInfo propertyInfo = attributeType.GetProperty(customAttributeValue.NamedArguments[i].Name);
                        propertyInfo.SetValue( attribute, customAttributeValue.NamedArguments[i].Value );
                        continue;
                    }
                    if ( CustomAttributeNamedArgumentKind.Field == customAttributeValue.NamedArguments[i].Kind ) {
                        FieldInfo fieldInfo = attributeType.GetField(customAttributeValue.NamedArguments[i].Name);
                        fieldInfo.SetValue( attribute, customAttributeValue.NamedArguments[i].Value );
                        continue;
                    }
                }
                result.Add( attribute );
            }
            return result;
        }
ComponentAttributeCollection ReadCustomAttributes ( MetadataReader reader , CustomAttributeHandleCollection customAttributeHandles , HandleKind handleKind ) { ComponentAttributeCollection result = new ComponentAttributeCollection ( ) ; foreach ( CustomAttributeHandle handle in customAttributeHandles ) { CustomAttribute customAttribute = reader . GetCustomAttribute ( handle ) ; if ( customAttribute . Parent . Kind != handleKind ) { continue ; } MemberReference constructorReference = reader . GetMemberReference ( ( MemberReferenceHandle ) customAttribute . Constructor ) ; TypeReference typeReference = reader . GetTypeReference ( ( TypeReferenceHandle ) constructorReference . Parent ) ; string typeNamespace = reader . GetString ( typeReference . Namespace ) ; string typeName = reader . GetString ( typeReference . Name ) ; string fullTypeName = typeNamespace + " " + typeName ; if ( false == _supportedCustomAttributes . TryGetValue ( fullTypeName , out Type attributeType ) ) { continue ; } CustomAttributeValue < Type > customAttributeValue = customAttribute . DecodeValue ( _customAttributeTypeProvider ) ; object [ ] args = new object [ customAttributeValue . FixedArguments . Length ] ; for ( int i = 0 ; i < args . Length ; i ++ ) { Type argType = customAttributeValue . FixedArguments [ i ] . Type ; if ( argType . IsEnum ) { args [ i ] = Enum . Parse ( argType , customAttributeValue . FixedArguments [ i ] . Value . ToString ( ) ) ; continue ; } if ( argType . IsArray ) { ImmutableArray < CustomAttributeTypedArgument < Type > > source = ( ImmutableArray < CustomAttributeTypedArgument < Type > > ) customAttributeValue . FixedArguments [ i ] . Value ; object [ ] target = ( object [ ] ) Array . CreateInstance ( argType . GetElementType ( ) , source . Length ) ; for ( int j = 0 ; i < target . Length ; j ++ ) { target [ j ] = source [ j ] . Value ; } args [ i ] = target ; continue ; } args [ i ] = customAttributeValue . FixedArguments [ i ] . Value ; } Attribute attribute = ( Attribute ) Activator . CreateInstance ( attributeType , args ) ; for ( int i = 0 ; i < customAttributeValue . NamedArguments . Length ; i ++ ) { if ( CustomAttributeNamedArgumentKind . Property == customAttributeValue . NamedArguments [ i ] . Kind ) { PropertyInfo propertyInfo = attributeType . GetProperty ( customAttributeValue . NamedArguments [ i ] . Name ) ; propertyInfo . SetValue ( attribute , customAttributeValue . NamedArguments [ i ] . Value ) ; continue ; } if ( CustomAttributeNamedArgumentKind . Field == customAttributeValue . NamedArguments [ i ] . Kind ) { FieldInfo fieldInfo = attributeType . GetField ( customAttributeValue . NamedArguments [ i ] . Name ) ; fieldInfo . SetValue ( attribute , customAttributeValue . NamedArguments [ i ] . Value ) ; continue ; } } result . Add ( attribute ) ; } return result ; }
Reads  instances from a given  for a specific
Reads instances from a given for a specific
//  Building array of types is only required for Type.GetConstructor - which is not used at the moment. //Type[] types = new Type[attributeValue.FixedArguments.Length]; //for ( int i = 0 ; i < types.Length ; i++ ) { //    types[i] = (Type)attributeValue.FixedArguments[i].Type; //} // Build array of arguments to be passed to Activator.CreateInstance //Type argType = (Type)val.FixedArguments[i].Type; // Assign values to Fields/Properties of attribute instance as passed as named arguments.
A  instance, containing  instances, as supported and read from the given HandleCollection
A instance containing instances as supported and read from the given HandleCollection
reader
MetadataReader
The  instance to read from.
The instance to read from .
customAttributeHandles
CustomAttributeHandleCollection
The  instance to read from.
The instance to read from .
handleKind
HandleKind
The  to read.
The to read .