Пример #1
0
        private static TypeDefinition DefineNullableAttribute(AssemblyDefinition assemblyDefinition, TypeReference embeddedAttribute, WellKnownTypes wellKnownTypes)
        {
            var attribute = new TypeDefinition(
                @namespace: "System.Runtime.CompilerServices",
                name: "NullableAttribute",
                TypeAttributes.NotPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit,
                wellKnownTypes.Module.ImportReference(wellKnownTypes.SystemAttribute));

            MethodDefinition compilerGeneratedConstructor = wellKnownTypes.SystemRuntimeCompilerServicesCompilerGeneratedAttribute.Resolve().Methods.Single(method => method.IsConstructor && !method.IsStatic && method.Parameters.Count == 0);

            attribute.CustomAttributes.Add(new CustomAttribute(wellKnownTypes.Module.ImportReference(compilerGeneratedConstructor)));
            attribute.CustomAttributes.Add(new CustomAttribute(embeddedAttribute.Resolve().Methods.Single(method => method.IsConstructor && !method.IsStatic && method.Parameters.Count == 0)));

            var constructorByte = MethodFactory.Constructor(wellKnownTypes);

            constructorByte.Parameters.Add(new ParameterDefinition(wellKnownTypes.TypeSystem.Byte));

            var constructorByteArray = MethodFactory.Constructor(wellKnownTypes);

            constructorByteArray.Parameters.Add(new ParameterDefinition(new ArrayType(wellKnownTypes.TypeSystem.Byte)));

            attribute.Methods.Add(constructorByte);
            attribute.Methods.Add(constructorByteArray);

            assemblyDefinition.MainModule.Types.Add(attribute);

            return(attribute);
        }
            protected override void ImplementAttribute(ModuleDefinition module, TypeDefinition attribute, WellKnownTypes wellKnownTypes, CustomAttributeFactory attributeFactory)
            {
                var constructor = MethodFactory.Constructor(wellKnownTypes.TypeSystem);

                constructor.Parameters.Add(new ParameterDefinition("returnValue", ParameterAttributes.None, wellKnownTypes.TypeSystem.Boolean));
                attribute.Methods.Add(constructor);

                attribute.CustomAttributes.Add(attributeFactory.AttributeUsage(AttributeTargets.Parameter, inherited: false));
            }
            protected override void ImplementAttribute(ModuleDefinition module, TypeDefinition attribute, WellKnownTypes wellKnownTypes, CustomAttributeFactory attributeFactory)
            {
                attribute.CustomAttributes.Add(attributeFactory.CompilerGenerated());
                attribute.CustomAttributes.Add(attributeFactory.Embedded());

                var constructor = MethodFactory.Constructor(wellKnownTypes.TypeSystem);

                constructor.Parameters.Add(new ParameterDefinition(wellKnownTypes.TypeSystem.Boolean));
                attribute.Methods.Add(constructor);
            }
            protected override void ImplementAttribute(ModuleDefinition module, TypeDefinition attribute, WellKnownTypes wellKnownTypes, CustomAttributeFactory attributeFactory)
            {
                attribute.CustomAttributes.Add(attributeFactory.NullableContext(1));
                attribute.CustomAttributes.Add(attributeFactory.Nullable(0));
                attribute.CustomAttributes.Add(attributeFactory.AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue, allowMultiple: true, inherited: false));

                var constructor = MethodFactory.Constructor(wellKnownTypes.TypeSystem);

                constructor.Parameters.Add(new ParameterDefinition("parameterName", ParameterAttributes.None, wellKnownTypes.TypeSystem.String));
                attribute.Methods.Add(constructor);
            }
Пример #5
0
        private static TypeDefinition DefineNotNullWhenAttribute(AssemblyDefinition assemblyDefinition, WellKnownTypes wellKnownTypes, CustomAttributeFactory attributeFactory)
        {
            var attribute = new TypeDefinition(
                @namespace: "System.Diagnostics.CodeAnalysis",
                name: "NotNullWhenAttribute",
                TypeAttributes.NotPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit,
                wellKnownTypes.Module.ImportReference(wellKnownTypes.SystemAttribute));

            var constructor = MethodFactory.Constructor(wellKnownTypes);

            constructor.Parameters.Add(new ParameterDefinition("returnValue", ParameterAttributes.None, wellKnownTypes.TypeSystem.Boolean));
            attribute.Methods.Add(constructor);

            attribute.CustomAttributes.Add(attributeFactory.AttributeUsage(AttributeTargets.Parameter, inherited: false));

            assemblyDefinition.MainModule.Types.Add(attribute);

            return(attribute);
        }
Пример #6
0
        private static TypeDefinition DefineNotNullIfNotNullAttribute(AssemblyDefinition assemblyDefinition, WellKnownTypes wellKnownTypes, CustomAttributeFactory attributeFactory)
        {
            var attribute = new TypeDefinition(
                @namespace: "System.Diagnostics.CodeAnalysis",
                name: "NotNullIfNotNullAttribute",
                TypeAttributes.NotPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit,
                wellKnownTypes.Module.ImportReference(wellKnownTypes.SystemAttribute));

            attribute.CustomAttributes.Add(attributeFactory.NullableContext(1));
            attribute.CustomAttributes.Add(attributeFactory.Nullable(0));
            attribute.CustomAttributes.Add(attributeFactory.AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue, allowMultiple: true, inherited: false));

            var constructor = MethodFactory.Constructor(wellKnownTypes);

            constructor.Parameters.Add(new ParameterDefinition("parameterName", ParameterAttributes.None, wellKnownTypes.TypeSystem.String));
            attribute.Methods.Add(constructor);

            assemblyDefinition.MainModule.Types.Add(attribute);

            return(attribute);
        }