C#Typedef保留属性

时间:2011-02-04 05:27:51

标签: c# serialization typedef alias

问题:我有Dictionary<String, String>我需要别名,但我还需要序列化/反序列化。

我尝试过的解决方案:

class Foo : Dictionary<String, String> { }

但是这样做是因为我必须创建一个Deserialization构造函数,当Dictionary已经被反序列化时,这会有点傻。

我也试过

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

但我需要这个工作在更多的那个文件中,如果在所有需要它的文件中添加该行,我将删除typedef的一半(即,如果我需要更改类型,我可以轻松地做到这一点)

我该怎么办?

2 个答案:

答案 0 :(得分:6)

使用别名方法保留了属性,但是您声明开销过多(每个文件等)。

类型级属性 一般保留 - 但它取决于属性 - 对于[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();
    }
}

答案 1 :(得分:1)

嗯“当字典已经被反序列化时,这会有点傻。”我不会说在任何情况下(几乎)在任何情况下调用一个基础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)
    {
    }   

}