Exemple #1
0
        object ConvertEnum(TypeSig elemType, IList <CAArgument> oldList)
        {
            var         td = elemType.ScopeType.ResolveTypeDef();
            ElementType underlyingElemType = ElementType.End;

            if (td != null && td.IsEnum)
            {
                underlyingElemType = td.GetEnumUnderlyingType().RemovePinnedAndModifiers().GetElementType();
            }
            if (!(ElementType.Boolean <= underlyingElemType && underlyingElemType <= ElementType.R8))
            {
                if (oldList.Count > 0 && oldList[0].Value != null)
                {
                    underlyingElemType = ModelUtils.GetElementType(oldList[0].Value.GetType());
                }
            }

            switch (underlyingElemType)
            {
            case ElementType.Boolean:       return(ConvertEnum <bool>(elemType, oldList));

            case ElementType.Char:          return(ConvertEnum <char>(elemType, oldList));

            case ElementType.I1:            return(ConvertEnum <sbyte>(elemType, oldList));

            case ElementType.U1:            return(ConvertEnum <byte>(elemType, oldList));

            case ElementType.I2:            return(ConvertEnum <short>(elemType, oldList));

            case ElementType.U2:            return(ConvertEnum <ushort>(elemType, oldList));

            case ElementType.I4:            return(ConvertEnum <int>(elemType, oldList));

            case ElementType.U4:            return(ConvertEnum <uint>(elemType, oldList));

            case ElementType.I8:            return(ConvertEnum <long>(elemType, oldList));

            case ElementType.U8:            return(ConvertEnum <ulong>(elemType, oldList));

            case ElementType.R4:            return(ConvertEnum <float>(elemType, oldList));

            case ElementType.R8:            return(ConvertEnum <double>(elemType, oldList));
            }
            return(Array.Empty <int>());
        }
Exemple #2
0
        public override string ToString()
        {
            var td = EnumType.ResolveTypeDef();

            if (!(td is null))
            {
                var s = ModelUtils.GetEnumFieldName(td, Value);
                if (!(s is null))
                {
                    return($"{EnumType}.{s}");
                }
            }
            if (!IsArray)
            {
                return($"({(EnumType ?? (object)dnSpy_AsmEditor_Resources.UnknownEnum)}){Value}");
            }

            var list = Value as System.Collections.IList;

            if (list is null)
            {
                return($"({(EnumType ?? (object)dnSpy_AsmEditor_Resources.UnknownEnum)}[])null");
            }

            var sb = new StringBuilder();

            sb.Append($"new {(EnumType ?? (object)dnSpy_AsmEditor_Resources.UnknownEnum)}[] {{");
            for (int i = 0; i < list.Count; i++)
            {
                if (i > 0)
                {
                    sb.Append(',');
                }
                sb.Append(' ');
                var s = ModelUtils.GetEnumFieldName(td, list[i]);
                sb.Append(s ?? (Value is null ? "null" : Value.ToString()));
            }
            sb.Append(" }");
            return(sb.ToString());
        }
Exemple #3
0
        public override string ToString()
        {
            var td = EnumType.ResolveTypeDef();

            if (td != null)
            {
                var s = ModelUtils.GetEnumFieldName(td, Value);
                if (s != null)
                {
                    return(string.Format("{0}.{1}", EnumType, s));
                }
            }
            if (!IsArray)
            {
                return(string.Format("({0}){1}", EnumType == null ? (object)dnSpy_AsmEditor_Resources.UnknownEnum : EnumType, Value));
            }

            var list = Value as System.Collections.IList;

            if (list == null)
            {
                return(string.Format("({0}[])null", EnumType == null ? (object)dnSpy_AsmEditor_Resources.UnknownEnum : EnumType));
            }

            var sb = new StringBuilder();

            sb.Append(string.Format("new {0}[] {{", EnumType == null ? (object)dnSpy_AsmEditor_Resources.UnknownEnum : EnumType));
            for (int i = 0; i < list.Count; i++)
            {
                if (i > 0)
                {
                    sb.Append(',');
                }
                sb.Append(' ');
                var s = ModelUtils.GetEnumFieldName(td, list[i]);
                sb.Append(s ?? (Value == null ? "null" : Value.ToString()));
            }
            sb.Append(" }");
            return(sb.ToString());
        }
Exemple #4
0
        public static string ValueToString(object value, bool addCast)
        {
            if (value == null)
            {
                return("null");
            }

            switch (ModelUtils.GetElementType(value.GetType()))
            {
            case ElementType.Boolean: return(NumberVMUtils.ToString((bool)value));

            case ElementType.Char:  return(NumberVMUtils.ToString((char)value));

            case ElementType.I1:    return(AddCast(NumberVMUtils.ToString((sbyte)value), addCast, value.GetType().FullName));

            case ElementType.U1:    return(AddCast(NumberVMUtils.ToString((byte)value), addCast, value.GetType().FullName));

            case ElementType.I2:    return(AddCast(NumberVMUtils.ToString((short)value), addCast, value.GetType().FullName));

            case ElementType.U2:    return(AddCast(NumberVMUtils.ToString((ushort)value), addCast, value.GetType().FullName));

            case ElementType.I4:    return(AddCast(NumberVMUtils.ToString((int)value), addCast, value.GetType().FullName));

            case ElementType.U4:    return(AddCast(NumberVMUtils.ToString((uint)value), addCast, value.GetType().FullName));

            case ElementType.I8:    return(AddCast(NumberVMUtils.ToString((long)value), addCast, value.GetType().FullName));

            case ElementType.U8:    return(AddCast(NumberVMUtils.ToString((ulong)value), addCast, value.GetType().FullName));

            case ElementType.R4:    return(AddCast(NumberVMUtils.ToString((float)value), addCast, value.GetType().FullName));

            case ElementType.R8:    return(AddCast(NumberVMUtils.ToString((double)value), addCast, value.GetType().FullName));

            case ElementType.String: return(NumberVMUtils.ToString((string)value, true));
            }
            if (value is TypeSig)
            {
                return(string.Format("typeof({0})", value));
            }

            var valueType = value.GetType();

            if (value is IList <bool> )
            {
                return(ArrayToString(value, typeof(bool)));
            }
            if (value is IList <char> )
            {
                return(ArrayToString(value, typeof(char)));
            }
            if (value is IList <sbyte> && valueType != typeof(byte[]))
            {
                return(ArrayToString(value, typeof(sbyte)));
            }
            if (value is IList <short> && valueType != typeof(ushort[]))
            {
                return(ArrayToString(value, typeof(short)));
            }
            if (value is IList <int> && valueType != typeof(uint[]))
            {
                return(ArrayToString(value, typeof(int)));
            }
            if (value is IList <long> && valueType != typeof(ulong[]))
            {
                return(ArrayToString(value, typeof(long)));
            }
            if (value is IList <byte> && valueType != typeof(sbyte[]))
            {
                return(ArrayToString(value, typeof(byte)));
            }
            if (value is IList <ushort> && valueType != typeof(short[]))
            {
                return(ArrayToString(value, typeof(ushort)));
            }
            if (value is IList <uint> && valueType != typeof(int[]))
            {
                return(ArrayToString(value, typeof(uint)));
            }
            if (value is IList <ulong> && valueType != typeof(long[]))
            {
                return(ArrayToString(value, typeof(ulong)));
            }
            if (value is IList <float> )
            {
                return(ArrayToString(value, typeof(float)));
            }
            if (value is IList <double> )
            {
                return(ArrayToString(value, typeof(double)));
            }
            if (value is IList <string> )
            {
                return(ArrayToString(value, typeof(string)));
            }
            if (value is IList <TypeSig> )
            {
                return(ArrayToString(value, typeof(Type)));
            }
            if (value is IList <object> )
            {
                return(ArrayToString(value, typeof(object)));
            }

            return(value.ToString());
        }
Exemple #5
0
        CAArgument CreateCAArgument(TypeSig ownerType, object value)
        {
            if (value == null || value is Null)
            {
                var t = ownerType.RemovePinnedAndModifiers();
                t = t is SZArraySig ? t.Next : t;
                if (t.RemovePinnedAndModifiers().GetElementType() == ElementType.Object)
                {
                    return(new CAArgument(module.CorLibTypes.String, null));
                }
                return(new CAArgument(ownerType, null));
            }

            switch (ModelUtils.GetElementType(value.GetType()))
            {
            case ElementType.Boolean: return(new CAArgument(module.CorLibTypes.Boolean, value));

            case ElementType.Char:  return(new CAArgument(module.CorLibTypes.Char, value));

            case ElementType.I1:    return(new CAArgument(module.CorLibTypes.SByte, value));

            case ElementType.U1:    return(new CAArgument(module.CorLibTypes.Byte, value));

            case ElementType.I2:    return(new CAArgument(module.CorLibTypes.Int16, value));

            case ElementType.U2:    return(new CAArgument(module.CorLibTypes.UInt16, value));

            case ElementType.I4:    return(new CAArgument(module.CorLibTypes.Int32, value));

            case ElementType.U4:    return(new CAArgument(module.CorLibTypes.UInt32, value));

            case ElementType.I8:    return(new CAArgument(module.CorLibTypes.Int64, value));

            case ElementType.U8:    return(new CAArgument(module.CorLibTypes.UInt64, value));

            case ElementType.R4:    return(new CAArgument(module.CorLibTypes.Single, value));

            case ElementType.R8:    return(new CAArgument(module.CorLibTypes.Double, value));

            case ElementType.String: return(new CAArgument(module.CorLibTypes.String, new UTF8String((string)value)));
            }
            if (value is TypeSig)
            {
                return(new CAArgument(new ClassSig(module.CorLibTypes.GetTypeRef("System", "Type")), value));
            }

            if (value is EnumInfo)
            {
                var enumInfo = (EnumInfo)value;
                var enumSig  = enumInfo.EnumType.ToTypeSig();
                if (!enumInfo.IsArray)
                {
                    return(new CAArgument(enumSig, enumInfo.Value));
                }
                var res  = CreateArray(enumSig, enumInfo.Value);
                var list = (IList <CAArgument>)res.Value;
                if (list != null)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        list[i] = new CAArgument(enumSig, list[i].Value);
                    }
                }
                return(res);
            }

            var valueType = value.GetType();

            if (value is IList <bool> )
            {
                return(CreateArray(module.CorLibTypes.Boolean, value));
            }
            if (value is IList <char> )
            {
                return(CreateArray(module.CorLibTypes.Char, value));
            }
            if (value is IList <sbyte> && valueType != typeof(byte[]))
            {
                return(CreateArray(module.CorLibTypes.SByte, value));
            }
            if (value is IList <short> && valueType != typeof(ushort[]))
            {
                return(CreateArray(module.CorLibTypes.Int16, value));
            }
            if (value is IList <int> && valueType != typeof(uint[]))
            {
                return(CreateArray(module.CorLibTypes.Int32, value));
            }
            if (value is IList <long> && valueType != typeof(ulong[]))
            {
                return(CreateArray(module.CorLibTypes.Int64, value));
            }
            if (value is IList <byte> && valueType != typeof(sbyte[]))
            {
                return(CreateArray(module.CorLibTypes.Byte, value));
            }
            if (value is IList <ushort> && valueType != typeof(short[]))
            {
                return(CreateArray(module.CorLibTypes.UInt16, value));
            }
            if (value is IList <uint> && valueType != typeof(int[]))
            {
                return(CreateArray(module.CorLibTypes.UInt32, value));
            }
            if (value is IList <ulong> && valueType != typeof(long[]))
            {
                return(CreateArray(module.CorLibTypes.UInt64, value));
            }
            if (value is IList <float> )
            {
                return(CreateArray(module.CorLibTypes.Single, value));
            }
            if (value is IList <double> )
            {
                return(CreateArray(module.CorLibTypes.Double, value));
            }
            if (value is IList <string> )
            {
                return(CreateArray(module.CorLibTypes.String, value));
            }
            if (value is IList <TypeSig> )
            {
                return(CreateArray(new ClassSig(module.CorLibTypes.GetTypeRef("System", "Type")), value));
            }
            if (value is IList <object> )
            {
                return(CreateArray(module.CorLibTypes.Object, value));
            }

            Debug.Fail(string.Format("Unknown CA arg: {0}, ownerType: {1}", value, ownerType));
            return(new CAArgument());
        }
Exemple #6
0
        object ConvertFromModel(TypeSig valueType, object value)
        {
            var           type = valueType.RemovePinnedAndModifiers();
            var           et   = type.GetElementType();
            ITypeDefOrRef tdr;
            TypeDef       td;

            switch (et)
            {
            case ElementType.Boolean:
            case ElementType.Char:
            case ElementType.I1:
            case ElementType.U1:
            case ElementType.I2:
            case ElementType.U2:
            case ElementType.I4:
            case ElementType.U4:
            case ElementType.I8:
            case ElementType.U8:
            case ElementType.R4:
            case ElementType.R8:
                if (ModelUtils.GetElementType(value?.GetType()) == et)
                {
                    return(value);
                }
                break;

            case ElementType.String:
                if (value == null)
                {
                    return(Null <string> .Instance);
                }
                else if (value is string)
                {
                    return(value);
                }
                else if (value is UTF8String)
                {
                    return(((UTF8String)value).String);
                }
                break;

            case ElementType.ValueType:
            case ElementType.Class:
                tdr = ((ClassOrValueTypeSig)type).TypeDefOrRef;
                if (tdr.IsSystemType())
                {
                    if (value == null)
                    {
                        return(Null <TypeSig> .Instance);
                    }
                    return(value);
                }
                td = tdr.ResolveTypeDef();
                if (td != null && !td.IsEnum)
                {
                    break;
                }
                return(new EnumInfo()
                {
                    EnumType = tdr,
                    Value = value,
                    IsArray = false,
                });

            case ElementType.SZArray:
                var elemType = type.Next.RemovePinnedAndModifiers();
                if (value == null)
                {
                    switch (elemType.GetElementType())
                    {
                    case ElementType.Boolean:       return(Null <bool[]> .Instance);

                    case ElementType.Char:          return(Null <char[]> .Instance);

                    case ElementType.I1:            return(Null <sbyte[]> .Instance);

                    case ElementType.U1:            return(Null <byte[]> .Instance);

                    case ElementType.I2:            return(Null <short[]> .Instance);

                    case ElementType.U2:            return(Null <ushort[]> .Instance);

                    case ElementType.I4:            return(Null <int[]> .Instance);

                    case ElementType.U4:            return(Null <uint[]> .Instance);

                    case ElementType.I8:            return(Null <long[]> .Instance);

                    case ElementType.U8:            return(Null <ulong[]> .Instance);

                    case ElementType.R4:            return(Null <float[]> .Instance);

                    case ElementType.R8:            return(Null <double[]> .Instance);

                    case ElementType.String:        return(Null <string[]> .Instance);

                    case ElementType.Object:        return(Null <object[]> .Instance);

                    case ElementType.ValueType:
                    case ElementType.Class:
                        tdr = ((ClassOrValueTypeSig)elemType).TypeDefOrRef;
                        if (tdr.IsSystemType())
                        {
                            return(Null <Type[]> .Instance);
                        }
                        td = tdr.ResolveTypeDef();
                        if (td != null && !td.IsEnum)
                        {
                            break;
                        }
                        return(EnumInfo.CreateNullArray(tdr));
                    }
                    break;
                }
                var oldList = value as IList <CAArgument>;
                if (oldList == null)
                {
                    break;
                }

                switch (elemType.GetElementType())
                {
                case ElementType.Boolean:       return(ConvertArray <bool>(elemType, oldList));

                case ElementType.Char:          return(ConvertArray <char>(elemType, oldList));

                case ElementType.I1:            return(ConvertArray <sbyte>(elemType, oldList));

                case ElementType.U1:            return(ConvertArray <byte>(elemType, oldList));

                case ElementType.I2:            return(ConvertArray <short>(elemType, oldList));

                case ElementType.U2:            return(ConvertArray <ushort>(elemType, oldList));

                case ElementType.I4:            return(ConvertArray <int>(elemType, oldList));

                case ElementType.U4:            return(ConvertArray <uint>(elemType, oldList));

                case ElementType.I8:            return(ConvertArray <long>(elemType, oldList));

                case ElementType.U8:            return(ConvertArray <ulong>(elemType, oldList));

                case ElementType.R4:            return(ConvertArray <float>(elemType, oldList));

                case ElementType.R8:            return(ConvertArray <double>(elemType, oldList));

                case ElementType.String:        return(ConvertArray <string>(elemType, oldList));

                case ElementType.Object:        return(ConvertArray <object>(elemType, oldList));

                case ElementType.ValueType:
                case ElementType.Class:
                    tdr = ((ClassOrValueTypeSig)elemType).TypeDefOrRef;
                    if (tdr.IsSystemType())
                    {
                        return(ConvertArray <TypeSig>(elemType, oldList));
                    }
                    td = tdr.ResolveTypeDef();
                    if (td != null && !td.IsEnum)
                    {
                        break;
                    }
                    return(ConvertEnum(elemType, oldList));
                }
                break;

            default:
                break;
            }
            return(value);
        }
Exemple #7
0
 static CAArgument CreateCAArgument(TypeSig type) => new CAArgument(type, ModelUtils.GetDefaultValue(type, true));