簡體   English   中英

C#Typedef保留屬性

[英]C# Typedef that preserves attributes

問題:我有Dictionary<String, String>我需要別名,但我還需要序列化/反序列化它。

我試過的解決方案:

class Foo : Dictionary<String, String> { }

但是那個工作因為我必須創建一個Deserialization構造函數,當Dictionary已經被反序列化時會有點傻。

我也試過了

using Foo = System.Collections.Generic.Dictionary<String, String>;

但我需要這個工作在更多的那個文件中,如果在所有需要它的文件中添加該行,我將刪除一半的typedef(也就是說,如果我需要更改類型,我可以這樣做容易)

我該怎么辦?

使用別名方法保留屬性,但是您聲明開銷過多(每個文件等)。

類型級屬性通常保留 - 但它取決於屬性 - 對於[Serializable] ,請注意它具有:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct
| AttributeTargets.Enum | AttributeTargets.Delegate, Inherited = false)]

Inherited = false是重要的 - 即它不是繼承的。

就個人而言,我可能會專注於在第一個例子中使序列化ctor / callbacks工作 - 我懷疑它需要更多的努力。 以下似乎很好:

[Serializable]
public class Foo: Dictionary<string, string> {
    public Foo() : base() { }
    public Foo(SerializationInfo info, StreamingContext context) : base(info, context) { }
    public Foo(int capacity) : base(capacity) { }
    public Foo(IEqualityComparer<string> comparer): base(comparer) {}
    public Foo(IDictionary<string,string> dictionary) : base(dictionary) { }
    public Foo(int capacity, IEqualityComparer<string> comparer) : base(capacity, comparer) { }
    public Foo(IDictionary<string, string> dictionary, IEqualityComparer<string> comparer) : base(dictionary, comparer) { }
}

但是,這是封裝的替代方案:

[Serializable]
public class Foo : IDictionary<string,string>
{
    private readonly Dictionary<string, string> inner = new Dictionary<string, string>();

    public void Add(string key, string value)
    {
        inner.Add(key, value);
    }

    public bool ContainsKey(string key)
    {
        return inner.ContainsKey(key);
    }

    public ICollection<string> Keys
    {
        get { return inner.Keys; }
    }

    public bool Remove(string key)
    {
        return inner.Remove(key);
    }

    public bool TryGetValue(string key, out string value)
    {
        return inner.TryGetValue(key, out value);
    }

    public ICollection<string> Values
    {
        get { return inner.Values; }
    }

    public string this[string key]
    {
        get
        {
            return inner[key];
        }
        set
        {
            inner[key] = value;
        }
    }

    void  ICollection<KeyValuePair<string, string>>.Add(KeyValuePair<string, string> item)
    {
        ((IDictionary<string,string>)inner).Add(item);
    }

    public void Clear()
    {
        inner.Clear();
    }

    bool ICollection<KeyValuePair<string, string>>.Contains(KeyValuePair<string, string> item)
    {
        return ((IDictionary<string, string>)inner).Contains(item);
    }

    void  ICollection<KeyValuePair<string, string>>.CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
    {
        ((IDictionary<string, string>)inner).CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get { return inner.Count; }
    }

    bool  ICollection<KeyValuePair<string, string>>.IsReadOnly
    {
        get { return ((IDictionary<string, string>)inner).IsReadOnly; }
    }

    bool ICollection<KeyValuePair<string, string>>.Remove(KeyValuePair<string, string> item)
    {
        return ((IDictionary<string, string>)inner).Remove(item);
    }

    public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
    {
        return inner.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return inner.GetEnumerator();
    }
}

嗯“當字典已經被反序列化時,這會有點傻。” 我不會說在任何情況下(幾乎)在任何情況下調用一個基礎ctor很愚蠢+這是1分鍾的努力,所以我會說這樣做......

[Serializable]
public class Foo : Dictionary<string, string>
{
    public Foo()
        : base()
    {
    }
    public Foo(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }   

}

要么

[Serializable]
public class Foo<TKey,TValue> : Dictionary<TKey,TValue>
{
    public Foo()
        : base()
    {
    }
    public Foo(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }   

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM