using System.Collections.Generic;
public static void Main()
var xx = new List<int>{1};
var yy = new List<int?>{};
var zz = new List<int?>{1};
Console.WriteLine(GetValue<int?>(1));
Console.WriteLine(GetValue<int>(xx));
Console.WriteLine(GetValue<long>(xx));
Console.WriteLine(GetValue<int?>(xx));
Console.WriteLine(GetValue<long?>(xx));
Console.WriteLine(GetValue<int>(yy));
Console.WriteLine(GetValue<long>(yy));
Console.WriteLine(GetValue<int?>(yy));
Console.WriteLine(GetValue<long?>(yy));
Console.WriteLine(GetValue<int>(zz));
Console.WriteLine(GetValue<long>(zz));
Console.WriteLine(GetValue<int?>(zz));
Console.WriteLine(GetValue<long>(zz));
public static T GetValue<T>(object value)
if (value == null || value == DBNull.Value)
else if (value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition().Equals(typeof(List<>)))
var list = ((System.Collections.IList)value).Cast<object>();
return GetValue<T>(list.Single());
if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
type = Nullable.GetUnderlyingType(type);
return (T)Convert.ChangeType(value, type);