Created
March 19, 2010 19:19
-
-
Save jpoehls/338077 to your computer and use it in GitHub Desktop.
a SerializationInfoHelper class that makes it easier to pull values out of SerializationInfo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Runtime.Serialization; | |
namespace Helpers | |
{ | |
public class SerializationInfoHelper | |
{ | |
private readonly SerializationInfo _info; | |
private bool _enumerated = false; | |
private Dictionary<string, object> _items; | |
public SerializationInfoHelper(SerializationInfo info) | |
{ | |
_info = info; | |
} | |
public T Get<T>(string name) | |
{ | |
Enumerate(); | |
if (_items.ContainsKey(name)) | |
{ | |
return (T)_items[name]; | |
} | |
return default(T); | |
} | |
public bool Contains(string name) | |
{ | |
Enumerate(); | |
return _items.ContainsKey(name); | |
} | |
private void Enumerate() | |
{ | |
if (_enumerated) | |
return; | |
_items = new Dictionary<string, object>(_info.MemberCount); | |
var e = _info.GetEnumerator(); | |
while (e.MoveNext()) | |
{ | |
_items.Add(e.Name, e.Value); | |
} | |
_enumerated = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment