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); }
private AnalysisNet.Types.TypeKind GetTypeKind(Cecil.TypeReference typeref) { AnalysisNet.Types.TypeKind result = AnalysisNet.Types.TypeKind.Unknown; if (typeref.IsValueType) { result = AnalysisNet.Types.TypeKind.ValueType; } else { result = AnalysisNet.Types.TypeKind.ReferenceType; } return(result); }
private void ExtractGenericTypeParameters(AnalysisNet.Types.IGenericDefinition definingType, Cecil.TypeDefinition typedef) { for (int i = 0; i < typedef.GenericParameters.Count; ++i) { Cecil.GenericParameter parameterdef = typedef.GenericParameters[i]; ushort index = (ushort)i; string name = parameterdef.Name; AnalysisNet.Types.TypeKind typeKind = GetGenericParameterTypeKind(parameterdef); AnalysisNet.Types.GenericParameter parameter = new AnalysisNet.Types.GenericParameter(AnalysisNet.Types.GenericParameterKind.Type, index, name, typeKind); ExtractCustomAttributes(parameter.Attributes, parameterdef.CustomAttributes); parameter.GenericContainer = definingType; definingType.GenericParameters.Add(parameter); parameter.Constraints.AddRange(parameterdef.Constraints.Select(c => ExtractType(c.ConstraintType))); } }
private AnalysisNet.Types.TypeDefinition ExtractClass(Cecil.TypeDefinition cecilType, AnalysisNet.Types.TypeKind typeKind, AnalysisNet.Types.TypeDefinitionKind typeDefinitionKind) { string name = UnmangleName(cecilType); AnalysisNet.Types.TypeDefinition type = new AnalysisNet.Types.TypeDefinition(name, typeKind, typeDefinitionKind); Cecil.TypeReference basedef = cecilType.BaseType; type.IsAbstract = cecilType.IsAbstract; type.IsSealed = cecilType.IsSealed; if (basedef != null) { type.Base = ExtractType(basedef) as AnalysisNet.Types.IBasicType; } ExtractCustomAttributes(type.Attributes, cecilType.CustomAttributes); ExtractGenericTypeParameters(type, cecilType); ExtractInterfaces(type.Interfaces, cecilType.Interfaces); ExtractFields(type, type.Fields, cecilType.Fields); ExtractMethods(type, type.Methods, cecilType.Methods); ExtractPropertyDefinitions(type, cecilType); ExtractExplicitMethodOverrides(type, cecilType); ExtractLayoutInformation(type, cecilType); return(type); }