2011-02-04 70 views
6

Vấn đề: Tôi có Dictionary<String, String> mà tôi cần một bí danh, nhưng tôi cũng cần phải tuần tự hóa/deserialize nó.C# Typedef giữ nguyên các thuộc tính

Solutions Tôi đã thử:

class Foo : Dictionary<String, String> { } 

nhưng mà làm việc vì tôi sẽ phải tạo một constructor Deserialization và đó sẽ là một chút ngớ ngẩn khi từ điển đã có thể được deserialized.

Tôi cũng đã cố gắng

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

nhưng tôi cần điều này để làm việc trong hơn một tập tin và nếu thêm dòng rằng trong tất cả các tập tin mà cần đến nó, tôi sẽ được loại bỏ một nửa số điểm của typedefs (mà là, nếu tôi cần thay đổi loại, tôi có thể làm điều đó dễ dàng)

Tôi có thể làm gì với điều này?

+0

Tại sao bạn cần một bí danh? Những gì problwm là bạn đang cố gắng để giải quyết? –

+0

Những lý do thông thường cần bí danh: Vì vậy, tôi có thể dễ dàng làm cho nó trở thành một lớp sau này nếu cần thiết và để mọi người không cố gắng coi đó là loại từ điển sai. Chỉnh sửa: cũng vì vậy nếu tôi cần phải thay đổi khóa hoặc giá trị thành một cái gì đó khác hơn là một chuỗi dễ dàng của nó. – Drew

+0

Đây có phải là chuỗi tuần tự 'BinaryFormatter' không? hay cái nào? –

Trả lời

6

Với thuộc tính phương thức tiếp cận bí danh được giữ nguyên, nhưng bạn cho biết quá nhiều chi phí (mỗi tệp, v.v.).

Loại cấp thuộc tính được genercally bảo quản - nhưng nó phụ thuộc vào thuộc tính - cho [Serializable], lưu ý rằng nó có:

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

các Inherited = false là đáng kể - tức là nó không được thừa kế.

Cá nhân tôi có lẽ sẽ tập trung vào việc nhận được các ctor/callback tuần tự làm việc trong ví dụ đầu tiên - tôi nghi ngờ nó sẽ cần nhiều nỗ lực hơn nữa. Sau đây có vẻ tốt đẹp:

[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) { } 
} 

Tuy nhiên, đây là một sự thay thế thông qua đóng gói: "đó sẽ là một chút ngớ ngẩn khi từ điển đã có thể được deserialized"

[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

Mmm Tôi sẽ không nói đó là ngớ ngẩn để gọi một ctor cơ sở tại (gần như) mọi trường hợp + đã ghi bàn 1 nỗ lực tối thiểu, vì vậy tôi muốn nói làm điều đó ...

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

} 

hoặc

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

} 
Các vấn đề liên quan