public static string GetTypeName(TypeReference type)
        {
            if (MonoType.IsCPrimitive(type))
            {
                return(ScriptType.ToCPrimitiveString(type.Name));
            }

            string name = type.Name;

            if (type.IsGenericInstance)
            {
                GenericInstanceType generic = (GenericInstanceType)type;
                int index = name.IndexOf('`');
                name  = name.Substring(0, index);
                name += '<';
                for (int i = 0; i < generic.GenericArguments.Count; i++)
                {
                    TypeReference arg = generic.GenericArguments[i];
                    name += GetArgumentName(arg);
                    if (i < generic.GenericArguments.Count - 1)
                    {
                        name += ", ";
                    }
                }
                name += '>';
            }
            else if (type.HasGenericParameters)
            {
                // TypeReference names parameters as <!0,!1> (!index) but TypeDefinition names them as <T1,T2> (RealParameterName)
                type = type.ResolveOrDefault();
                int index = name.IndexOf('`');
                name  = name.Substring(0, index);
                name += '<';
                for (int i = 0; i < type.GenericParameters.Count; i++)
                {
                    GenericParameter par = type.GenericParameters[i];
                    name += GetArgumentName(par);
                    if (i < type.GenericParameters.Count - 1)
                    {
                        name += ", ";
                    }
                }
                name += '>';
            }
            else if (type.IsArray)
            {
                ArrayType array = (ArrayType)type;
                name = GetTypeName(array.ElementType) + $"[{new string(',', array.Dimensions.Count - 1)}]";
            }
            return(name);
        }
Exemple #2
0
        public static string GetName(TypeReference type)
        {
            if (MonoType.IsCPrimitive(type))
            {
                return(ScriptType.ToCPrimitiveString(type.Name));
            }

            if (type.IsGenericInstance)
            {
                GenericInstanceType generic = (GenericInstanceType)type;
                return(GetGenericInstanceName(generic));
            }
            else if (type.HasGenericParameters)
            {
                return(GetGenericTypeName(type));
            }
            else if (type.IsArray)
            {
                ArrayType array = (ArrayType)type;
                return(GetName(array.ElementType) + $"[{new string(',', array.Dimensions.Count - 1)}]");
            }
            return(type.Name);
        }