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);
            }
예제 #2
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);
        }