简体   繁体   English

将枚举分配为属性c#

[英]Assign enum as property c#

I did not find any answer on the forum (however if there is one please let me know). 我在论坛上没有找到任何答案(但是,如果有答案,请告诉我)。 I am writing backend structure for the ASP.NET MVC app and have some troubles with C# case. 我正在为ASP.NET MVC应用程序编写后端结构,并且在C#情况下遇到一些麻烦。

I am wondering if such a solution is possible to happen (and need to do a following thing the way I described or similarly). 我想知道是否有可能实现这样的解决方案(并且需要按照我的描述或类似方式做以下事情)。 I will show the problem in the example below, because I do not know how to say it clearly in words. 我将在下面的示例中显示该问题,因为我不知道如何用语言清楚地表达它。

I have defined Enum as following: 我将枚举定义如下:

public enum myEnum
{
    FirstOpt = 0,
    SecondOpt= 1,
    ThirdOpt = 2,

    Unknown = -1,
}

Now, I want to assign myEnum in the different part of my solution in the custom attribute. 现在,我想在解决方案的不同部分的custom属性中分配myEnum。

The attribute looks the following way: 该属性如下所示:

    public class SearchTypeAttribute : Attribute
    {
        public Type SType { get; set; }

        public IEnumerable<object> SItems { get; set; }
    }

Then I want to use it in the following way: 然后,我想通过以下方式使用它:

public class SomeClass
{
    [Key]
    public long Id { get; set; }

    [SearchTypeAttribute(
        SType = typeof(Enums.myEnum),
        SItems =  Enum.GetValues(typeof(Enums.myEnum))
    )]
    public string Type{ get; set; } // later to this item will be assigned string name of the assigned value
 }

When I am doing this, the following error appears: 当我这样做时,出现以下错误:

Error CS0655 'SItems' is not a valid named attribute argument because it is not a valid attribute parameter type 错误CS0655'SItems'不是有效的命名属性参数,因为它不是有效的属性参数类型

I was also trying to assign it as here: 我也在尝试将其分配为:

public class SomeClass
{
    [Key]
    public long Id { get; set; }

    [SearchTypeAttribute(
        SType = typeof(Enums.myEnum),
        SItems =  Enums.myEnum // here !
    )]
    public string Type{ get; set; } 

 }

But I still have no idea what "Type" of property I should use in my SearchTypeAttribute, to be able to assign those values there. 但是我仍然不知道应该在SearchTypeAttribute中使用什么“类型”属性,以便在那里分配这些值。

I am doing this to be able to generate different types of fields in search bars in the views later. 我这样做是为了稍后能够在搜索栏中生成不同类型的字段。 Then in my code I want to assign the list of enum values or the specific enum to some variable, so I can then, operate on those values. 然后在我的代码中,我想将枚举值或特定枚举的列表分配给某个变量,这样我就可以对这些值进行操作。

What types should I use to assign this type of data SItems ? 我应该使用什么类型来分配这种类型的数据SItem Is there other approach to do it ? 还有其他方法吗? I am not yet really advanced in c#. 我在C#方面还不是很先进。 Thank you for any help in advance. 感谢您的任何帮助。

An attribute is a compile-time thing. 属性是编译时的东西。 So you have to provide all the information at compile-time also. 因此,您还必须在编译时提供所有信息。 However Enum.GetValues will be executed at runtime , making it impossible to be used for an attribute. 但是Enum.GetValues将在运行时执行,因此无法将其用于属性。 The only way to achieve this is by writing the possible enum-values directy into the attribute: 实现此目的的唯一方法是直接将可能的枚举值写入属性:

public class SomeClass
{
    [Key]
    public long Id { get; set; }

    [SearchTypeAttribute(
        SType = typeof(Enums.myEnum),
        SItems =  new[] { Enums.FirstOpt, Enums.SecondOpt, ...}
    )]
    public string Type{ get; set; } 

 }

Apart from this I can´t see why your SItems is of type IEnumerable<object> , when it obviously has only Enums -elements in it. 除此之外,当它显然只包含Enums SItems ,我看不到为什么您的SItemsIEnumerable<object>类型。 It´s not even possible to use an IEnumerable on an attribute, only arrays of primitive types are allowed, as mentioned here . It's甚至不能使用IEnumerable一个属性,只有基本类型数组是允许的,提到这里 So SItems should be an Enums[] . 所以SItems应该是Enums[]

Another approach is to rely on the attributes constructor and initialize SItems from there: 另一种方法是依靠属性构造函数并从那里初始化SItems

public class SearchTypeAttribute : Attribute
{
    public SearchTypeAttribute(Type type)
    {
        this.SType = type;
        this.SItems = Enum.GetValues(type);
    }

}

Now simply use the attribute as follows: 现在,只需使用以下属性:

[SearchTypeAttribute(typeof(Enums.myEnum))]
public string Type{ get; set; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM