public CodeTypeDeclaration(string name) { this.attributes = System.Reflection.TypeAttributes.Public; this.baseTypes = new CodeTypeReferenceCollection(); this.members = new CodeTypeMemberCollection(); base.Name = name; }
public InterpretedDefinitionType( string name, string @namespace, Assembly assembly, Type declaringType, Lazy <Type> baseType, Lazy <Type[]> interfaces, Func <Type, IReadOnlyCollection <ILazyMember <MemberInfo> > > getMembers, TypeAttributes attributes, Type[] genericParameters ) : base(baseType, interfaces, getMembers) { _name = name; _namespace = @namespace; _assembly = assembly; _declaringType = declaringType; _attributes = attributes; _genericParameters = genericParameters; }
private TypeBuilder CreateTypeBuilder(string name, System.Reflection.TypeAttributes attributes, Type parent, List <Type> interfaces) { var assemblyName = new AssemblyName( $"__assembly__{DateTime.Now.Millisecond}" ); var assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess.RunAndSave ); var moduleBuilder = assemblyBuilder.DefineDynamicModule( assemblyBuilder.GetName().Name, false ); return(moduleBuilder.DefineType(name, attributes, parent, interfaces.ToArray() )); }
static CodeTypeDeclaration CreateTypeDeclaration(TypeDefinition publicType) { if (IsDelegate(publicType)) { return(CreateDelegateDeclaration(publicType)); } bool @static = false; TypeAttributes attributes = 0; if (publicType.IsPublic || publicType.IsNestedPublic) { attributes |= TypeAttributes.Public; } if (publicType.IsNestedFamily) { attributes |= TypeAttributes.NestedFamily; } if (publicType.IsSealed && !publicType.IsAbstract) { attributes |= TypeAttributes.Sealed; } else if (!publicType.IsSealed && publicType.IsAbstract && !publicType.IsInterface) { attributes |= TypeAttributes.Abstract; } else if (publicType.IsSealed && publicType.IsAbstract) { @static = true; } // Static support is a hack. CodeDOM does support it, and this isn't // correct C#, but it's good enough for our API outline var name = publicType.Name; var index = name.IndexOf('`'); if (index != -1) { name = name.Substring(0, index); } var declaration = new CodeTypeDeclaration(@static ? "static " + name : name) { CustomAttributes = CreateCustomAttributes(publicType), // TypeAttributes must be specified before the IsXXX as they manipulate TypeAttributes! TypeAttributes = attributes, IsClass = publicType.IsClass, IsEnum = publicType.IsEnum, IsInterface = publicType.IsInterface, IsStruct = publicType.IsValueType && !publicType.IsPrimitive && !publicType.IsEnum, }; if (declaration.IsInterface && publicType.BaseType != null) { throw new NotImplementedException("Base types for interfaces needs testing"); } PopulateGenericParameters(publicType, declaration.TypeParameters); if (publicType.BaseType != null && ShouldOutputBaseType(publicType)) { if (publicType.BaseType.FullName == "System.Enum") { var underlyingType = publicType.GetEnumUnderlyingType(); if (underlyingType.FullName != "System.Int32") { declaration.BaseTypes.Add(CreateCodeTypeReference(underlyingType)); } } else { declaration.BaseTypes.Add(CreateCodeTypeReference(publicType.BaseType)); } } foreach (var @interface in publicType.Interfaces.OrderBy(i => i.FullName) .Select(t => new { Reference = t, Definition = t.Resolve() }) .Where(t => ShouldIncludeType(t.Definition)) .Select(t => t.Reference)) { declaration.BaseTypes.Add(CreateCodeTypeReference(@interface)); } foreach (var memberInfo in publicType.GetMembers().Where(ShouldIncludeMember).OrderBy(m => m.Name)) { AddMemberToTypeDeclaration(declaration, memberInfo); } // Fields should be in defined order for an enum var fields = !publicType.IsEnum ? publicType.Fields.OrderBy(f => f.Name) : (IEnumerable <FieldDefinition>)publicType.Fields; foreach (var field in fields) { AddMemberToTypeDeclaration(declaration, field); } foreach (var nestedType in publicType.NestedTypes.Where(ShouldIncludeType).OrderBy(t => t.FullName)) { var nestedTypeDeclaration = CreateTypeDeclaration(nestedType); declaration.Members.Add(nestedTypeDeclaration); } return(declaration); }
static CodeTypeDeclaration CreateTypeDeclaration(TypeDefinition publicType, string[] whitelistedNamespacePrefixes, AttributeFilter attributeFilter) { if (publicType.IsDelegate()) { return(CreateDelegateDeclaration(publicType, attributeFilter)); } var @static = false; TypeAttributes attributes = 0; if (publicType.IsPublic || publicType.IsNestedPublic) { attributes |= TypeAttributes.Public; } if (publicType.IsNestedFamily || publicType.IsNestedFamilyOrAssembly) { attributes |= TypeAttributes.NestedFamily; } if (publicType.IsSealed && !publicType.IsAbstract) { attributes |= TypeAttributes.Sealed; } else if (!publicType.IsSealed && publicType.IsAbstract && !publicType.IsInterface) { attributes |= TypeAttributes.Abstract; } else if (publicType.IsSealed && publicType.IsAbstract) { @static = true; } // Static support is a hack. CodeDOM does support it, and this isn't // correct C#, but it's good enough for our API outline var name = publicType.Name; var isStruct = publicType.IsValueType && !publicType.IsPrimitive && !publicType.IsEnum; var @readonly = isStruct && publicType.CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.IsReadOnlyAttribute"); var index = name.IndexOf('`'); if (index != -1) { name = name.Substring(0, index); } var declarationName = string.Empty; if (@readonly) { declarationName += CodeNormalizer.ReadonlyMarker; } if (@static) { declarationName += CodeNormalizer.StaticMarker; } declarationName += name; var declaration = new CodeTypeDeclaration(declarationName) { CustomAttributes = CreateCustomAttributes(publicType, attributeFilter), // TypeAttributes must be specified before the IsXXX as they manipulate TypeAttributes! TypeAttributes = attributes, IsClass = publicType.IsClass, IsEnum = publicType.IsEnum, IsInterface = publicType.IsInterface, IsStruct = isStruct, }; if (declaration.IsInterface && publicType.BaseType != null) { throw new NotImplementedException("Base types for interfaces needs testing"); } PopulateGenericParameters(publicType, declaration.TypeParameters, attributeFilter, parameter => { var declaringType = publicType.DeclaringType; while (declaringType != null) { if (declaringType.GenericParameters.Any(p => p.Name == parameter.Name)) { return(false); // https://github.com/ApiApprover/ApiApprover/issues/108 } declaringType = declaringType.DeclaringType; } return(true); }); if (publicType.BaseType != null && ShouldOutputBaseType(publicType)) { if (publicType.BaseType.FullName == "System.Enum") { var underlyingType = publicType.GetEnumUnderlyingType(); if (underlyingType.FullName != "System.Int32") { declaration.BaseTypes.Add(underlyingType.CreateCodeTypeReference()); } } else { declaration.BaseTypes.Add(publicType.BaseType.CreateCodeTypeReference(publicType)); } } foreach (var @interface in publicType.Interfaces.OrderBy(i => i.InterfaceType.FullName, StringComparer.Ordinal) .Select(t => new { Reference = t, Definition = t.InterfaceType.Resolve() }) .Where(t => ShouldIncludeType(t.Definition)) .Select(t => t.Reference)) { declaration.BaseTypes.Add(@interface.InterfaceType.CreateCodeTypeReference(@interface)); } foreach (var memberInfo in publicType.GetMembers().Where(memberDefinition => ShouldIncludeMember(memberDefinition, whitelistedNamespacePrefixes)).OrderBy(m => m.Name, StringComparer.Ordinal)) { AddMemberToTypeDeclaration(declaration, memberInfo, attributeFilter); } // Fields should be in defined order for an enum var fields = !publicType.IsEnum ? publicType.Fields.OrderBy(f => f.Name, StringComparer.Ordinal) : (IEnumerable <FieldDefinition>)publicType.Fields; foreach (var field in fields) { AddMemberToTypeDeclaration(declaration, field, attributeFilter); } foreach (var nestedType in publicType.NestedTypes.Where(ShouldIncludeType).OrderBy(t => t.FullName, StringComparer.Ordinal)) { using (NullableContext.Push(nestedType)) { var nestedTypeDeclaration = CreateTypeDeclaration(nestedType, whitelistedNamespacePrefixes, attributeFilter); declaration.Members.Add(nestedTypeDeclaration); } } return(declaration); }
private void LoadTypeInfo(Type[] AssemblyType) { //loop through the Assembly Type and write the //attributes out to the list box. foreach (Type baseType in AssemblyType) { //get the full name of the assembly. listTypes.Items.Add("Assembly Type: " + baseType.FullName + "(" + baseType.UnderlyingSystemType + ")"); //get the base type of the assembly. listTypes.Items.Add("Base Type: " + baseType.BaseType); //is the assembly serializable. listTypes.Items.Add("Serializable: " + baseType.IsSerializable); //is the assembly defined as abstract. listTypes.Items.Add("Abstract: " + baseType.IsAbstract); //is the assembly a class library. listTypes.Items.Add("Class: " + baseType.IsClass); //is the assembly public. listTypes.Items.Add("Public: " + baseType.IsPublic); //is the assembly sealed. listTypes.Items.Add("Sealed: " + baseType.IsSealed); //get the attributes defined - returns only .NET base attributes. //for customattributes use the "GetCustomAttributes()" method. System.Reflection.TypeAttributes baseTypeAttributes = baseType.Attributes; listTypes.Items.Add(""); listTypes.Items.Add("Attributes:"); listTypes.Items.Add(baseTypeAttributes.ToString()); //get all of the properties defined on the assembly. PropertyInfo[] propInfo = baseType.GetProperties(); //get all of the methods defined on the assembly. MethodInfo[] methodInfo = baseType.GetMethods(); listTypes.Items.Add(""); listTypes.Items.Add("Properties:"); //loop through the property info array. foreach (PropertyInfo prop in propInfo) { //get the property name. listTypes.Items.Add("Name: " + prop.Name); //get the member type "Property". listTypes.Items.Add("Member Type: " + prop.MemberType); //get the property type. listTypes.Items.Add("Property Type: " + prop.PropertyType); //does the property have a get method defined. listTypes.Items.Add("Read: " + prop.CanRead); //does the property have a set method defined. listTypes.Items.Add("Write: " + prop.CanWrite); listTypes.Items.Add(""); } listTypes.Items.Add(""); listTypes.Items.Add("Methods:"); //loop through the method info array. foreach (MethodInfo method in methodInfo) { //get the method name. listTypes.Items.Add("Name: " + method.Name); //get the membertype. listTypes.Items.Add("Member Type: " + method.MemberType); //get the return type of the method. listTypes.Items.Add("Return Type: " + method.ReturnType); //is the method defined as public. listTypes.Items.Add("Public: " + method.IsPublic); //is the method defined as private. listTypes.Items.Add("Private: " + method.IsPrivate); //is the method defined as abstract. listTypes.Items.Add("Abstract: " + method.IsAbstract); //is the method defined as virtual. listTypes.Items.Add("Virtual: " + method.IsVirtual); //is the method defined as static. listTypes.Items.Add("Static: " + method.IsStatic); listTypes.Items.Add(""); } } }
public System.Reflection.Emit.TypeBuilder DefineNestedType(string name, System.Reflection.TypeAttributes attr, System.Type parent, System.Reflection.Emit.PackingSize packSize, int typeSize) { throw null; }
public System.Reflection.Emit.TypeBuilder DefineNestedType(string name, System.Reflection.TypeAttributes attr, System.Type parent) { throw null; }
public System.Reflection.Emit.TypeBuilder DefineType(string name, System.Reflection.TypeAttributes attr, System.Type parent, System.Type[] interfaces) { throw null; }
public System.Reflection.Emit.TypeBuilder DefineType(string name, System.Reflection.TypeAttributes attr, System.Type parent, int typesize) { throw null; }
public System.Reflection.Emit.TypeBuilder DefineType(string name, System.Reflection.TypeAttributes attr) { throw null; }
public System.Reflection.Emit.EnumBuilder DefineEnum(string name, System.Reflection.TypeAttributes visibility, System.Type underlyingType) { throw null; }