简体   繁体   中英

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.

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.

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

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.

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 ? Is there other approach to do it ? I am not yet really advanced in 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. 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. It´s not even possible to use an IEnumerable on an attribute, only arrays of primitive types are allowed, as mentioned here . So SItems should be an Enums[] .

Another approach is to rely on the attributes constructor and initialize SItems from there:

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; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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