Here is a simple implementation of generic method that get value from array in class and cast it to a type.
private T GetReferenceFieldValue<T>(ReferenceRow referenceRow, string fieldName)
{
var referenceField = referenceRow.field.FirstOrDefault(x => x.name == fieldName);
if (referenceField != null)
{
//Dictionary for actions with types
var typeActions = new Dictionary<Type, Func<object>>
{
{ typeof(DateTime), () => { return DateTime.Parse(referenceField.Item.ToString()); } },
{ typeof(int), () => { return int.Parse(referenceField.Item.ToString()); } },
{ typeof(string), () => { return referenceField.Item.ToString(); } },
{ typeof(Guid), () => { return new Guid(referenceField.Item.ToString()); } },
};
var fieldValue = typeActions[typeof(T)]();
return (T)Convert.ChangeType(fieldValue, typeof(T));
}
return default(T);
}