Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save IJsWorkshop/ef9564b7a72b203709f237a5304bfce6 to your computer and use it in GitHub Desktop.
Save IJsWorkshop/ef9564b7a72b203709f237a5304bfce6 to your computer and use it in GitHub Desktop.
Basic custom generic DeSerializer
public static T DeSerializer<T>(Dictionary<string, IStorageElement> element)
{
var o = (T)Activator.CreateInstance<T>();
var props = o.GetType().GetProperties();
foreach (var prop in props)
{
// match element name with property name set value
if (element.TryGetValue(prop.Name, out var e))
{
// type override
if (prop.PropertyType.Name == "Guid")
{
Guid result = new Guid(e.Value.ToString());
prop.SetValue(o, result);
}
else
if (prop.PropertyType.Name == "DateTime")
{
if (DateTime.TryParse(e.Value.ToString(), out DateTime r))
{
prop.SetValue(o, r);
}
}
else
{
prop.SetValue(o, e.Value);
}
}
}
return o;
}
@IJsWorkshop
Copy link
Author

Handles DateTime and Guid's type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment