正确实现不同类型但语义上相当的两个对象的比较 [英] Properly implement comparison of two objects with different type but semantically equivalent

查看:126
本文介绍了正确实现不同类型但语义上相当的两个对象的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了类似的问题

如何比较具有相似属性的两个明显不同的对象

可能隐含地和/或部分地回复我的问题。

that may implicitly and/or in part reply to my question.

假设我想比较(没有很多嵌套条件)这个对象:

Suppose I want compare (without a lot of nested conditions) this object:

class ObjectA {
  public string PropertyX { get; set; }
  public char PropertyY { get; set; }
  public long PropertyZ { get; set; }
}

System.String 。我只对相等性不等式感兴趣(不是关于身份的一系列值)。

to a System.String. I'm interested only in equality or inequality (not a range of values about identity).

实现 ObjectA 中的IEquatable< string> 是一个适当的选择?我不在乎什么只是工作,我想确定这种情况的正确模式。

Implementing IEquatable<string> in ObjectA is a proper choice? I don't care of what simply works, I want to identify the proper pattern for such case.

作为其他信息,请考虑 ObjectA 通常将以 IEnumerable< ObjectA> 的顺序提供。

As other information, please consider that ObjectA will often be supplied as sequence of IEnumerable<ObjectA>.

我不需要知道string == != objectA instance;

I don't need to know if "string" == or != objectA instance; sorting is not involved.

编辑以澄清(和帮助)

对不起,写一个好问题有时候很难...

假设我不能代表 ObjectA 作为比较目的的字符串(违反封装不是一个选项)。

Suppose I can't represent ObjectA as string for the purpose of comparison (violating encapsulation is not an option).


  • 在上下文1中, ve匹配它与 PropertyY

在上下文2中,我必须匹配它算法应用于 PropertyY / PropertyZ

In context-2 I've to match it against an algorithm applied to PropertyY/PropertyZ.

@Oliver解决方案在问题的结尾帮助我(再次+1)。
我可以简单地定义一个自定义界面:

@Oliver solution in the end of the question helps me again (and +1 again). I can simply define a custom interface:

interface IContextConverter {
  string ToEquatableStringForContext1();
  string ToEquatableStringForContext2();  
}

既然我还有一个 ObjectB 具有相同的逻辑但不同的属性,两者都将实现 IContextConverter 或者我可以找到一个更好的名称)避免违反 RAP

Since I've also an ObjectB with same logic but different properties, both will implement IContextConverter (or maybe I find a better name) avoiding to violate RAP.

推荐答案

我强烈建议实现 IEquatable< string> ,特别是在使用集合,字典,LINQ等等,你真的不知道什么时候这些方法之一将被称为内部深处,这可能导致微妙的错误。

I would strongly recommend to not implement IEquatable<string>, cause especially when working with collections, dictionaries, LINQ, etc. you don't really know when one of these methods will be called somewhere deep inside which leads maybe to subtle bugs.

由于你喜欢比较不同类型的两个对象,一个简单的 Comparer< T> 也不会工作。

Due to the fact that you like to compare two objects of different types a simple Comparer<T> wouldn't work also.

所以写一个 TypeConverter 其中将您的对象转换为t他需要类型(在你的情况下,一个 string )或者添加一个方法给你的对象,如 .ToEquatableString()并使用他们的输出将您的对象与其他字符串进行比较。

So either write a TypeConverter which converts your object into the desired type (in your case a string) or add a method to your object like .ToEquatableString() and use their output to compare your object with the other string.

以下是一个示例,您可以获取所有与其他集合中的字符串匹配的元素:

Here is an example on you could get all elements, that match one of a string in another collection:

IEnumerable<String> otherElements = new[] {"abc", "def", "ghi" };
IEnumerable<ObjectA> myObjects = GetObjects();

var matchesFound = otherElements.Join( // Take the first collection.
              myObjects, // Take the second collection.
              s => s, // Use the elements in the first collection as key (the string).
              obj => obj.ToEquatableString(),  // Create a string from each object for comparison.
              (s, obj) => obj, // From the matching pairs take simply the objects found.
              StringComparer.OrdinalIgnoreCase); // Use a special string comparer if desired.

这篇关于正确实现不同类型但语义上相当的两个对象的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆