如何比较具有类似性质的两种明显不同的对象 [英] How to compare two distinctly different objects with similar properties

查看:121
本文介绍了如何比较具有类似性质的两种明显不同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是所有在C#中使用.NET 2.0。

This is all in C#, using .NET 2.0.

我有对象的两个列表。他们不是相关的对象,但他们确实有一些事情
在常见的,可以作为一个基于GUID唯一标识进行比较这样。这两个列表需要
由只包含的GUID另一个列表可能会或可能不会匹配过滤
编号的包含在第一两个列表。

I have two lists of objects. They are not related objects, but they do have certain things in common that can be compared such as a Guid based unique identifer. These two lists need be filtered by another list which just contains Guid's which may or may not match up with the ID's contained in the first two lists.

我曾经想过铸造每个对象名单只是对象,并通过排序的想法
这一点,但我不知道,我就可以,一旦它投来访问ID属性,我
认为该方法的两个列表进行排序应该是在明知有些哑了什么
进行排序名单。

I have thought about the idea of casting each object list to just 'object' and sorting by that, but I'm not sure that I'll be able to access the ID property once it's cast, and I'm thinking that the method to sort the two lists should be somewhat dumb in knowing what the list to be sorted is.

什么会在每个对象列表带来最好的方式,以便它可以针对仅与列表进行排序的ID

What would be the best way to bring in each object list so that it can be sorted against the list with only the ID's?

推荐答案

您应该让您的每一个不同的对象实现共同的接口。然后创建一个IComparer的< T>该接口并在排序使用它

You should make each of your different objects implement a common interface. Then create an IComparer<T> for that interface and use it in your sort.

这篇关于如何比较具有类似性质的两种明显不同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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