private void Unmark(TypeSignature typeSig)
        {
            var type = typeSig.Resolve(_assembly) as BuildType;

            if (type == null)
            {
                return;
            }

            type.Rename = false;
        }
        private void WriteEnumValue(TypeSignature argumentType, object element)
        {
            // Try resolve enum and get enum underlying type.
            var enumTypeDef = argumentType.Resolve();

            if (enumTypeDef != null && enumTypeDef.IsEnum)
            {
                WriteElement(enumTypeDef.GetEnumUnderlyingType(), element);
                return;
            }

            // Enum arguments can never be null.
            if (element is null)
            {
                _context.DiagnosticBag.RegisterException(
                    new NotSupportedException($"The element of the enum-typed argument is null."));

                // Assume 0 if it is.
                element = 0;
            }

            // Try inferring the enum type from the argument value.
            var corLibTypeFactory = argumentType.Module.CorLibTypeFactory;

            argumentType = Type.GetTypeCode(element.GetType()) switch
            {
                TypeCode.Boolean => corLibTypeFactory.Boolean,
                TypeCode.Byte => corLibTypeFactory.Byte,
                TypeCode.Char => corLibTypeFactory.Char,
                TypeCode.Int16 => corLibTypeFactory.Int16,
                TypeCode.Int32 => corLibTypeFactory.Int32,
                TypeCode.Int64 => corLibTypeFactory.Int64,
                TypeCode.SByte => corLibTypeFactory.SByte,
                TypeCode.UInt16 => corLibTypeFactory.UInt16,
                TypeCode.UInt32 => corLibTypeFactory.UInt32,
                TypeCode.UInt64 => corLibTypeFactory.UInt64,
                _ => _context.DiagnosticBag.RegisterExceptionAndReturnDefault <TypeSignature>(
                    new NotSupportedException($"The element of the enum-typed argument is not of an integral type."))
            };

            if (argumentType != null)
            {
                WriteElement(argumentType, element);
            }
        }
Esempio n. 3
0
        private void WriteValue(IBinaryStreamWriter writer, TypeSignature argumentType, ITypeCodedIndexProvider provider, object value)
        {
            if (argumentType.IsTypeOf("System", "Type"))
            {
                writer.WriteSerString(TypeNameBuilder.GetAssemblyQualifiedName((TypeSignature)value));
                return;
            }

            switch (argumentType.ElementType)
            {
            case ElementType.Boolean:
                writer.WriteByte((byte)((bool)value ? 1 : 0));
                break;

            case ElementType.Char:
                writer.WriteUInt16((char)value);
                break;

            case ElementType.I1:
                writer.WriteSByte((sbyte)value);
                break;

            case ElementType.U1:
                writer.WriteByte((byte)value);
                break;

            case ElementType.I2:
                writer.WriteInt16((short)value);
                break;

            case ElementType.U2:
                writer.WriteUInt16((ushort)value);
                break;

            case ElementType.I4:
                writer.WriteInt32((int)value);
                break;

            case ElementType.U4:
                writer.WriteUInt32((uint)value);
                break;

            case ElementType.I8:
                writer.WriteInt64((long)value);
                break;

            case ElementType.U8:
                writer.WriteUInt64((ulong)value);
                break;

            case ElementType.R4:
                writer.WriteSingle((float)value);
                break;

            case ElementType.R8:
                writer.WriteDouble((double)value);
                break;

            case ElementType.String:
                writer.WriteSerString(value as string);
                break;

            case ElementType.Object:
                var valueType    = value.GetType();
                var innerTypeSig = argumentType.Module.CorLibTypeFactory.FromName(valueType.Namespace, valueType.Name);
                TypeSignature.WriteFieldOrPropType(writer, innerTypeSig);
                WriteValue(writer, innerTypeSig, provider, value);
                break;

            case ElementType.Class:
            case ElementType.Enum:
            case ElementType.ValueType:
                var enumTypeDef = argumentType.Resolve();
                if (enumTypeDef != null && enumTypeDef.IsEnum)
                {
                    WriteValue(writer, enumTypeDef.GetEnumUnderlyingType(), provider, Value);
                }
                else
                {
                    throw new NotImplementedException();
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        private void Process(TypeSignature typeSig, Module module)
        {
            switch (typeSig.ElementCode)
            {
            case TypeElementCode.Array:
            {
                Process(typeSig.ElementType, module);
            }
            break;

            case TypeElementCode.ByRef:
            {
                Process(typeSig.ElementType, module);
            }
            break;

            case TypeElementCode.CustomModifier:
            {
                CustomModifierType modifierType;
                var modifier = typeSig.GetCustomModifier(out modifierType);
                Process(modifier, module);
                Process(typeSig.ElementType, module);
            }
            break;

            case TypeElementCode.FunctionPointer:
            {
                Process(typeSig.GetFunctionPointer(), module);
            }
            break;

            case TypeElementCode.GenericParameter:
                break;

            case TypeElementCode.GenericType:
            {
                Process(typeSig.DeclaringType, module);
                Process(typeSig.GenericArguments, module);
            }
            break;

            case TypeElementCode.Pinned:
            {
                Process(typeSig.ElementType, module);
            }
            break;

            case TypeElementCode.Pointer:
            {
                Process(typeSig.ElementType, module);
            }
            break;

            case TypeElementCode.DeclaringType:
            {
                var type = typeSig.Resolve(module, true).DeclaringType as BuildType;
                if (type != null)
                {
                    UnstripAndEnqueue(type);
                }
            }
            break;

            default:
                throw new InvalidOperationException();
            }
        }