如何创建最多接受 4 个参数的构造函数?

我正在创建一个最多接受 4 个参数的属性。


我这样编码:


internal class BaseAnnotations

{


    public const string GET = "GET";

    public const string POST = "POST";

    public const string PATCH = "PATCH";

    public const string DELETE = "DELETE";



    public class OnlyAttribute : Attribute

    {

        public bool _GET = false;

        public bool _POST = false;

        public bool _PATCH = false;

        public bool _DELETE = false;


        public OnlyAttribute(string arg1)

        {

            SetMethod(arg1);

        }


        public OnlyAttribute(string arg1, string arg2)

        {

            SetMethod(arg1);

            SetMethod(arg2);

        }


        public OnlyAttribute(string arg1, string arg2, string arg3)

        {

            SetMethod(arg1);

            SetMethod(arg2);

            SetMethod(arg3);

        }


        public OnlyAttribute(string arg1, string arg2, string arg3, string arg4)

        {

            SetMethod(arg1);

            SetMethod(arg2);

            SetMethod(arg3);

            SetMethod(arg4);

        }


        public void SetMethod(string arg)

        {

            switch (arg)

            {

                case GET: _GET = true; break;

                case POST: _POST = true; break;

                case PATCH: _PATCH = true; break;

                case DELETE: _DELETE = true; break;

            }

        }

    }

}

我需要像这样使用它:


public class ExampleModel : BaseAnnotations

{

    /// <summary>

    /// Example's Identification 

    /// </summary>

    [Only(GET, DELETE)]

    public long? IdExample { get; set; }


    // ...

有没有办法只在上面的 4 个构造函数中的一个构造函数中进行编码以避免重复?


我在想像 JavaScript 的 spread operator 之类的东西(...args) => args.forEach(arg => setMethod(arg))。


提前致谢。


婷婷同学_
浏览 217回答 3
3回答

子衿沉夜

我建议在这里重新考虑您的设计。考虑:[Flags]public enum AllowedVerbs{&nbsp; &nbsp; None = 0,&nbsp; &nbsp; Get = 1,&nbsp; &nbsp; Post = 2,&nbsp; &nbsp; Patch = 4,&nbsp; &nbsp; Delete = 8}public class OnlyAttribute : Attribute{&nbsp; &nbsp; private readonly AllowedVerbs _verbs;&nbsp; &nbsp; public bool Get => (_verbs & AllowedVerbs.Get) != 0;&nbsp; &nbsp; public bool Post => (_verbs & AllowedVerbs.Post) != 0;&nbsp; &nbsp; public bool Patch => (_verbs & AllowedVerbs.Patch) != 0;&nbsp; &nbsp; public bool Delete => (_verbs & AllowedVerbs.Delete ) != 0;&nbsp; &nbsp; public OnlyAttribute(AllowedVerbs verbs) => _verbs = verbs;}然后调用者可以使用:[Only(AllowedVerbs.Get)]或者[Only(AllowedVerbs.Post | AllowedVerbs.Delete)]

白衣非少年

好的答案,但请考虑使用 4 个属性。对于您的示例,这可能有效。public class GetAttribute: Attribute {}public class PostAttribute: Attribute {}public class PatchAttribute: Attribute {}public class DeleteAttribute: Attribute {}[GET] [DELETE]public long? IdExample { get; set; }它简单而直接。当然还有更多属性,但您可能有更多需要它们的实例。每个属性都有一个默认构造函数。每个操作的属性仅存在就足以传达允许的内容。

料青山看我应如是

&nbsp;public OnlyAttribute(params string[] parameters)&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; if (parameters.Length > 4) throw new ArugumentException(nameof(parameters));&nbsp; &nbsp; &nbsp; &nbsp; foreach (var param in parameters)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetMethod(param);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP