[英]c# generics how to cast return type?
i have a dictionary 我有一本字典
var dictionary = new Dictionary<string,string>{{"name","Smith"},{"age","20"}};
and i want to map it's values to a class 我想将其值映射到一个类
class Person{
public string Name {get;set;}
public string Age {get;set;}
}
i tried to do it in this way 我试图以这种方式做到这一点
public T Map<T>(Dictionary<string, object> row)
{
var p= new Person();
if (row.ContainsKey("name")) Person.Name= row["name"];
if (row.ContainsKey("age")) Person.Age= row["age"];
return (T) p;
}
and the problem is that i fail at return type, i dont know how to cast T to class Person. 问题是我在返回类型时失败,我不知道如何将T强制转换为Person类。 any ideas ? 有任何想法吗 ? Thanks. 谢谢。
The issue is with this line: 问题在于此行:
var p= new Person();
You are trying to return a Person
type, though T
can be anything . 您正在尝试返回一个Person
类型,尽管T
可以是任何东西 。
You need to add a generic type constraint to the class, only allowing T
to be a Person
- though that wouldn't be very useful. 您需要向该类添加一个通用类型约束 ,只允许T
成为Person
-尽管那不是很有用。
But in your case, generics are not even needed: 但是在您的情况下,甚至不需要泛型:
public Person Map(Dictionary<string, string> row)
{
var p= new Person();
if (row.ContainsKey("name")) Person.Name= row["name"];
if (row.ContainsKey("age")) Person.Age= row["age"];
return p;
}
T is just a type. T只是一种类型。 You wouldn't be casting it to anything. 您不会将其投射到任何东西上。 If you had an instance of T, you would have (Person)T. 如果您有T的实例,那么您将有(Person)T。
You can also do: where T: Person assuming your question is just worded in reverse. 您也可以执行以下操作:其中T:假设您的问题的人的措词恰好相反。
If you had a dictionary of the form: 如果您有以下形式的字典:
new Dictionary<string,string>{{"Smith", "20"},{"Paul","44"}}
You can do this easily with LINQ. 您可以使用LINQ轻松做到这一点。 Something like: 就像是:
dictionary
.Select(k=>new Person()
{
Name=k.Key,
Age=k.Value
})
.ToList();
With the dictionary you have, you'd have to do something like: 使用字典,您必须执行以下操作:
public Person GetPersonFromDictionary(Dictionary<string,string> dictionary)
{
var myPerson = new Person()
{
Name=dictionary[name],
Age=dictionary[age]
}
return myPerson;
}
Assuming you were guaranteed the dictionary had the values you needed. 假设您可以保证字典具有所需的值。 I don't see any need to involve generics here. 我认为这里不需要涉及泛型。
Either: 要么:
public Person Map(Dictionary<string, object> row)
{
var p= new Person();
if (row.ContainsKey("name")) Person.Name= row["name"];
if (row.ContainsKey("age")) Person.Age= row["age"];
return p;
}
Or: 要么:
public T Map<T>(Dictionary<string, object> row)
where T : Person
{
var p= new Person();
if (row.ContainsKey("name")) Person.Name= row["name"];
if (row.ContainsKey("age")) Person.Age= row["age"];
return (T) p;
}
Or: 要么:
public T Map<T>(Dictionary<string, object> row)
where T : Person, new ()
{
var p= new T();
if (row.ContainsKey("name")) Person.Name= row["name"];
if (row.ContainsKey("age")) Person.Age= row["age"];
return p;
}
You can use reflection to populate the properties: 您可以使用反射来填充属性:
public static T Map<T>(Dictionary<string, string> dictionary) where T : class, new()
{
var obj = new T();
var properties = typeof(T).GetProperties();
foreach (var item in dictionary)
{
var prop = properties.FirstOrDefault(p => p.Name.Equals(item.Key, StringComparison.InvariantCultureIgnoreCase));
if (prop != null)
prop.SetValue(obj, item.Value, null);
}
return obj;
}
... ...
var dictionary = new Dictionary<string, string> { { "name", "Smith" }, { "age", "20" } };
Person o = Map<Person>(dictionary);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.