Example #1
0
        private BfAssembly GetAssembly(MemberReference reference, AssemblyNameReference scope)
        {
            BfAssembly bfAssembly = null;

            if (scope != null && _dictionary.ContainsKey(scope.FullName))
            {
                bfAssembly = _dictionary[scope.FullName];
            }
            else if (reference.DeclaringType.Scope is ModuleDefinition)
            {
                bfAssembly = _dictionary[((ModuleDefinition)reference.DeclaringType.Scope).Assembly.Name.FullName];
            }

            return(bfAssembly);
        }
Example #2
0
        private BfType CreateBfType(TypeDefinition typeDefinition, BfAssembly bfAssembly)
        {
            var    key = GetKey(typeDefinition);
            BfType createBfType;

            if (!bfAssembly.GetTypesDictionary().ContainsKey(key))
            {
                createBfType = new BfType(this, typeDefinition, bfAssembly);
                bfAssembly.GetTypesDictionary().Add(GetKey(typeDefinition), createBfType);
                Types.Add(createBfType);
                bfAssembly.Namespaces.Add(createBfType.Namespace);
            }
            else
            {
                createBfType = bfAssembly.GetTypesDictionary()[key];
            }

            return(createBfType);
        }
Example #3
0
        private void GetTypes(Dictionary <string, AssemblyTuple> nameToAssemblyTuples)
        {
            var dictionary = new Dictionary <AssemblyDefinition, BfAssembly>();

            foreach (var assemblyTuple in nameToAssemblyTuples.Values.Where(a => !dictionary.ContainsKey(a.Assembly)))
            {
                var bfAssembly = new BfAssembly(assemblyTuple.Assembly, assemblyTuple.IsCoreAssembly, assemblyTuple.Directory);

                dictionary.Add(assemblyTuple.Assembly, bfAssembly);

                foreach (var moduleDefinition in assemblyTuple.Assembly.Modules)
                {
                    _assembliesDictionary.SafeAdd(moduleDefinition.Name, bfAssembly);

                    if (assemblyTuple.IsCoreAssembly)
                    {
                        moduleDefinition.Types.ForEach(t => CreateBfType(t, bfAssembly));
                    }
                }
            }

            nameToAssemblyTuples.Keys.ForEach(k => _dictionary.Add(k, dictionary[nameToAssemblyTuples[k].Assembly]));
        }
Example #4
0
        internal BfType GetBfType(TypeReference typeReference)
        {
            BfType bfType = null;

            if (typeReference == null)
            {
                bfType = null;
            }
            else
            {
                typeReference = GetTypeReference(typeReference);

                if (typeReference is GenericParameter)
                {
                    var bfAssembly = _dictionary.Values.Where(assembly =>
                    {
                        if (assembly.Name == "mscorlib")
                        {
                            return(string.Compare(assembly.Version, "2.0.0.0", StringComparison.Ordinal) > -1);
                        }
                        return(false);
                    }).FirstOrDefault() ?? _dictionary.Values.FirstOrDefault(assembly => assembly.IsCoreAssembly);

                    // ReSharper disable once PossibleNullReferenceException
                    bfType = !bfAssembly.GetTypesDictionary().ContainsKey(typeReference.FullName)
                        ? CreateBfType(new TypeDefinition(typeReference.Name, typeReference.Namespace, TypeAttributes.Abstract, null), bfAssembly)
                        : bfAssembly.GetTypesDictionary()[typeReference.FullName];
                }
                else
                {
                    try
                    {
                        BfAssembly bfAssembly = null;

                        var scope = typeReference.Scope as AssemblyNameReference;

                        if (scope != null && _dictionary.ContainsKey(scope.FullName))
                        {
                            bfAssembly = _dictionary[scope.FullName];
                        }
                        else if (typeReference.Scope is ModuleDefinition && _dictionary.ContainsKey(((ModuleDefinition)typeReference.Scope).Assembly.Name.FullName))
                        {
                            bfAssembly = _dictionary[((ModuleDefinition)typeReference.Scope).Assembly.Name.FullName];
                        }
                        else if (typeReference.Scope is ModuleReference && _dictionary.ContainsKey(((ModuleReference)typeReference.Scope).Name))
                        {
                            bfAssembly = _assembliesDictionary[((ModuleReference)typeReference.Scope).Name];
                        }

                        BfType type = null;

                        if (bfAssembly != null)
                        {
                            bfAssembly.GetTypesDictionary().TryGetValue(GetKey(typeReference), out type);

                            if (type == null)
                            {
                                var typeDefinition = bfAssembly.GetAssemblyDefinition().Modules.SelectMany(m => m.Types).FirstOrDefault(p => typeReference.FullName == p.FullName);

                                if (typeDefinition != null)
                                {
                                    type = CreateBfType(typeDefinition, bfAssembly);
                                }
                            }
                        }

                        if (type != null)
                        {
                            bfType = type;
                        }
                    }
                    catch (KeyNotFoundException)
                    {
                    }
                }
            }

            return(bfType);
        }
Example #5
0
        internal BfType(BfCache cache, TypeDefinition type, BfAssembly assembly)
        {
            _typeDefinition = type;
            _bfCache        = cache;

            Name      = _typeDefinition.Name;
            Assembly  = assembly;
            Namespace = cache.GetNamespace(this);

            IsClass     = _typeDefinition.IsClass;
            IsEnum      = _typeDefinition.IsEnum;
            IsValueType = _typeDefinition.IsValueType;
            IsInterface = _typeDefinition.IsInterface;

            if (_typeDefinition.IsNestedAssembly)
            {
                IsNested   = true;
                IsInternal = true;
            }

            if (_typeDefinition.IsNestedFamily)
            {
                IsNested    = true;
                IsProtected = true;
            }

            if (_typeDefinition.IsNestedFamilyAndAssembly)
            {
                IsNested = true;
                IsProtectedAndInternal = true;
            }

            if (_typeDefinition.IsNestedFamilyOrAssembly)
            {
                IsNested = true;
                IsProtectedOrInternal = true;
            }

            if (_typeDefinition.IsNestedPrivate)
            {
                IsNested  = true;
                IsPrivate = true;
            }

            if (_typeDefinition.IsNestedPublic)
            {
                IsNested = true;
                IsPublic = true;
            }

            IsInternal = _typeDefinition.IsNotPublic;
            IsPublic   = _typeDefinition.IsPublic;

            if ((!_typeDefinition.IsSealed ? 1 : (!_typeDefinition.IsAbstract ? 1 : 0)) == 0)
            {
                IsStatic = true;
            }
            else
            {
                IsSealed   = _typeDefinition.IsSealed;
                IsAbstract = _typeDefinition.IsAbstract;
            }
        }