internal static CustomAttributeNamedArgument Load(IBinaryAccessor accessor, Module module)
        {
            var argument = new CustomAttributeNamedArgument();

            int argumentType = accessor.ReadByte();

            switch (argumentType)
            {
            case Metadata.ElementType.Field:
                argument._type = CustomAttributeNamedArgumentType.Field;
                break;

            case Metadata.ElementType.Property:
                argument._type = CustomAttributeNamedArgumentType.Property;
                break;

            default:
                throw new InvalidDataException();
            }

            bool isArray     = false;
            int  elementType = accessor.ReadByte();

            if (elementType == ElementType.SzArray)
            {
                elementType = accessor.ReadByte();
                isArray     = true;
            }

            switch (elementType)
            {
            case ElementType.Enum:
            {
                string enumTypeName = CustomAttributeHelper.ReadBlobString(accessor);
                enumTypeName   = enumTypeName.TrimEnd(new char[] { '\0' });
                argument._name = CustomAttributeHelper.ReadBlobString(accessor);
                int arrayLength = isArray ? CustomAttributeHelper.ReadArrayLength(accessor) : 1;
                argument._value = CustomAttributeTypedArgument.LoadEnum(accessor, module, enumTypeName, isArray, arrayLength);
            }
            break;

            default:
            {
                argument._name = CustomAttributeHelper.ReadBlobString(accessor);
                int arrayLength = isArray ? CustomAttributeHelper.ReadArrayLength(accessor) : 1;
                argument._value = CustomAttributeTypedArgument.Load(accessor, module, elementType, isArray, arrayLength);
            }
            break;
            }

            return(argument);
        }
        private static CustomAttributeTypedArgument LoadObject(IBinaryAccessor accessor, Module module, bool isArray, int arrayLength)
        {
            var type = TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, module.Assembly);

            if (isArray)
            {
                var value = new CustomAttributeTypedArgument[arrayLength];
                for (int i = 0; i < arrayLength; i++)
                {
                    value[i] = LoadObject(accessor, module);
                }

                return(new CustomAttributeTypedArgument(value, type));
            }
            else
            {
                var value = LoadObject(accessor, module);
                return(new CustomAttributeTypedArgument(value, type));
            }
        }
        public virtual void Visit(CustomAttributeTypedArgument argument)
        {
            Visit(argument.Type);

            object value = argument.Value;

            if (value != null)
            {
                if (value is Signature)
                {
                    Visit((Signature)value);
                }
                else if (value is CustomAttributeTypedArgument)
                {
                    Visit((CustomAttributeTypedArgument)value);
                }
                else if (value is Signature[])
                {
                    var array = (Signature[])value;
                    for (int i = 0; i < array.Length; i++)
                    {
                        var sigValue = array[i];
                        if (sigValue != null)
                        {
                            Visit(sigValue);
                        }
                    }
                }
                else if (value is CustomAttributeTypedArgument[])
                {
                    var array = (CustomAttributeTypedArgument[])value;
                    for (int i = 0; i < array.Length; i++)
                    {
                        Visit(array[i]);
                    }
                }
            }
        }
 public CustomAttributeNamedArgument(string name, CustomAttributeNamedArgumentType type, CustomAttributeTypedArgument value)
 {
     _name  = name;
     _type  = type;
     _value = value;
 }
示例#5
0
        public virtual bool Build(ref CustomAttributeTypedArgument argument)
        {
            bool changed = true;

            var type = argument.Type;

            changed |= Build(ref type);

            object value = argument.Value;

            if (value != null)
            {
                if (value is Signature)
                {
                    var sigValue = (Signature)value;
                    if (Build(ref sigValue))
                    {
                        value   = sigValue;
                        changed = true;
                    }
                }
                else if (value is CustomAttributeTypedArgument)
                {
                    var typedValue = (CustomAttributeTypedArgument)value;
                    if (Build(ref typedValue))
                    {
                        value   = typedValue;
                        changed = true;
                    }
                }
                else if (value is Signature[])
                {
                    var array = (Signature[])value;
                    for (int i = 0; i < array.Length; i++)
                    {
                        var sigValue = array[i];
                        if (sigValue != null)
                        {
                            if (Build(ref sigValue))
                            {
                                array[i] = sigValue;
                                changed  = true;
                            }
                        }
                    }

                    if (changed)
                    {
                        value = array;
                    }
                }
                else if (value is CustomAttributeTypedArgument[])
                {
                    var array = (CustomAttributeTypedArgument[])value;
                    for (int i = 0; i < array.Length; i++)
                    {
                        var typedValue = array[i];
                        if (Build(ref typedValue))
                        {
                            array[i] = typedValue;
                            changed  = true;
                        }
                    }

                    if (changed)
                    {
                        value = array;
                    }
                }
            }

            if (!changed)
            {
                return(false);
            }

            argument = new CustomAttributeTypedArgument(value, type);
            return(true);
        }