Created
July 7, 2023 19:51
-
-
Save IJsWorkshop/ef9564b7a72b203709f237a5304bfce6 to your computer and use it in GitHub Desktop.
Basic custom generic DeSerializer
This file contains 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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Handles DateTime and Guid's type