internal static void AddGenericParameterTypeConstraintStep( this List <ISourceCodeBuilderStep> steps, GenericParameterTypeConstraint constraint) { switch (constraint) { case GenericParameterTypeConstraint.Class: steps.Add(new WriteClassKeyword()); break; case GenericParameterTypeConstraint.Struct: steps.Add(new WriteStructKeyword()); break; } }
internal GenericParameterDeclarationWithMonoCecil(AssemblyWithMonoCecil assembly, GenericParameter type) { this.type = type; attributes = new Lazy <Attributes>(() => new Attributes(assembly, type)); typeConstraint = GetTypeConstraint(type); direction = GetDirection(type); hasEmptyConstructorConstraint = type.HasDefaultConstructorConstraint && !type.HasNotNullableValueTypeConstraint; genericParameterConstraints = new List <GenericParameterReferenceWithMonoCecil>(); interfaceConstraints = new List <InterfaceReferenceWithMonoCecil>(); foreach (TypeReference constraintType in type.Constraints) { TypeDefinition constraintTypeDefinition = constraintType.Resolve(); if (constraintType.IsGenericParameter) { genericParameterConstraints.Add(new GenericParameterReferenceWithMonoCecil(constraintType)); } else if (constraintTypeDefinition.IsInterface) { interfaceConstraints.Add(new InterfaceReferenceWithMonoCecil(assembly, constraintType)); } else if (constraintTypeDefinition.IsClass) { if (baseClassConstraint != null) { throw new InvalidOperationException("GenericParameterDeclaration appears to have 2 base classes."); } if (constraintType.FullName != "System.ValueType") { baseClassConstraint = new ClassReferenceWithMonoCecil(assembly, constraintType); } } else { throw new InvalidOperationException("Unknown constraint type."); } } }
internal GenericParameterDeclarationWithReflection(Type type) { this.type = type; attributes = new Lazy <Attributes>(() => new Attributes(type)); typeConstraint = GetTypeConstraint(type); direction = GetDirection(type); hasEmptyConstructorConstraint = type.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint) && typeConstraint != GenericParameterTypeConstraint.Struct; genericParameterConstraints = new List <GenericParameterReferenceWithReflection>(); interfaceConstraints = new List <InterfaceReferenceWithReflection>(); foreach (Type constraintType in type.GetGenericParameterConstraints().Where(constraint => constraint != typeof(ValueType))) { if (constraintType.IsGenericParameter) { genericParameterConstraints.Add(new GenericParameterReferenceWithReflection(constraintType)); } else if (constraintType.IsInterface) { interfaceConstraints.Add(new InterfaceReferenceWithReflection(constraintType)); } else if (constraintType.IsClass) { if (baseClassConstraint != null) { throw new InvalidOperationException("GenericParameterDeclaration appears to have 2 base classes."); } baseClassConstraint = new ClassReferenceWithReflection(constraintType); } else { throw new InvalidOperationException("Unknown constraint type."); } } }