mercredi 13 novembre 2019

Decide Value of custom attribute at runtime

I have the following scenario:

/*attribute to be filled*/
public class Flower
{
    public FlowerType Type
    {
        get;
        set;
    }

    public string Value { get; set; }
}


public enum FlowerType 
{
    Rose,
    Daisy,
    Tulip
}


[AttributeUsage(AttributeTargets.Class)]
public sealed class FlowerAttribute : Attribute
{
    public string FlowerValue{ get; }

    public FlowerAttribute(string flowerValue)
    {
        FlowerValue = flowerValue;
    }
}

What I am trying to achieve is a way to give the class Flower an attribute depending on runtime, meaning that if the flower will be instantiated as a Rose type, then the attribute on top of Flower will have the FlowerValue set to "Rose". Can I achieve this somehow? Thanks as always in advance!





Aucun commentaire:

Enregistrer un commentaire