GetEnumUnderlyingType() public method

public GetEnumUnderlyingType ( ) : Type
return Type
コード例 #1
0
        public static Type GetEnumEnderlyingType(Type type)
        {
#if FEAT_IKVM
            return(type.GetEnumUnderlyingType());
#else
            return(Enum.GetUnderlyingType(type));
#endif
        }
コード例 #2
0
        void GenerateEnum(ProcessedType type)
        {
            Type t               = type.Type;
            var  managed_name    = type.ObjCName;
            var  underlying_type = t.GetEnumUnderlyingType();
            var  base_type       = NameGenerator.GetTypeName(underlying_type);

            // it's nicer to expose flags as unsigned integers - but .NET defaults to `int`
            bool flags = t.HasCustomAttribute("System", "FlagsAttribute");

            if (flags)
            {
                switch (Type.GetTypeCode(underlying_type))
                {
                case TypeCode.SByte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                    base_type = "unsigned " + base_type;
                    break;
                }
            }

            var macro = flags ? "NS_OPTIONS" : "NS_ENUM";

            headers.WriteLine($"/** Enumeration {managed_name}");
            headers.WriteLine($" *  Corresponding .NET Qualified Name: `{t.AssemblyQualifiedName}`");
            headers.WriteLine(" */");
            headers.WriteLine($"typedef {macro}({base_type}, {managed_name}) {{");
            headers.Indent++;
            foreach (var name in t.GetEnumNames())
            {
                var value = t.GetField(name).GetRawConstantValue();
                headers.Write($"{managed_name}{name} = ");
                if (flags)
                {
                    headers.Write($"0x{value:x}");
                }
                else
                {
                    headers.Write(value);
                }
                headers.WriteLine(',');
            }
            headers.Indent--;
            headers.WriteLine("};");
            headers.WriteLine();
        }
コード例 #3
0
        void GenerateEnum(Type t)
        {
            var managed_name    = GetObjCName(t);
            var underlying_type = t.GetEnumUnderlyingType();
            var base_type       = GetTypeName(underlying_type);

            // it's nicer to expose flags as unsigned integers - but .NET defaults to `int`
            bool flags = t.HasCustomAttribute("System", "FlagsAttribute");

            if (flags)
            {
                switch (Type.GetTypeCode(underlying_type))
                {
                case TypeCode.SByte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                    base_type = "unsigned " + base_type;
                    break;
                }
            }

            var macro = flags ? "NS_OPTIONS" : "NS_ENUM";

            headers.WriteLine($"typedef {macro}({base_type}, {managed_name}) {{");
            foreach (var name in t.GetEnumNames())
            {
                var value = t.GetField(name).GetRawConstantValue();
                headers.Write($"\t{managed_name}{name} = ");
                if (flags)
                {
                    headers.Write($"0x{value:x}");
                }
                else
                {
                    headers.Write(value);
                }
                headers.WriteLine(',');
            }
            headers.WriteLine("};");
            headers.WriteLine();
        }
コード例 #4
0
ファイル: outline.cs プロジェクト: samcf111/unityMono5.5.0
        public void OutlineType()
        {
            bool first;

            OutlineAttributes();
            o.Write(GetTypeVisibility(t));

            if (t.IsClass && !t.IsSubclassOf(type_multicast_delegate))
            {
                if (t.IsSealed)
                {
                    o.Write(t.IsAbstract ? " static" : " sealed");
                }
                else if (t.IsAbstract)
                {
                    o.Write(" abstract");
                }
            }

            o.Write(" ");
            o.Write(GetTypeKind(t));
            o.Write(" ");

            Type [] interfaces = (Type [])Comparer.Sort(TypeGetInterfaces(t, declared_only));
            Type    parent     = t.BaseType;

            if (t.IsSubclassOf(type_multicast_delegate))
            {
                MethodInfo method;

                method = t.GetMethod("Invoke");

                o.Write(FormatType(method.ReturnType));
                o.Write(" ");
                o.Write(GetTypeName(t));
                o.Write(" (");
                OutlineParams(method.GetParameters());
                o.Write(")");

                WriteGenericConstraints(t.GetGenericArguments());

                o.WriteLine(";");
                return;
            }

            o.Write(GetTypeName(t));
            if (((parent != null && parent != type_object && parent != type_value_type) || interfaces.Length != 0) && !t.IsEnum)
            {
                first = true;
                o.Write(" : ");

                if (parent != null && parent != type_object && parent != type_value_type)
                {
                    o.Write(FormatType(parent));
                    first = false;
                }

                foreach (Type intf in interfaces)
                {
                    if (!first)
                    {
                        o.Write(", ");
                    }
                    first = false;

                    o.Write(FormatType(intf));
                }
            }

            if (t.IsEnum)
            {
                Type underlyingType = t.GetEnumUnderlyingType();
                if (underlyingType != type_int)
                {
                    o.Write(" : {0}", FormatType(underlyingType));
                }
            }
            WriteGenericConstraints(t.GetGenericArguments());
            o.WriteLine(" {");
            o.Indent++;

            if (t.IsEnum)
            {
                bool is_first = true;
                foreach (FieldInfo fi in t.GetFields(BindingFlags.Public | BindingFlags.Static))
                {
                    if (!is_first)
                    {
                        o.WriteLine(",");
                    }
                    is_first = false;
                    o.Write(fi.Name);
                }
                o.WriteLine();
                o.Indent--; o.WriteLine("}");
                return;
            }

            first = true;

            foreach (ConstructorInfo ci in t.GetConstructors(DefaultFlags))
            {
                if (!ShowMember(ci))
                {
                    continue;
                }

                if (first)
                {
                    o.WriteLine();
                }
                first = false;

                OutlineMemberAttribute(ci);
                OutlineConstructor(ci);

                o.WriteLine();
            }


            first = true;

            foreach (MethodInfo m in Comparer.Sort(t.GetMethods(DefaultFlags)))
            {
                if (!ShowMember(m))
                {
                    continue;
                }

                if ((m.Attributes & MethodAttributes.SpecialName) != 0)
                {
                    continue;
                }

                if (first)
                {
                    o.WriteLine();
                }
                first = false;

                OutlineMemberAttribute(m);
                OutlineMethod(m);

                o.WriteLine();
            }

            first = true;

            foreach (MethodInfo m in t.GetMethods(DefaultFlags))
            {
                if (!ShowMember(m))
                {
                    continue;
                }

                if ((m.Attributes & MethodAttributes.SpecialName) == 0)
                {
                    continue;
                }
                if (!(m.Name.StartsWith("op_")))
                {
                    continue;
                }

                if (first)
                {
                    o.WriteLine();
                }
                first = false;

                OutlineMemberAttribute(m);
                OutlineOperator(m);

                o.WriteLine();
            }

            first = true;

            foreach (PropertyInfo pi in Comparer.Sort(t.GetProperties(DefaultFlags)))
            {
                if (!((pi.CanRead && ShowMember(pi.GetGetMethod(true))) ||
                      (pi.CanWrite && ShowMember(pi.GetSetMethod(true)))))
                {
                    continue;
                }

                if (first)
                {
                    o.WriteLine();
                }
                first = false;

                OutlineMemberAttribute(pi);
                OutlineProperty(pi);

                o.WriteLine();
            }

            first = true;

            foreach (FieldInfo fi in t.GetFields(DefaultFlags))
            {
                if (!ShowMember(fi))
                {
                    continue;
                }

                if (first)
                {
                    o.WriteLine();
                }
                first = false;

                OutlineMemberAttribute(fi);
                OutlineField(fi);

                o.WriteLine();
            }

            first = true;

            foreach (EventInfo ei in Comparer.Sort(t.GetEvents(DefaultFlags)))
            {
                if (!ShowMember(ei.GetAddMethod(true)))
                {
                    continue;
                }

                if (first)
                {
                    o.WriteLine();
                }
                first = false;

                OutlineMemberAttribute(ei);
                OutlineEvent(ei);

                o.WriteLine();
            }

            first = true;

            foreach (Type ntype in Comparer.Sort(t.GetNestedTypes(DefaultFlags)))
            {
                if (!ShowMember(ntype))
                {
                    continue;
                }

                if (first)
                {
                    o.WriteLine();
                }
                first = false;

#if STATIC
                new Outline(universe, mscorlib, ntype, o, declared_only, show_private, filter_obsolete).OutlineType();
#else
                new Outline(ntype, o, declared_only, show_private, filter_obsolete).OutlineType();
#endif
            }

            o.Indent--; o.WriteLine("}");
        }
コード例 #5
0
 void ReadFixedArg(StringBuilder sb, ByteReader br, Type type, bool arrayElement = false)
 {
     if (type.IsArray)
     {
         int length = br.ReadInt32();
         if (length == -1 && compat == CompatLevel.None)
         {
             sb.Append("nullref");
         }
         else if (length == 0 && compat != CompatLevel.None)
         {
             throw new IKVM.Reflection.BadImageFormatException();
         }
         else
         {
             Type elementType = type.GetElementType();
             AppendCATypeName(sb, elementType, null);
             sb.AppendFormat("[{0}](", length);
             for (int i = 0; i < length; i++)
             {
                 if (i != 0)
                 {
                     sb.Append(' ');
                 }
                 if (elementType == typeofSystemObject)
                 {
                     string typeName;
                     ReadFixedArg(sb, br, ReadFieldOrPropType(sb, br, out typeName), false);
                 }
                 else
                 {
                     ReadFixedArg(sb, br, elementType, true);
                 }
             }
             sb.Append(')');
         }
     }
     else if (type.FullName == "System.Type" && type.Assembly.GetName().Name == "mscorlib")
     {
         if (!arrayElement)
         {
             AppendCATypeName(sb, type, null);
             sb.Append('(');
         }
         string typeName;
         var type1 = ReadType(br, out typeName);
         if (type1 == null)
         {
             if (typeName == null)
             {
                 sb.Append("nullref");
             }
             else
             {
                 sb.Append("class ").Append(QuoteIdentifier(typeName, true));
             }
         }
         else
         {
             AppendTypeName(sb, type1, typeName, compat != CompatLevel.None && IsNestedTypeWithNamespace(type1));
         }
         if (!arrayElement)
         {
             sb.Append(')');
         }
     }
     else if (type.Assembly == mscorlib)
     {
         if (!arrayElement)
         {
             AppendCATypeName(sb, type, null);
             sb.Append('(');
         }
         if (type == typeofSystemBoolean)
         {
             sb.Append(br.ReadByte() == 0 ? "false" : "true");
         }
         else if (type == typeofSystemByte)
         {
             sb.Append(br.ReadByte());
         }
         else if (type == typeofSystemSByte)
         {
             sb.Append(br.ReadSByte());
         }
         else if (type == typeofSystemChar)
         {
             sb.AppendFormat("0x{0:X4}", (int)br.ReadChar());
         }
         else if (type == typeofSystemInt16)
         {
             sb.Append(br.ReadInt16());
         }
         else if (type == typeofSystemUInt16)
         {
             sb.Append(br.ReadUInt16());
         }
         else if (type == typeofSystemInt32)
         {
             sb.Append(br.ReadInt32());
         }
         else if (type == typeofSystemUInt32)
         {
             sb.Append(br.ReadInt32());
         }
         else if (type == typeofSystemInt64)
         {
             sb.Append(br.ReadInt64());
         }
         else if (type == typeofSystemUInt64)
         {
             sb.Append(br.ReadInt64());
         }
         else if (type == typeofSystemSingle)
         {
             sb.Append(ToString(br.ReadSingle(), true));
         }
         else if (type == typeofSystemDouble)
         {
             sb.Append(ToString(br.ReadDouble(), true));
         }
         else if (type == typeofSystemString)
         {
             var str = br.ReadString();
             if (str == null)
             {
                 sb.Append("nullref");
             }
             else
             {
                 if (compat != CompatLevel.None)
                 {
                     int pos = str.IndexOf((char)0);
                     if (pos != -1)
                     {
                         str = str.Substring(0, pos);
                     }
                 }
                 sb.Append(QuoteIdentifier(str, true));
             }
         }
         else if (type == typeofSystemObject)
         {
             string typeName;
             ReadFixedArg(sb, br, ReadFieldOrPropType(sb, br, out typeName));
         }
         else
         {
             throw new NotImplementedException(type.FullName);
         }
         if (!arrayElement)
         {
             sb.Append(')');
         }
     }
     else if (type.__IsMissing || (compat != CompatLevel.None && typerefs.Contains(type)))
     {
         // ildasm actually tries to load the assembly, but we can't do that, so we cheat by having
         // a list of 'known' enum types
         if (type.Assembly.GetName().Name == "mscorlib")
         {
             switch (type.FullName)
             {
                 case "System.AttributeTargets":
                 case "System.Runtime.ConstrainedExecution.Consistency":
                 case "System.Runtime.ConstrainedExecution.Cer":
                 case "System.Security.Permissions.SecurityAction":
                 case "System.Security.Permissions.SecurityPermissionFlag":
                 case "System.Runtime.Versioning.ResourceScope":
                 case "System.Runtime.InteropServices.CallingConvention":
                 case "System.Runtime.InteropServices.CharSet":
                     ReadFixedArg(sb, br, typeofSystemInt32);
                     return;
                 case "System.Security.SecurityRuleSet":
                     if (compat != CompatLevel.V20)
                     {
                         ReadFixedArg(sb, br, typeofSystemByte);
                         return;
                     }
                     break;
                 case "System.Diagnostics.Tracing.EventLevel":
                 case "System.Diagnostics.Tracing.EventTask":
                 case "System.Diagnostics.Tracing.EventOpcode":
                     if (compat != CompatLevel.V20 && compat != CompatLevel.V40)
                     {
                         ReadFixedArg(sb, br, typeofSystemInt32);
                         return;
                     }
                     break;
                 case "System.Type":
                     sb.Append("type(");
                     string typeName;
                     AppendTypeName(sb, ReadType(br, out typeName), typeName);
                     sb.Append(")");
                     return;
             }
         }
         switch (br.Length)
         {
             case 1:
                 if (compat != CompatLevel.None)
                 {
                     // ildasm uses bool (???) as the underlying type in this case
                     sb.AppendFormat("bool({0})", br.ReadByte() == 0 ? "false" : "true");
                 }
                 else
                 {
                     // just guess that the enum has int8 as the underlying type
                     sb.AppendFormat("int8({0})", br.ReadSByte());
                 }
                 break;
             case 2:
                 // just guess that the enum has int16 as the underlying type
                 sb.AppendFormat("int16({0})", br.ReadInt16());
                 break;
             case 4:
                 // just guess that the enum has int32 as the underlying type
                 sb.AppendFormat("int32({0})", br.ReadInt32());
                 break;
             case 8:
                 // just guess that the enum has int64 as the underlying type
                 sb.AppendFormat("int64({0})", br.ReadInt64());
                 break;
             default:
                 throw new IKVM.Reflection.BadImageFormatException();
         }
     }
     else if (type.IsEnum)
     {
         ReadFixedArg(sb, br, type.GetEnumUnderlyingType(), arrayElement);
     }
     else
     {
         throw new NotImplementedException(type.FullName);
     }
 }
コード例 #6
0
 public static Type GetUnderlyingEnumType(Type type)
 {
     return(type.GetEnumUnderlyingType());
 }
コード例 #7
0
ファイル: CABlob.cs プロジェクト: blinds52/ikdasm
        void ReadFixedArg(StringBuilder sb, ByteReader br, Type type, bool arrayElement = false)
        {
            if (type.IsArray)
            {
                int length = br.ReadInt32();
                if (length == -1 && compat == CompatLevel.None)
                {
                    sb.Append("nullref");
                }
                else if (length == 0 && compat != CompatLevel.None)
                {
                    throw new IKVM.Reflection.BadImageFormatException();
                }
                else
                {
                    Type elementType = type.GetElementType();
                    AppendCATypeName(sb, elementType, null);
                    sb.AppendFormat("[{0}](", length);
                    for (int i = 0; i < length; i++)
                    {
                        if (i != 0)
                        {
                            sb.Append(' ');
                        }
                        if (elementType == typeofSystemObject)
                        {
                            string typeName;
                            ReadFixedArg(sb, br, ReadFieldOrPropType(sb, br, out typeName), false);
                        }
                        else
                        {
                            ReadFixedArg(sb, br, elementType, true);
                        }
                    }
                    sb.Append(')');
                }
            }
            else if (type.FullName == "System.Type" && type.Assembly.GetName().Name == "mscorlib")
            {
                if (!arrayElement)
                {
                    AppendCATypeName(sb, type, null);
                    sb.Append('(');
                }
                string typeName;
                var    type1 = ReadType(br, out typeName);
                if (type1 == null)
                {
                    if (typeName == null)
                    {
                        sb.Append("nullref");
                    }
                    else
                    {
                        sb.Append("class ").Append(QuoteIdentifier(typeName, true));
                    }
                }
                else
                {
                    AppendTypeName(sb, type1, typeName, compat != CompatLevel.None && IsNestedTypeWithNamespace(type1));
                }
                if (!arrayElement)
                {
                    sb.Append(')');
                }
            }
            else if (type.Assembly == mscorlib)
            {
                if (!arrayElement)
                {
                    AppendCATypeName(sb, type, null);
                    sb.Append('(');
                }
                if (type == typeofSystemBoolean)
                {
                    sb.Append(br.ReadByte() == 0 ? "false" : "true");
                }
                else if (type == typeofSystemByte)
                {
                    sb.Append(br.ReadByte());
                }
                else if (type == typeofSystemSByte)
                {
                    sb.Append(br.ReadSByte());
                }
                else if (type == typeofSystemChar)
                {
                    sb.AppendFormat("0x{0:X4}", (int)br.ReadChar());
                }
                else if (type == typeofSystemInt16)
                {
                    sb.Append(br.ReadInt16());
                }
                else if (type == typeofSystemUInt16)
                {
                    sb.Append(br.ReadUInt16());
                }
                else if (type == typeofSystemInt32)
                {
                    sb.Append(br.ReadInt32());
                }
                else if (type == typeofSystemUInt32)
                {
                    sb.Append(br.ReadInt32());
                }
                else if (type == typeofSystemInt64)
                {
                    sb.Append(br.ReadInt64());
                }
                else if (type == typeofSystemUInt64)
                {
                    sb.Append(br.ReadInt64());
                }
                else if (type == typeofSystemSingle)
                {
                    sb.Append(ToString(br.ReadSingle(), true));
                }
                else if (type == typeofSystemDouble)
                {
                    sb.Append(ToString(br.ReadDouble(), true));
                }
                else if (type == typeofSystemString)
                {
                    var str = br.ReadString();
                    if (str == null)
                    {
                        sb.Append("nullref");
                    }
                    else
                    {
                        if (compat != CompatLevel.None)
                        {
                            int pos = str.IndexOf((char)0);
                            if (pos != -1)
                            {
                                str = str.Substring(0, pos);
                            }
                        }
                        sb.Append(QuoteIdentifier(str, true));
                    }
                }
                else if (type == typeofSystemObject)
                {
                    string typeName;
                    ReadFixedArg(sb, br, ReadFieldOrPropType(sb, br, out typeName));
                }
                else
                {
                    throw new NotImplementedException(type.FullName);
                }
                if (!arrayElement)
                {
                    sb.Append(')');
                }
            }
            else if (type.__IsMissing || (compat != CompatLevel.None && typerefs.Contains(type)))
            {
                // ildasm actually tries to load the assembly, but we can't do that, so we cheat by having
                // a list of 'known' enum types
                if (type.Assembly.GetName().Name == "mscorlib")
                {
                    switch (type.FullName)
                    {
                    case "System.AttributeTargets":
                    case "System.Runtime.ConstrainedExecution.Consistency":
                    case "System.Runtime.ConstrainedExecution.Cer":
                    case "System.Security.Permissions.SecurityAction":
                    case "System.Security.Permissions.SecurityPermissionFlag":
                    case "System.Runtime.Versioning.ResourceScope":
                    case "System.Runtime.InteropServices.CallingConvention":
                    case "System.Runtime.InteropServices.CharSet":
                        ReadFixedArg(sb, br, typeofSystemInt32);
                        return;

                    case "System.Security.SecurityRuleSet":
                        if (compat != CompatLevel.V20)
                        {
                            ReadFixedArg(sb, br, typeofSystemByte);
                            return;
                        }
                        break;

                    case "System.Diagnostics.Tracing.EventLevel":
                    case "System.Diagnostics.Tracing.EventTask":
                    case "System.Diagnostics.Tracing.EventOpcode":
                        if (compat != CompatLevel.V20 && compat != CompatLevel.V40)
                        {
                            ReadFixedArg(sb, br, typeofSystemInt32);
                            return;
                        }
                        break;

                    case "System.Type":
                        sb.Append("type(");
                        string typeName;
                        AppendTypeName(sb, ReadType(br, out typeName), typeName);
                        sb.Append(")");
                        return;
                    }
                }
                switch (br.Length)
                {
                case 1:
                    if (compat != CompatLevel.None)
                    {
                        // ildasm uses bool (???) as the underlying type in this case
                        sb.AppendFormat("bool({0})", br.ReadByte() == 0 ? "false" : "true");
                    }
                    else
                    {
                        // just guess that the enum has int8 as the underlying type
                        sb.AppendFormat("int8({0})", br.ReadSByte());
                    }
                    break;

                case 2:
                    // just guess that the enum has int16 as the underlying type
                    sb.AppendFormat("int16({0})", br.ReadInt16());
                    break;

                case 4:
                    // just guess that the enum has int32 as the underlying type
                    sb.AppendFormat("int32({0})", br.ReadInt32());
                    break;

                case 8:
                    // just guess that the enum has int64 as the underlying type
                    sb.AppendFormat("int64({0})", br.ReadInt64());
                    break;

                default:
                    throw new IKVM.Reflection.BadImageFormatException();
                }
            }
            else if (type.IsEnum)
            {
                ReadFixedArg(sb, br, type.GetEnumUnderlyingType(), arrayElement);
            }
            else
            {
                throw new NotImplementedException(type.FullName);
            }
        }