此示例代码说明了如何完成此操作。请注意,这假定要索引的每个属性都是唯一的。在此示例中,EmployeeID 对于每个员工都是唯一的。为了处理非独特情况,您需要修改代码以具有以下内容:
Dictionary<string, Dictionary<int, List<T>>> intIndexes = new Dictionary<string, Dictionary<int, List<T>>>();
代替:
Dictionary<string, Dictionary<int, T>> intIndexes = new Dictionary<string, Dictionary<int, T>>();
此外,您必须重新定义 getByPropertyValue 如下:
public List<T> getByPropertyValue(string propertyName, int propertyValue)
理想情况下,最好提供指示该属性是否独特的提示。这不是一个完整的实现,但您应该了解如何使用反射来实现您想要的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IndexerSampleCode
{
class Program
{
static void Main(string[] args)
{
Indexer<Employee> indexer = new Indexer<Employee>();
Employee e = new Employee();
e.EmployeeID = 45;
e.FirstName = "Tarik";
e.LastName = "Hoshan";
e.BirthDate = new DateTime(1965, 2, 18);
indexer.add(e);
var e2 = indexer.getByPropertyValue("EmployeeID", 45);
Console.WriteLine(e2.FirstName);
Console.ReadKey();
}
}
class Indexer<T>
{
// Collection of dictionories that will be used to index properties of type int
Dictionary<string, Dictionary<int, T>> intIndexes = new Dictionary<string, Dictionary<int, T>>();
public Indexer() {
System.Type indexerType = this.GetType().UnderlyingSystemType;
System.Type elementType = indexerType.GetGenericArguments()[0];
var members = elementType.GetProperties();
// Loop through each property and create a Dictionary corresponding to it
foreach (var member in members)
{
if (member.PropertyType == typeof(int))
{
intIndexes.Add(member.Name, new Dictionary<int, T>());
}
}
}
public T getByPropertyValue(string propertyName, int propertyValue)
{
Dictionary<int, T> index = intIndexes[propertyName];
return index[propertyValue];
}
public void add(T o) {
var type = o.GetType();
var members = type.GetProperties();
foreach (var member in members)
{
if (member.PropertyType == typeof(int))
{
var propertyName = member.Name;
Dictionary<int, T> index = intIndexes[propertyName];
int value = (int) o.GetType().GetProperty(propertyName).GetValue(o, null);
index.Add(value, o);
}
}
}
}
// Sample test class
class Employee
{
public DateTime BirthDate
{
set;
get;
}
public string FirstName
{
set;
get;
}
public string LastName
{
set;
get;
}
public int EmployeeID {
set;
get;
}
}
}