有没有办法获得给定类型的默认比较器,其中类型是可变的并且仅在运行时知道?考虑以下:
var RT = typeof(string);
var comparer = EqualityComparer<RT>.Default;
这显然不编译,但如果编译,comparer应该有一个等于的值EqualityComparer<string>.Default
我能想到的唯一方法是制作一个可以通过反射调用的“盒装”比较器(见下文)。这行得通,但它很麻烦。有没有更好的方法来做到这一点?
澄清一下,由于反射,这不是一个好主意,但为什么我需要它?
有问题的算法是大型遗留搜索 API 的一部分。消费者将对象列表(例如,List<Person>)传递给 API,在内部创建类型特定的索引(使用反射),以便调用者随后可以搜索该对象中的任何字段(例如,可能是姓氏)。这通常不是必需的,但在我正在服务的用例中,我们正在搜索非常大的集合与其他非常大的集合。为此目的,数据库存储过程可能更好。但是现在我需要修补这个遗留 API 以支持用户定义的比较算法,并且还支持用户选择不提供任何比较算法的情况,我只知道运行时类型RT。
// Example usage
// Assume "RT" is a Type known only at runtime (e.g., typeof(string))
var box = typeof(BoxedComparer<>);
var generic = box.MakeGenericType(RT);
var specific = (IBoxedComparer) Activator.CreateInstance(generic);
// Now with specific you can get the equality comparer for the runtime type (RT)
var comparer = specific.GetEqualityComparer();
public interface IBoxedComparer
{
// You need the interface to allow a "typeless" cast
EqualityComparer GetEqualityComparer()
}
public BoxedComparer<T> : IBoxedComparer
{
public EqualityComparer GetEqualityComparer() { return EqualityComparer<T>.Default; }
}