void AddTypes(Collection <TypeDefinition> types) { foreach (TypeDefinition td in types) { if ((td.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public) { string name = td.FullName; if (name.Length == 0 || name[0] == '<') { continue; } name = ReflectionClass.SplitTypeParameterCountFromReflectionName(name); AddClassToNamespaceListInternal(new CecilClass(this.AssemblyCompilationUnit, null, td, name)); } } }
void AddTypes(TypeDefinitionCollection types) { foreach (TypeDefinition td in types) { if ((td.Attributes & TypeAttributes.Public) == TypeAttributes.Public) { if ((td.Attributes & TypeAttributes.NestedAssembly) == TypeAttributes.NestedAssembly || (td.Attributes & TypeAttributes.NestedPrivate) == TypeAttributes.NestedPrivate || (td.Attributes & TypeAttributes.NestedFamANDAssem) == TypeAttributes.NestedFamANDAssem) { continue; } string name = td.FullName; if (name.Length == 0 || name[0] == '<') { continue; } name = ReflectionClass.SplitTypeParameterCountFromReflectionName(name); AddClassToNamespaceListInternal(new CecilClass(this.AssemblyCompilationUnit, null, td, name)); } } }
void InitMembers(TypeDefinition type) { string defaultMemberName = null; foreach (CustomAttribute att in type.CustomAttributes) { if (att.Constructor.DeclaringType.FullName == "System.Reflection.DefaultMemberAttribute" && att.ConstructorArguments.Count == 1) { defaultMemberName = att.ConstructorArguments[0].Value as string; } } foreach (TypeDefinition nestedType in type.NestedTypes) { TypeAttributes visibility = nestedType.Attributes & TypeAttributes.VisibilityMask; if (visibility == TypeAttributes.NestedPublic || visibility == TypeAttributes.NestedFamily || visibility == TypeAttributes.NestedFamORAssem) { string name = nestedType.Name; int pos = name.LastIndexOf('/'); if (pos > 0) { name = name.Substring(pos + 1); } if (name.Length == 0 || name[0] == '<') { continue; } name = ReflectionClass.SplitTypeParameterCountFromReflectionName(name); name = this.FullyQualifiedName + "." + name; InnerClasses.Add(new CecilClass(this.CompilationUnit, this, nestedType, name)); } } foreach (FieldDefinition field in type.Fields) { if (IsVisible(field.Attributes) && !field.IsSpecialName) { DefaultField f = new DefaultField(this, field.Name); f.Modifiers = TranslateModifiers(field); f.ReturnType = CreateType(this.ProjectContent, this, field.FieldType, field); AddAttributes(CompilationUnit.ProjectContent, f, f.Attributes, field); Fields.Add(f); } } foreach (PropertyDefinition property in type.Properties) { AddProperty(defaultMemberName, property); } foreach (EventDefinition eventDef in type.Events) { if (eventDef.AddMethod != null && IsVisible(eventDef.AddMethod.Attributes)) { DefaultEvent e = new DefaultEvent(this, eventDef.Name); if (this.ClassType == ClassType.Interface) { e.Modifiers = ModifierEnum.Public | ModifierEnum.Abstract; } else { e.Modifiers = TranslateModifiers(eventDef); } e.ReturnType = CreateType(this.ProjectContent, this, eventDef.EventType, eventDef); AddAttributes(CompilationUnit.ProjectContent, e, e.Attributes, eventDef); Events.Add(e); } } this.AddDefaultConstructorIfRequired = (this.ClassType == ClassType.Struct || this.ClassType == ClassType.Enum); foreach (MethodDefinition method in type.Methods) { if (method.IsConstructor || !method.IsSpecialName) { AddMethod(method); } } }
static IReturnType CreateType(IProjectContent pc, IEntity member, TypeReference type, ICustomAttributeProvider attributeProvider, ref int typeIndex) { while (type is OptionalModifierType || type is RequiredModifierType) { type = ((TypeSpecification)type).ElementType; } if (type == null) { LoggingService.Warn("CecilReader: Null type for: " + member); return(new VoidReturnType(pc)); } if (type is ByReferenceType) { // TODO: Use ByRefRefReturnType return(CreateType(pc, member, (type as ByReferenceType).ElementType, attributeProvider, ref typeIndex)); } else if (type is PointerType) { typeIndex++; return(new PointerReturnType(CreateType(pc, member, (type as PointerType).ElementType, attributeProvider, ref typeIndex))); } else if (type is ArrayType) { typeIndex++; return(new ArrayReturnType(pc, CreateType(pc, member, (type as ArrayType).ElementType, attributeProvider, ref typeIndex), (type as ArrayType).Rank)); } else if (type is GenericInstanceType) { GenericInstanceType gType = (GenericInstanceType)type; IReturnType baseType = CreateType(pc, member, gType.ElementType, attributeProvider, ref typeIndex); IReturnType[] para = new IReturnType[gType.GenericArguments.Count]; for (int i = 0; i < para.Length; ++i) { typeIndex++; para[i] = CreateType(pc, member, gType.GenericArguments[i], attributeProvider, ref typeIndex); } return(new ConstructedReturnType(baseType, para)); } else if (type is GenericParameter) { GenericParameter typeGP = type as GenericParameter; if (typeGP.Owner is MethodDefinition) { IMethod method = member as IMethod; if (method != null) { if (typeGP.Position < method.TypeParameters.Count) { return(new GenericReturnType(method.TypeParameters[typeGP.Position])); } } return(new GenericReturnType(new DefaultTypeParameter(method, typeGP.Name, typeGP.Position))); } else { IClass c = (member is IClass) ? (IClass)member : (member is IMember) ? ((IMember)member).DeclaringType : null; if (c != null && typeGP.Position < c.TypeParameters.Count) { if (c.TypeParameters[typeGP.Position].Name == type.Name) { return(new GenericReturnType(c.TypeParameters[typeGP.Position])); } } return(new GenericReturnType(new DefaultTypeParameter(c, typeGP.Name, typeGP.Position))); } } else { string name = type.FullName; if (name == null) { throw new ApplicationException("type.FullName returned null. Type: " + type.ToString()); } int typeParameterCount; if (name.IndexOf('/') > 0) { typeParameterCount = 0; StringBuilder newName = new StringBuilder(); foreach (string namepart in name.Split('/')) { if (newName.Length > 0) { newName.Append('.'); } int partTypeParameterCount; newName.Append(ReflectionClass.SplitTypeParameterCountFromReflectionName(namepart, out partTypeParameterCount)); typeParameterCount += partTypeParameterCount; } name = newName.ToString(); } else { name = ReflectionClass.SplitTypeParameterCountFromReflectionName(name, out typeParameterCount); } if (typeParameterCount == 0 && name == "System.Object" && HasDynamicAttribute(attributeProvider, typeIndex)) { return(new DynamicReturnType(pc)); } IClass c = pc.GetClass(name, typeParameterCount); if (c != null) { return(c.DefaultReturnType); } else { // example where name is not found: pointers like System.Char* // or when the class is in a assembly that is not referenced return(new GetClassReturnType(pc, name, typeParameterCount)); } } }