Example #1
0
        static TypeDef FindTypeFullName(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.FullName(a, false, null, sb), name))
                {
                    return true;
                }
                sb.Clear();
                if (comparer.Equals(s2 = FullNameCreator.FullName(a, true, null, sb), name))
                {
                    return true;
                }
                sb.Clear();
                if (comparer.Equals(CleanTypeName(s1), name))
                {
                    return true;
                }
                sb.Clear();
                return comparer.Equals(CleanTypeName(s2), name);
            }));
        }
Example #2
0
        public static string GetFnPtrFullName(FnPtrSig sig)
        {
            if (sig == null)
            {
                return(string.Empty);
            }
            var methodSig = sig.MethodSig;

            if (methodSig == null)
            {
                return(GetFnPtrName(sig));
            }

            var sb = new StringBuilder();

            sb.Append("method ");
            sb.Append(FullNameCreator.FullName(methodSig.RetType, false));
            sb.Append(" *(");
            PrintArgs(sb, methodSig.Params, true);
            if (methodSig.ParamsAfterSentinel != null)
            {
                if (methodSig.Params.Count > 0)
                {
                    sb.Append(",");
                }
                sb.Append("...,");
                PrintArgs(sb, methodSig.ParamsAfterSentinel, false);
            }
            sb.Append(")");

            return(sb.ToString());
        }
Example #3
0
        /// <summary>
        /// Gets the next <see cref="TypeDef"/> or <c>null</c> if there are no more left.
        /// The cache is updated with the returned <see cref="TypeDef"/> before the method
        /// returns.
        /// </summary>
        /// <returns>The next <see cref="TypeDef"/> or <c>null</c> if none</returns>
        TypeDef GetNextTypeDefCache()
        {
            var type = GetNextTypeDef();

            if (type == null)
            {
                return(null);
            }

            // Only insert it if another type with the exact same sig/name isn't already
            // in the cache. This should only happen with some obfuscated assemblies.

            if (!typeRefCache.ContainsKey(type))
            {
                typeRefCache[type] = type;
            }
            string fn;

            sb.Length = 0;
            if (!normalNameCache.ContainsKey(fn = FullNameCreator.FullName(type, false, null, sb)))
            {
                normalNameCache[fn] = type;
            }
            sb.Length = 0;
            if (!reflectionNameCache.ContainsKey(fn = FullNameCreator.FullName(type, true, null, sb)))
            {
                reflectionNameCache[fn] = type;
            }

            return(type);
        }
Example #4
0
 static void PrintArgs(StringBuilder sb, IList <TypeSig> args, bool isFirst)
 {
     foreach (var arg in args)
     {
         if (!isFirst)
         {
             sb.Append(",");
         }
         isFirst = false;
         sb.Append(FullNameCreator.FullName(arg, false));
     }
 }
Example #5
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);
        }
Example #6
0
 TypeDef FindSlowNormal(string fullName)
 {
     InitializeTypeEnumerator();
     while (true)
     {
         var type = GetNextTypeDef();
         if (type == null)
         {
             return(type);
         }
         sb.Length = 0;
         if (FullNameCreator.FullName(type, false, null, sb) == fullName)
         {
             return(type);
         }
     }
 }
Example #7
0
        TypeDef FindCacheNormal(string fullName)
        {
            TypeDef cachedType;

            if (normalNameCache.TryGetValue(fullName, out cachedType))
            {
                return(cachedType);
            }

            // Build the cache lazily
            while (true)
            {
                cachedType = GetNextTypeDefCache();
                if (cachedType == null)
                {
                    return(cachedType);
                }
                sb.Length = 0;
                if (FullNameCreator.FullName(cachedType, false, null, sb) == fullName)
                {
                    return(cachedType);
                }
            }
        }
Example #8
0
 /// <inheritdoc/>
 public override string ToString()
 {
     return(FullNameCreator.FullName(type == null ? null : type, false));
 }
Example #9
0
 public override string ToString()
 {
     return(FullNameCreator.MethodFullName(InterfaceType == null ? "" : FullNameCreator.FullName(InterfaceType, false), Name, MethodSig));
 }
Example #10
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());
        }