简体   繁体   中英

How to extend switch case dynamic property getter in base class?

I have a base class that fetches attributes like below, to map between JSON/C# names:

[JsonPropertyName("foo")]
[DataMember(Name = "foo")]
public FooAttribute { get; set; }

[JsonPropertyName("bar")]
[DataMember(Name = "bar")]
public BarAttribute { get; set; }

public object? this[string attributeName]
        {
            get
            {
                switch (attributeName)
                {
                    case "foo":
                        return FooAttribute;
                    case "bar":
                        return BarAttribute;
                    case ....
    
                    default:
                        try
                        {
                            return Attributes[attributeName];
                        }
                        catch (KeyNotFoundException)
                        {
                            return null;
                        }
                }
            }
        }

But now I want to make derived classes that implement all of the above, with additional attributes such as ChildA: spam : SpamAttribute and ChildB: eggs : EggsAttribute .

How can I accomplish that, without copy-pasting the entire switch case?

I don't want to contradict your concept of base class with string indexer and storing additional values in Attributes dictionary. It isn't the best of, but I have already met this solution in the past too.

class Base
{
    private IDictionary<string, object> Attributes = new Dictionary<string, object>(StringComparer.Ordinal);

    public virtual object this[string attributeName]
    {
        get
        {
            switch (attributeName)
            {
                default:
                    Attributes.TryGetValue(attributeName, out var valueOrNull);
                    return valueOrNull;
            }
        }
    }
}

class Descendant1 : Base
{
    // ... FooAttribute and BarAttribute props here ...

    public override object this[string attributeName]
    {
        get
        {
            switch (attributeName)
            {
                case "foo":
                    return FooAttribute;
                case "bar":
                    return BarAttribute;
                default:
                    return base[attributeName];
            }
        }
    }
}

class Descendant2 : Descendant1
{
    // ... BaconAttribute and EggsAttribute props here ...

    public override object this[string attributeName]
    {
        get
        {
            switch (attributeName)
            {
                case "bacon":
                    return BaconAttribute;
                case "eggs":
                    return EggsAttribute;
                default:
                    return base[attributeName];
            }
        }
    }
}

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