比较通用字段
我有一些泛型类型,如下所示:
public struct Tuple<T1, T2> { ... }
public struct Tuple<T1, T2, T3> { ... }
etc.
理论上,这些类型应该能够将自己与相同类型的其他值进行比较,以便我可以编写以下类型的代码:
List<Tuple<Type, String>> l = new List<Tuple<Type, String>>();
l.Add(new Tuple<Type, String>(typeof(ISomeInterface), "123"));
if (l.Contains(new Tuple<Type, String>(typeof(ISomeOtherInterface), "123"))
...
不幸的是,我的代码中有一个错误,并且那么问题就变成了如何正确地做到这一点。
该错误与我的 CompareTo> 实现有关,它基本上如下所示:
Int32 result = HelperMethods.CompareTwoFields<T1>(_Value1, other._Value1);
if (result != 0)
return result;
Int32 result = HelperMethods.CompareTwoFields<T2>(_Value2, other._Value2);
if (result != 0)
return result;
return 0;
HelperMethods.CompareTwoFields 如下所示:
internal static Int32 CompareTwoFields<T>(T field1, T field2)
{
Int32 result = 0;
if (ReferenceEquals(field1, null) != ReferenceEquals(field2, null))
result = ReferenceEquals(field1, null) ? -1 : +1;
else if (!ReferenceEquals(field1, null) && field1 is IComparable<T>)
result = ((IComparable<T>)field1).CompareTo(field2);
else if (!typeof(T).IsValueType)
{
if (Object.ReferenceEquals(field1, field2))
return 0;
else
return field1.ToString().CompareTo(field2.ToString());
}
return result;
}
最后一个 if 语句现在有一些内容我用来修复该错误,但这是否正确?
基本上,如何比较两个 Type 对象? 除了将它们转换为字符串并进行比较之外,是否有有意义的比较?
I have some generic types, like the following:
public struct Tuple<T1, T2> { ... }
public struct Tuple<T1, T2, T3> { ... }
etc.
These should in theory be able to compare themselves against other values of the same type, so that I can write the following type of code:
List<Tuple<Type, String>> l = new List<Tuple<Type, String>>();
l.Add(new Tuple<Type, String>(typeof(ISomeInterface), "123"));
if (l.Contains(new Tuple<Type, String>(typeof(ISomeOtherInterface), "123"))
...
unfortunately, there was a bug in my code, and the question then becomes how to do this correctly.
The bug had to do with my implementation of CompareTo>, which basically looks like this:
Int32 result = HelperMethods.CompareTwoFields<T1>(_Value1, other._Value1);
if (result != 0)
return result;
Int32 result = HelperMethods.CompareTwoFields<T2>(_Value2, other._Value2);
if (result != 0)
return result;
return 0;
HelperMethods.CompareTwoFields looks like this:
internal static Int32 CompareTwoFields<T>(T field1, T field2)
{
Int32 result = 0;
if (ReferenceEquals(field1, null) != ReferenceEquals(field2, null))
result = ReferenceEquals(field1, null) ? -1 : +1;
else if (!ReferenceEquals(field1, null) && field1 is IComparable<T>)
result = ((IComparable<T>)field1).CompareTo(field2);
else if (!typeof(T).IsValueType)
{
if (Object.ReferenceEquals(field1, field2))
return 0;
else
return field1.ToString().CompareTo(field2.ToString());
}
return result;
}
The last if-statement there is something I tucked on now to fix the bug, but is this correct?
Basically, how do I compare two Type objects? Is there a meaningful comparison for these, except just to convert them to a string and compare?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的意思是:
这适用于
IEquatable
(对于T : IEquatable
),否则回退到object.Equals
。还有用于不等式比较(大于/小于)的
Comparer.Default
:这使用
T : IComparable
或T : IComparable
代码> 否则。顺便说一句,
Type
应该只使用object.Equals
提供的常规引用比较,因此它应该可以与上面的内容一起正常工作。Do you mean:
This works with
IEquatable<T>
(forT : IEquatable<T>
), else falls back toobject.Equals
.There is also
Comparer<T>.Default
for inequality comparisons (greater/less):This uses
T : IComparable<T>
, orT : IComparable
otherwise.By the way,
Type
should just use the regular reference compare provided byobject.Equals
, so it should just work fine with the above.