gpt4 book ai didi

保留属性的 C# Typedef

转载 作者:太空狗 更新时间:2023-10-29 21:30:32 24 4
gpt4 key购买 nike

问题:我有 Dictionary<String, String>我需要一个别名,但我还需要对其进行序列化/反序列化。

我尝试过的解决方案:

class Foo : Dictionary<String, String> { }

但这行得通,因为我必须创建一个反序列化构造函数,而当 Dictionary 已经可以反序列化时,这会有点愚蠢。

我也试过

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

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

我该怎么办?

最佳答案

使用 alias 方法属性被保留,但你说这是太多的开销(每个文件等)。

类型级属性通常保留 - 但它取决于属性 - 对于[Serializable],注意它有:

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

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

就我个人而言,我可能会专注于让序列化 ctor/回调在第一个示例中工作——我怀疑这需要更多的努力。以下似乎不错:

[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();
}
}

关于保留属性的 C# Typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4894856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com