public static class TypeConverter
{
public static object ConvertToType(string value, Type type)
{
if (type == typeof(string))
{
return value;
}
else if (type == typeof(int) || type == typeof(int?))
{
return int.TryParse(value, out var result) ? (object)result : null;
}
else if (type == typeof(bool) || type == typeof(bool?))
{
return bool.TryParse(value, out var result) ? (object)result : null;
}
else if (type == typeof(DateTime) || type == typeof(DateTime?))
{
return DateTime.TryParse(value, out var result) ? (object)result : null;
}
else
{
return Convert.ChangeType(value, type);
}
}
}
Что произойдет при создании привязки, если одно из указанных полей в строке полей не существует для типа T? Как с этим справиться?
The GetTypeBuilder method seems to define a new type in a dynamically created assembly. Is this necessary, and is there a better approach to dynamically select fields from an IQueryable?
Подробнее здесь: https://stackoverflow.com/questions/786 ... -queryable