public void DefineNestedType_String(string name) { TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); TypeBuilder nestedType = type.DefineNestedType(name); Helpers.VerifyType(nestedType, type.Module, type, name, TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.NestedPrivate, typeof(object), 0, PackingSize.Unspecified, new Type[0]); }
public void DefineNestedType(string name, TypeAttributes attributes, Type parent, PackingSize packingSize, int typesize, Type[] implementedInterfaces) { bool isDefaultImplementedInterfaces = implementedInterfaces?.Length == 0; bool isDefaultPackingSize = packingSize == PackingSize.Unspecified; bool isDefaultSize = typesize == 0; bool isDefaultParent = parent == null; bool isDefaultAttributes = attributes == TypeAttributes.NestedPrivate; void Verify(TypeBuilder type, TypeBuilder declaringType) { bool allowsNullParent = attributes.HasFlag(TypeAttributes.Abstract) && attributes.HasFlag(TypeAttributes.ClassSemanticsMask); Type baseType = allowsNullParent ? parent : (parent ?? typeof(object)); Helpers.VerifyType(type, declaringType.Module, declaringType, name, attributes, baseType, typesize, packingSize, implementedInterfaces); } if (isDefaultImplementedInterfaces) { if (isDefaultSize && isDefaultPackingSize) { if (isDefaultParent) { if (isDefaultAttributes) { // Use DefineNestedType(string) TypeBuilder type1 = Helpers.DynamicType(TypeAttributes.Public); Verify(type1.DefineNestedType(name), type1); } // Use DefineNestedType(string, TypeAttributes) TypeBuilder type2 = Helpers.DynamicType(TypeAttributes.Public); Verify(type2.DefineNestedType(name, attributes), type2); } // Use DefineNestedType(string, TypeAttributes, Type) TypeBuilder type3 = Helpers.DynamicType(TypeAttributes.Public); Verify(type3.DefineNestedType(name, attributes, parent), type3); } else if (isDefaultSize) { // Use DefineNestedType(string, TypeAttributes, Type, PackingSize) TypeBuilder type4 = Helpers.DynamicType(TypeAttributes.Public); Verify(type4.DefineNestedType(name, attributes, parent, packingSize), type4); } else if (isDefaultPackingSize) { // Use DefineNestedType(string, TypeAttributes, Type, int) TypeBuilder type5 = Helpers.DynamicType(TypeAttributes.Public); Verify(type5.DefineNestedType(name, attributes, parent, typesize), type5); } // Use DefineNestedType(string, TypeAttributes, Type, PackingSize, int); TypeBuilder type6 = Helpers.DynamicType(TypeAttributes.Public); Verify(type6.DefineNestedType(name, attributes, parent, packingSize, typesize), type6); } else { // Use DefineNestedType(string, TypeAttributes, Type, Type[]) Assert.True(isDefaultSize && isDefaultPackingSize); // Sanity check TypeBuilder type7 = Helpers.DynamicType(TypeAttributes.Public); Verify(type7.DefineNestedType(name, attributes, parent, implementedInterfaces), type7); } }
public void DefineType(string name, TypeAttributes attributes, Type parent, PackingSize packingSize, int typesize, Type[] implementedInterfaces) { bool isDefaultImplementedInterfaces = implementedInterfaces?.Length == 0; bool isDefaultPackingSize = packingSize == PackingSize.Unspecified; bool isDefaultSize = typesize == 0; bool isDefaultParent = parent == null; bool isDefaultAttributes = attributes == TypeAttributes.NotPublic; void Verify(TypeBuilder type, Module module) { Type baseType = attributes.HasFlag(TypeAttributes.Abstract) && parent == null ? null : (parent ?? typeof(object)); Helpers.VerifyType(type, module, null, name, attributes, baseType, typesize, packingSize, implementedInterfaces); } if (isDefaultImplementedInterfaces) { if (isDefaultSize && isDefaultPackingSize) { if (isDefaultParent) { if (isDefaultAttributes) { // Use DefineType(string) ModuleBuilder module1 = Helpers.DynamicModule(); Verify(module1.DefineType(name), module1); } // Use DefineType(string, TypeAttributes) ModuleBuilder module2 = Helpers.DynamicModule(); Verify(module2.DefineType(name, attributes), module2); } // Use DefineType(string, TypeAttributes, Type) ModuleBuilder module3 = Helpers.DynamicModule(); Verify(module3.DefineType(name, attributes, parent), module3); } else if (isDefaultSize) { // Use DefineType(string, TypeAttributes, Type, PackingSize) ModuleBuilder module4 = Helpers.DynamicModule(); Verify(module4.DefineType(name, attributes, parent, packingSize), module4); } else if (isDefaultPackingSize) { // Use DefineType(string, TypeAttributes, Type, int) ModuleBuilder module5 = Helpers.DynamicModule(); Verify(module5.DefineType(name, attributes, parent, typesize), module5); } // Use DefineType(string, TypeAttributes, Type, PackingSize, int) ModuleBuilder module6 = Helpers.DynamicModule(); Verify(module6.DefineType(name, attributes, parent, packingSize, typesize), module6); } else { // Use DefineType(string, TypeAttributes, Type, Type[]) Assert.True(isDefaultSize && isDefaultPackingSize); // Sanity check ModuleBuilder module7 = Helpers.DynamicModule(); Verify(module7.DefineType(name, attributes, parent, implementedInterfaces), module7); } }
public void DefineNestedType_String_TypeAttributes_Type(TypeAttributes attributes) { TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); Type parent = type.GetType(); foreach (string name in new string[] { "abc", "a\0b\0cd" }) { TypeBuilder nestedType = type.DefineNestedType(name, attributes, parent); Helpers.VerifyType(nestedType, type.Module, type, name, attributes, parent, 0, PackingSize.Unspecified, new Type[0]); } }
public void DefineNestedType_String_TypeAttributes_Type_PackingSize(TypeAttributes attributes) { foreach (PackingSize packagingSize in new PackingSize[] { PackingSize.Size1, PackingSize.Size128, PackingSize.Size16, PackingSize.Size2, PackingSize.Size32, PackingSize.Size4, PackingSize.Size64, PackingSize.Size8, PackingSize.Unspecified }) { TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); Type parent = type.GetType(); foreach (string name in new string[] { "abc", "a\0b\0cd" }) { TypeBuilder nestedType = type.DefineNestedType(name, attributes, parent, packagingSize); Helpers.VerifyType(nestedType, type.Module, type, name, attributes, parent, 0, packagingSize, new Type[0]); } } }
public void DefineNestedType_String_TypeAttributes(TypeAttributes attributes) { TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); foreach (string name in new string[] { "abc", "a\0b\0cd" }) { TypeBuilder nestedType = type.DefineNestedType(name, attributes); Assert.Equal(name, nestedType.Name); Assert.Equal(attributes, nestedType.Attributes); TypeAttributes noBaseTypeAttributes = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.NestedPublic | TypeAttributes.ClassSemanticsMask | TypeAttributes.Abstract; Type expectedBaseType = attributes == noBaseTypeAttributes ? null : typeof(object); Helpers.VerifyType(nestedType, type.Module, type, name, attributes, expectedBaseType, 0, PackingSize.Unspecified, new Type[0]); } }