对于未知类型确定的平等 [英] Determining equality for unknown types

查看:207
本文介绍了对于未知类型确定的平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查给定的实例与集合(两者都是未知类型)相匹配。

I need to check the given instance is matching with the collection (Both are unknown types).

看一看

void Main()
{
    // Employee "John" Object got from Service Layer #1
    Object obj1 = Client1.GetObject("John");

    // Employee "John" Object got from Service Layer #2
    Object obj2 = Client2.GetObject("John");

    bool flag = CheckEquality(obj1, obj2); // Both objects are Unknown Type
}



平等检查方法:

The Equality Check Method:

public bool CheckEquality(object obj1, object obj2)
{
    // Solution for Determining equality for unknown types
}

例如 - 考虑下面的代码:(仅适用于理解的目的)

For example - consider the Following code: (Only for understanding purpose)

情景1

public class EmpPerson
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
}



OBJ1 持有实例新EmpPerson(){ID = 1,名称=约翰,地址=洛杉矶};

OBJ2 持有实例新EmpPerson(){ID = 1,名称=约翰,地址=洛杉矶};

情景2

OBJ1 存放值约翰·史密斯

OBJ2 存放值约翰·史密斯

情景3

< STRONG> OBJ1 存放值约翰·史密斯

OBJ2 持有实例新EmpPerson(){ID = 1,名称=约翰 ,地址=洛杉矶};

方案#4

OBJ1 存放值新UnknownTypeA()

OBJ2 存放值新UnknownTypeX()

我需要检查两个对象是相同的。

I need to Check both the objects are identical.

请asssit我。如何检查未知类型的平等。

Kindly asssit me. how to check the equality for unknown types.

推荐答案

好吧,我觉得有你缺少一些在这里分...

OK I think there are some points you are missing here...

首先,什么叫平等是什么意思?

两个对象相等当他们持有相同的参考?
为他们平等时,他们有一个共同的属性,该属性是一样的吗?
或什么时候?

Are two objects equal when they hold the same reference ? Are they equal when they have a common property that is the same ? Or when ?

二(也许更重要),为什么你需要检查对象平等的,你不知道吗?

如果你甚至不知道类型的对象,有什么说他们是平等的地步?你甚至不能使用他们...

If you don't even know the types of your objects, what's the point of saying they're equals ? You cannot even use them...

如果你知道你的类型的对象,那么你知道如何将它们进行比较。

And if you know the types of your objects, then you know how to compare them.

您加入情景案例#4,为什么你比较两个不同类型的对象,当你不知道任何类型的?

You added scenario case #4, why do you compare two objects of different types when you don't know any type ?

在OOP中,你不能比较不同类型的对象,因为它没有任何意义,你不能比较苹果和梨...

In OOP, you cannot compare objects of different types, because it doesn't make sense, you can't compare apples and pears...

这就是为什么我们通常制造容纳不同类型的公用部分,或者说,我们做的方法来变换另一种对象类型接口。

So that's why we usually create Interfaces that hold the common parts of different types, or that we make methods to transform an object type in another.

我给这里的解决方案,意味着您对这些对象实施控制。

The solution I give here, implies that you have control over the objects implementation.

您可能希望重载equals()方法(从Object继承)。

You may want to overload the Equals() method (inherited from Object).

public class EmpPerson
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }

    public override bool Equals(object obj)
    {
        EmpPerson cmp = (EmpPerson)obj;
        return ID.Equals(cmp.ID);
    }
}



每当你需要对它们进行比较:

And whenever you need to compare them :

obj1.Equals(obj2);

这不过带来的对象必须是同一类型的限制,任何的类型。

This however brings the restrictions that objects need to be of the same type, whatever the type.

注:错误检查一直没有在这里实现,你需要做...

这篇关于对于未知类型确定的平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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