Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created March 19, 2010 19:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
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