这并不能真正回答您的问题(对不起!),但我认为这是一个试图帮助您解决这里的一些问题的项目。我不久前写了这些东西,但从那时起就没有做过任何事情。
该项目称为 Essence ( http://essence.codeplex.com/ ),它使用 System.Linq.Expression 库生成(基于属性)Equals/GetHashCode/CompareTo/ToString 的标准表示,以及能够基于参数列表创建 IEqualityComparer 和 IComparer 类。(我也有一些进一步的想法,但想在继续深入之前获得一些社区反馈。)
(这意味着它几乎和手写一样快 - 不是的主要是 CompareTo(); 因为 Linq.Expressions 在 3.5 版本中没有变量的概念 - 所以你有当你没有得到匹配时,在底层对象上调用 CompareTo() 两次。使用 Linq.Expressions 的 DLR 扩展解决了这个问题。我想我可以使用 emit il,但当时我并没有那么灵感.)
这是一个非常简单的想法,但我以前从未见过它。
现在的问题是,我对完善它失去了兴趣(这将包括为 codeproject 写一篇文章,记录一些代码等),但如果你觉得这会是什么,我可能会被说服这样做出于兴趣。
(codeplex 站点没有可下载的包;只需转到源代码并获取它 - 哦,它是用 f# 编写的(尽管所有测试代码都在 c# 中),因为这是我有兴趣学习的东西。)
无论如何,这是我的测试用例中的一些示例 C# 示例。
// --------------------------------------------------------------------
// USING MY ESSENCE LIBRARY:
// --------------------------------------------------------------------
[EssenceClass(UseIn = EssenceFunctions.All)]
public class TestEssence : IEquatable<TestEssence>, IComparable<TestEssence>
{
[Essence(Order=0, Format="i={0},")] public int MyInt { get; set; }
[Essence(Order=1, Format="s={0},")] public string MyString { get; set; }
[Essence(Order=2, Format="d={0:yyyy-MM-dd}")] public DateTime MyDateTime { get; set; }
public override int GetHashCode() { return Essence<TestEssence>.GetHashCodeStatic(this); }
public override string ToString() { return Essence<TestEssence>.ToStringStatic(this); }
public int CompareTo(TestEssence other) { return Essence<TestEssence>.CompareToStatic(this, other); }
public static bool operator ==(TestEssence lhs, TestEssence rhs) { return Essence<TestEssence>.EqualsStatic(lhs, rhs); }
public override bool Equals(object obj) { return this == (TestEssence)obj; }
public bool Equals(TestEssence other) { return this == other; }
public static bool operator !=(TestEssence lhs, TestEssence rhs) { return !(lhs == rhs); }
}
// --------------------------------------------------------------------
// EQUIVALENT HAND WRITTEN CODE:
// --------------------------------------------------------------------
public class TestManual
{
public int MyInt;
public string MyString;
public DateTime MyDateTime;
public override int GetHashCode()
{
var x = MyInt.GetHashCode();
x *= Essence<TestEssence>.HashCodeMultiplier;
x ^= (MyString == null) ? 0 : MyString.GetHashCode();
x *= Essence<TestEssence>.HashCodeMultiplier;
x ^= MyDateTime.GetHashCode();
return x;
}
public static bool operator ==(TestManual lhs, TestManual rhs)
{
if (ReferenceEquals(lhs, null))
{
if (ReferenceEquals(rhs, null))
return true;
return false;
}
if (ReferenceEquals(rhs, null))
return false;
if (ReferenceEquals(lhs, rhs))
return true;
if (typeof(TestManual) != rhs.GetType())
return false;
return lhs.MyInt == rhs.MyInt
&& lhs.MyString == rhs.MyString
&& lhs.MyDateTime == rhs.MyDateTime;
}
public override bool Equals(object obj) { return this == obj as TestManual; }
public bool Equals(TestManual other) { return this == other; }
public static bool operator !=(TestManual lhs, TestManual rhs) { return !(lhs == rhs); }
public override string ToString()
{
if (MyString == null)
return string.Format("i={0},d={1:yyyy-MM-dd}", MyInt, MyDateTime);
return string.Format("i={0},s={1},d={2:yyyy-MM-dd}", MyInt, MyString, MyDateTime);
}
public int CompareTo(TestManual other)
{
if (other == null)
return 1;
if (ReferenceEquals(this, other))
return 0;
int result = 0;
result = MyInt.CompareTo(other.MyInt);
if (result != 0) return result;
result = MyString.CompareTo(other.MyString);
if (result != 0) return result;
result = MyDateTime.CompareTo(other.MyDateTime);
if (result != 0) return result;
return result;
}
}
// --------------------------------------------------------------------
// --------------------------------------------------------------------
// ALTERNATIVE USAGE
// --------------------------------------------------------------------
// --------------------------------------------------------------------
class Simple
{
public Simple(int value) { Value1 = value; }
public Simple(int value1, int value2) { Value1 = value1; Value2 = value2; }
public readonly int Value1;
public readonly int Value2;
}
[Test]
public void TestReverseForwardString()
{
var _11 = new Simple(1, 1);
var _12 = new Simple(1, 2);
var _21 = new Simple(2, 1);
var _22 = new Simple(2, 2);
var items = new[] { _11, _12, _21, _22 };
var reverseComparer = Essence<Simple>.CreateComparer("-Value1", "Value2");
Array.Sort(items, reverseComparer);
Assert.AreSame(_21, items[0]);
Assert.AreSame(_22, items[1]);
Assert.AreSame(_11, items[2]);
Assert.AreSame(_12, items[3]);
}
[Test]
public void TestReverseForwardLambda()
{
var _11 = new Simple(1, 1);
var _12 = new Simple(1, 2);
var _21 = new Simple(2, 1);
var _22 = new Simple(2, 2);
var items = new[] { _11, _12, _21, _22 };
var reverseComparer = Essence<Simple>.CreateComparer(x => x.Action.ReverseNext, x => x.Member.Value1, x => x.Member.Value2);
Array.Sort(items, reverseComparer);
Assert.AreSame(_21, items[0]);
Assert.AreSame(_22, items[1]);
Assert.AreSame(_11, items[2]);
Assert.AreSame(_12, items[3]);
}
玩得开心!
保罗。