Obfuscar.AssemblyCache.GetTypeDefinition C# (CSharp) Method

GetTypeDefinition() public method

public GetTypeDefinition ( TypeReference type ) : Mono.Cecil.TypeDefinition
type Mono.Cecil.TypeReference
return Mono.Cecil.TypeDefinition
        public TypeDefinition GetTypeDefinition(TypeReference type)
        {
            if (type == null)
                return null;

            TypeDefinition typeDef = type as TypeDefinition;
            if (typeDef == null) {
                AssemblyNameReference name = type.Scope as AssemblyNameReference;
                if (name != null) {
                    // try to self resolve, fall back to default resolver
                    AssemblyDefinition assmDef = null;
                    assmDef = SelfResolve (name);
                    if (assmDef == null) {
                        try {
                            Console.WriteLine ("Trying to resolve dependency: " + name);
                            assmDef = resolver.Resolve (name);
                            cache [name.FullName] = assmDef;
                        } catch (FileNotFoundException) {
                            throw new ApplicationException ("Unable to resolve dependency:  " + name.Name);
                        }
                    }

                    string fullName = null;
                    while (type.IsNested) {
                        if (fullName == null)
                            fullName = type.Name;
                        else
                            fullName = type.Name + "/" + fullName;
                        type = type.DeclaringType;
                    }
                    if (fullName == null)
                        fullName = type.Namespace + "." + type.Name;
                    else
                        fullName = type.Namespace + "." + type.Name + "/" + fullName;
                    typeDef = assmDef.MainModule.GetType (fullName);
                } else {
                    GenericInstanceType gi = type as GenericInstanceType;
                    if (gi != null)
                        return GetTypeDefinition (gi.ElementType);
                }
            }

            return typeDef;
        }

Usage Example

Esempio n. 1
0
        void GetVirtualMethods(AssemblyCache cache, HashSet <MethodKey> methods, TypeDefinition type)
        {
            // check the interfaces
            foreach (var ifaceRef in type.Interfaces)
            {
                TypeDefinition iface = project.GetTypeDefinition(ifaceRef.InterfaceType);

                // if it's not in the project, try to get it via the cache
                if (iface == null)
                {
                    iface = cache.GetTypeDefinition(ifaceRef.InterfaceType);
                }

                // search interface
                if (iface != null)
                {
                    GetVirtualMethods(cache, methods, iface);
                }
            }

            // check the base type unless it isn't in the project, or we don't have one
            TypeDefinition baseType = project.GetTypeDefinition(type.BaseType);

            // if it's not in the project, try to get it via the cache
            if (baseType == null)
            {
                baseType = cache.GetTypeDefinition(type.BaseType);
            }

            // search base
            if (baseType != null)
            {
                GetVirtualMethods(cache, methods, baseType);
            }

            foreach (MethodDefinition method in type.Methods)
            {
                if (method.IsVirtual)
                {
                    methods.Add(new MethodKey(method));
                }
            }
        }
All Usage Examples Of Obfuscar.AssemblyCache::GetTypeDefinition