Example #1
0
        private AnalysisNet.Types.IType ExtractType(Cecil.GenericInstanceType typeref)
        {
            AnalysisNet.Types.BasicType genericTyperef = (AnalysisNet.Types.BasicType)ExtractType(typeref.ElementType);
            AnalysisNet.Types.IType[]   arguments      = typeref.GenericArguments.Select(argumentref => ExtractType(argumentref)).ToArray();
            AnalysisNet.Types.BasicType instancedType  = AnalysisNet.Extensions.Instantiate(genericTyperef, arguments);
            instancedType.Resolve(host);

            return(instancedType);
        }
Example #2
0
        public AnalysisNet.Types.IType ExtractType(Cecil.TypeReference typeReference)
        {
            return(performanceCache.GetOrCreate(typeReference, (cacheEntry) =>
            {
                AnalysisNet.Types.IType result = null;

                if (typeReference is Cecil.ArrayType arrayType)
                {
                    result = ExtractType(arrayType);
                }
                else if (typeReference is Cecil.ByReferenceType byReferenceType)
                {
                    result = ExtractType(byReferenceType);
                }
                else if (typeReference is Cecil.PointerType pointerType)
                {
                    result = ExtractType(pointerType);
                }
                else if (typeReference is Cecil.GenericParameter genericParameterType)
                {
                    result = ExtractType(genericParameterType);
                }
                else if (typeReference is Cecil.FunctionPointerType functionPointerType)
                {
                    result = ExtractType(functionPointerType);
                }
                else if (typeReference is Cecil.GenericInstanceType genericInstanceType)
                {
                    result = ExtractType(genericInstanceType);
                }
                else
                {
                    // named type reference
                    result = ExtractNonGenericInstanceType(typeReference);
                }

                if (result is AnalysisNet.Types.BasicType)
                {
                    AnalysisNet.Types.BasicType basicType = result as AnalysisNet.Types.BasicType;
                    basicType.Resolve(host);

                    if (basicType.GenericType is AnalysisNet.Types.BasicType)
                    {
                        basicType = basicType.GenericType as AnalysisNet.Types.BasicType;
                        basicType.Resolve(host);
                    }
                }

                return result;
            }));
        }
Example #3
0
        private AnalysisNet.Types.IType ExtractNonGenericInstanceType(Cecil.TypeReference typeref)
        {
            if (typeref.IsGenericInstance)
            {
                throw new Exception("precondition violation");
            }


            // we don't want the file extension
            string containingAssembly;

            if (typeref.Scope is Cecil.AssemblyNameReference assemblyRef)
            {
                containingAssembly = assemblyRef.Name;
            }
            else if (typeref.Scope is Cecil.ModuleDefinition moduleDef)
            {
                containingAssembly = moduleDef.Assembly.Name.Name;
            }
            else
            {
                throw new NotImplementedException();
            }

            string containingNamespace = typeref.Namespace;
            string name = UnmangleName(typeref);

            AnalysisNet.Types.TypeKind  kind    = GetTypeKind(typeref);
            AnalysisNet.Types.BasicType newType = new AnalysisNet.Types.BasicType(name, kind)
            {
                //ExtractAttributes(newType.Attributes, typeref.Attributes);

                ContainingAssembly  = new AnalysisNet.AssemblyReference(containingAssembly),
                ContainingNamespace = containingNamespace,

                GenericParameterCount = typeref.GenericParameters.Count
            };

            genericParameterExtractor.MapGenericParameters(typeref, newType);

            if (typeref.IsNested)
            {
                newType.ContainingType = (AnalysisNet.Types.IBasicType)ExtractType(typeref.DeclaringType);
                // analysis-net does not follow ecma
                // it expects nested types and their enclosing type share the same namespace
                newType.ContainingNamespace = newType.ContainingType.ContainingNamespace;
            }

            return(newType);
        }