Esempio n. 1
0
        static TypeDef FindTypeName(ModuleDef module, string name, StringComparer comparer)
        {
            var sb = new StringBuilder();

            return(module.GetTypes().FirstOrDefault(a => {
                sb.Clear();
                string s1, s2;
                if (comparer.Equals(s1 = FullNameCreator.Name(a, false, sb), name))
                {
                    return true;
                }
                sb.Clear();
                if (comparer.Equals(s2 = FullNameCreator.Name(a, true, sb), name))
                {
                    return true;
                }
                sb.Clear();
                if (comparer.Equals(CleanTypeName(s1), name))
                {
                    return true;
                }
                sb.Clear();
                return comparer.Equals(CleanTypeName(s2), name);
            }));
        }
Esempio n. 2
0
        string GetNameByType(TypeSig type)
        {
            type = type.RemoveModifiers();

            GenericInstSig git = type as GenericInstSig;

            if (git != null && git.GenericType != null && git.GenericArguments.Count == 1 && git.GenericType.TypeDefOrRef.Compare(systemString, nullableString))
            {
                type = ((GenericInstSig)type).GenericArguments[0];
            }

            string name;

            if (type == null)
            {
                return(string.Empty);
            }
            if (type.IsSingleOrMultiDimensionalArray)
            {
                name = "array";
            }
            else if (type.IsPointer || type.IsByRef)
            {
                name = "ptr";
            }
            else
            {
                stringBuilder.Clear();
                if (FullNameCreator.NameSB(type, false, stringBuilder).EndsWith("Exception"))
                {
                    name = "ex";
                }
                else
                {
                    stringBuilder.Clear();
                    if (!typeNameToVariableNameDict.TryGetValue(FullNameCreator.FullName(type, false, null, null, null, stringBuilder), out name))
                    {
                        stringBuilder.Clear();
                        name = FullNameCreator.Name(type, false, stringBuilder);
                        // remove the 'I' for interfaces
                        if (name.Length >= 3 && name[0] == 'I' && char.IsUpper(name[1]) && char.IsLower(name[2]))
                        {
                            name = name.Substring(1);
                        }
                        name = CleanUpVariableName(name);
                    }
                }
            }
            return(name);
        }
Esempio n. 3
0
        public static string CreateDisplayName(IMethod method, bool fullName = false)
        {
            var sb = new StringBuilder();

            if (fullName)
            {
                sb.AppendFormat("{0}.", CreateDisplayName(method.DeclaringType, true));
            }

            if (method.MethodSig == null)
            {
                sb.Append(method.Name ?? "<<<NULL>>>");
            }
            else
            {
                if (!UTF8String.IsNullOrEmpty(method.Name))
                {
                    sb.Append(method.Name);
                }
                if (method.MethodSig.Generic)
                {
                    sb.Append('<');
                    uint genParamCount = method.MethodSig.GenParamCount;
                    for (uint i = 0; i < genParamCount; i++)
                    {
                        if (i != 0)
                        {
                            sb.Append(", ");
                        }
                        sb.Append(FullNameCreator.Name(new GenericMVar(i, method as MethodDef), false));
                    }
                    sb.Append('>');
                }
                sb.Append('(');
                int count = PrintMethodArgList(sb, method.MethodSig.Params, false, false);
                PrintMethodArgList(sb, method.MethodSig.ParamsAfterSentinel, count > 0, true);
                if (method.Name == ".ctor" || method.Name == ".cctor")
                {
                    sb.Append(")");
                }
                else
                {
                    sb.Append(") : ");
                    sb.Append(CreateDisplayName(method.MethodSig.RetType));
                }
            }
            return(sb.ToString());
        }
Esempio n. 4
0
        public static string GetName(this IType type, StringBuilder sb)
        {
            var td = type as TypeDef;

            if (td != null)
            {
                return(td.Name);
            }
            var tr = type as TypeRef;

            if (tr != null)
            {
                return(tr.Name);
            }
            sb.Length = 0;
            return(FullNameCreator.Name(type, false, sb));
        }
Esempio n. 5
0
        public static string CreateDisplayName(IType type, bool fullName = false)
        {
            var sb = new StringBuilder();

            if (type is ExportedType)
            {
                if (fullName)
                {
                    sb.Append(FullNameCreator.FullName((ExportedType)type, true));
                }
                else
                {
                    sb.Append(FullNameCreator.Name((ExportedType)type, true));
                }
                return(sb.ToString());
            }
            if (type is TypeSpec)
            {
                type = ((TypeSpec)type).TypeSig;
            }

            if (type is TypeSig)
            {
                if (type is TypeDefOrRefSig)
                {
                    type = ((TypeDefOrRefSig)type).TypeDefOrRef;
                }
                else
                {
                    CreateDisplayName(sb, new RecursionCounter(), (TypeSig)type, fullName);
                    return(sb.ToString());
                }
            }

            if (type is TypeRef)
            {
                if (fullName)
                {
                    sb.Append(FullNameCreator.FullName((TypeRef)type, true));
                }
                else
                {
                    sb.Append(FullNameCreator.Name((TypeRef)type, true));
                }
            }
            else if (type is TypeDef)
            {
                var typeDef = (TypeDef)type;
                if (fullName)
                {
                    sb.Append(FullNameCreator.FullName(typeDef, true));
                }
                else
                {
                    sb.Append(FullNameCreator.Name(typeDef, true));
                }

                if (typeDef.HasGenericParameters)
                {
                    sb.Append('<');
                    uint genParamCount = (uint)typeDef.GenericParameters.Count;
                    for (uint i = 0; i < genParamCount; i++)
                    {
                        if (i != 0)
                        {
                            sb.Append(", ");
                        }
                        sb.Append(FullNameCreator.Name(new GenericVar(i, typeDef), false));
                    }
                    sb.Append('>');
                }
            }
            else
            {
                throw new NotSupportedException();
            }
            return(sb.ToString());
        }