Why is a Nullable not a valid Custom Attribute Parameter when T is?

前端 未结 4 2128
猫巷女王i
猫巷女王i 2020-12-30 22:07

If I have an enum like this

public enum Hungry
{
    Somewhat,
    Very,
    CouldEatMySocks
}

and a custom attribute like this

<         


        
4条回答
  •  天命终不由人
    2020-12-30 22:54

    Hungry? is equal to Nullable, which in terms mean that

    [Hunger(NullableHungerLevel = Hungry.CouldEatMySocks)]
    

    is equal to

    [Hunger(NullableHungerLevel = new Nullable(Hungry.CouldEatMySocks))]
    

    Since you can only use constant values in named attribute arguments you will have to resort to Shimmy's solution.

提交回复
热议问题