如何动态更改字典中键的类型? [英] How to dynamically change type of a key in a dictionary?
问题描述
大家好!
我需要一个小问题的帮助.我正在列出具有几个属性的客户列表,并且我想创建一个使用字典来遍历所有客户的搜索功能,但是该字典的键必须动态更改, 所以我可以使用多个属性作为搜索参数.
I need help with a small problem. I am making a list of customers that have a couple properties, and i would like to make a search function that will use dictionary to loop through all customers, but the key of that dictionary has to be dynamically changed, so i can use multiple properties as a search parameter.
这是我的课程:
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
我在这里吸引客户并将其添加到列表中:
Here i am making customers and adding them to the list:
Customer c1 = new Customer() { ID = 1, Name = "Mike", Salary = 5000 };
Customer c2 = new Customer() { ID = 2, Name = "John", Salary = 6500 };
Customer c3 = new Customer() { ID = 3, Name = "Steve", Salary = 3500 };
List<Customer> listOfCustomers = new List<Customer>();
listOfCustomers.Add(c1);
listOfCustomers.Add(c2);
listOfCustomers.Add(c3);
现在,我正在制作字典并使用foreach遍历它:
Now, i am making a dictionary and looping through it with foreach:
Dictionary<object, Customer> searchCustomers = listOfCustomers.ToDictionary(c => (object)c.ID, c => c);
foreach (KeyValuePair<object,Customer> customer in searchCustomers)
{
Customer x = customer.Value;
Console.WriteLine("Customer ID = {0}, Name = {1}, Salary = {2}",
x.ID, x.Name, x.Salary);
}
字典的值是Customer类型的,因此它将返回该实例的所有属性.
Value of the dictionary is of type Customer, so it will return all properties of that instance.
由于我将字典的键作为对象,所以我始终可以:
Since i made the key of dictionary as object, i can always do:
c => (object)c.Name
或
c => (object)c.Salary
它将根据该参数进行搜索.
And it will search by that parameter.
但是,我必须在代码中手动更改它.我有什么办法可以动态更改它,以便客户端可以自己选择要搜索的参数?
BUT, i have to change that manually in code. Is there any way that i can change this dynamically, so that the client can choose the parameter for search by himself?
这是我到目前为止尝试过的:
This is what i tried so far:
string key = "Name";
和
c => (object)c.key
所以我可以动态更改字符串,但是那样行不通.
so i could change string dynamically, but it does not work that way.
我也尝试过使用反射:
Type T = Type.GetType("Exercise.Customer");
PropertyInfo[] properties = T.GetProperties();
List<string> listOfProperties = new List<string>();
foreach (PropertyInfo property in properties)
{
listOfProperties.Add(property.Name);
}
但是结果基本上是一样的,因为我仍然会得到一个字符串值,就像我使用
But the outcome is basically the same, since i will still get a string value, just like when i did with
string key = "Name";
c => (object)c.key
c => (object)c.key
我最后尝试做的是列出属性类型列表:
The last thing i tried is making a list of property types:
List<Type> listOfProperties = new List<Type>();
foreach (PropertyInfo property in properties)
{
listOfProperties.Add(property.PropertyType);
}
我打印了该列表,它确实显示了Customer类中的所有类型的属性.
I printed the list and it does show all types of properties in class Customer.
因此,我尝试将字典的键设置为属性类型列表:
So, i tried to set the key of the dictionary as list of property types:
Dictionary<List<Type>, Customer> searchCustomers = listOfCustomers.ToDictionary(???);
但是我不知道这是否有可能,以及如何在此处设置键和值参数.
But i have no idea is this even possible, and how to set the key and value parameters here.
请帮助! :)
推荐答案
您可以创建扩展方法或常规方法,以委托作为输入,并留在调用方上以指定要分配的属性用于字典中的键:
You can create an extension method or a normal method which takes a delegate as input and leave on the caller to specify which property to use for Key in dictionary :
public static Dictionary<TKey, Customer> ToDictionary<TKey>(Func<Customer,TKey> key,List<Customer> customers)
{
var dict = new Dictionary<TKey, Customer>();
foreach(var item in customers)
{
dict.Add(key.Invoke(item),item);
}
return dict;
}
您可以像现在这样调用它,我现在在Customer类上对其进行了定义,因此您可以在其中定义方法:
and you can call it like, i defined it on Customer class for now, so it is up to you where you want to define the method:
Customer.ToDictionary(x=>x.ID, listOfCustomers);
希望有帮助!
这篇关于如何动态更改字典中键的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!