C# multiple attribute to single custom attribute

Viewed 219

I have a azure function (HTTP trigger) which uses attributes of the OpenApi NuGet package. See a example of my function:

[FunctionName(nameof(GetTestModel))]
[OpenApiOperation(
    operationId: nameof(GetTestModel),
    Summary = $"Summary")]
[OpenApiParameter(
    name: "id",
    Description = "The Id of the object.",
    Required = true)]
[OpenApiParameter(
    name: nameof(TestRequestModel.TraceId),
    Description = "The TraceId/CorrelationId to use for this request path",
    In = ParameterLocation.Header)]
[OpenApiResponseWithBody(
    HttpStatusCode.OK,
    "application/json",
    typeof(TestResponseModel),
    Description = $"An object containing all information about the retrieved {nameof(TestResponseModel)}.")]
[OpenApiResponseWithBody(
    HttpStatusCode.NotFound,
    "application/json",
    typeof(CustomHttpError),
    Description = "An object containing information about occured errors/exceptions")]
public async Task<ActionResult<TestResponseModel>> Run(
    [HttpTrigger(AuthorizationLevel.Function, "GET", Route = "test")]
    TestRequestModel request)
{
    // do stuff.
}

As you can see that are a lot of attributes. I would like to reduce it to 1 custom attribute, like this:

[FunctionName(nameof(GetTestModel))]
[OpenApiCustom(nameof(GetTestModel), typeof(TestResponseModel), typeof(TestResponseModel)]
public async Task<ActionResult<TestResponseModel>> Run(
    [HttpTrigger(AuthorizationLevel.Function, "GET", Route = "test")]
    TestRequestModel request)
{
    // do stuff.
}

Is this possible?

1 Answers

This is possible. you can create custom Attributes. with multiple attributes variable but not duplicate.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunctionApp1
{
    internal class CustomAttribute : Attribute
    {
        private int version;
        private string packagename;
        private string applicationname;
        private string applicationversion;
        public CustomAttribute(string _name, string _address, string _description, int _version, string _packagename, string _applicationname, string _applicationversion)
        {
            this.version = _version;
            this.packagename = _packagename;
            this.applicationname = _applicationname;
            this.applicationversion = _applicationversion;
        }
    }

    public class YourAttribute : Attribute
    {
        private int version;
        private string packagename;
        private string applicationname;
        private string applicationversion;
        public YourAttribute(string _name, string _address,string _description, int _version, string _packagename, string _applicationname, string _applicationversion)
        {
            this.version = _version;
            this.packagename = _packagename;
            this.applicationname = _applicationname;
            this.applicationversion = _applicationversion;
        }
    }
}

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NSwag.Annotations;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        [Custom("NameString","AddressString","DescriptionString",1,"ApplicationPackageNamestring","ApplicationNameString","ApplicationVersionString")]
        [Your("NameString", "AddressString", "DescriptionString", 1, "ApplicationPackageNamestring", "ApplicationNameString", "ApplicationVersionString")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            return new OkObjectResult("Successfully");
        }
    }
}

More Information about Creating Custom Attributes

Related