简体   繁体   English

比较“对象”类型的值的最有效方法是什么

[英]What is the most efficient way to compare values of type “object”

I have a method that needs to compare 2 parameters of type object . 我有一种方法需要比较类型为object 2个参数。 These parameters may be of any type ( eg string , int , uint , bool , … int[] , byte[] , etc. ) but the values to compare are always of same type (eg. We either compare an int to an int or an int[] with an int[] value, etc) . 这些参数可以是任何类型( 例如 stringintuintbool ,… int[]byte[] ), 但是要比较的值始终是同一类型(例如,我们将int与int进行比较或具有int []值的int []等)
What is the best and most efficient way to do this in C#? 用C#做到这一点的最好,最有效的方法是什么?
Here is what I have tried (but it is not working for arrays): 这是我尝试过的方法(但不适用于数组):

public bool CompareParameterValue(string paramName, object value) 
{
  // Note that value and p.value as assumed to be of same type within this function
  bool wasModified = false;
  var p = Parameters.GetParameter(paramName);

  if ((p.value != null) && !p.value.Equals(value)) 
  {
    p.value = value;
    p.state = "Modified";
    wasModified = true;
  }
  return wasModified;
}

Please note that efficiency is the key. 请注意,效率是关键。

You could handle the subset of types that are either of a type that properly implements object.Equals() , or that implements IEnumerable for a sequence of a type that properly implements object.Equals (such as List<string> or int[] or IEnumerable<double> ): 你可以处理的,它们是类型的任一类型的子集正常器具object.Equals()或实现IEnumerable一个类型的序列适当工具object.Equals (如List<string>int[]IEnumerable<double> ):

public static bool ObjectsEqual(object lhs, object rhs)
{
    if (ReferenceEquals(lhs, rhs))
        return true;

    if (lhs == null)
        return false;

    if (rhs == null)
        return false;

    if (lhs.GetType() != rhs.GetType())
        return false;

    if (lhs is IEnumerable seq1 && rhs is IEnumerable seq2)
        return seq1.Cast<object>().SequenceEqual(seq2.Cast<object>());

    return lhs.Equals(rhs);
}

Then you can call that in your CompareParameterValue() method instead of calling p.value.Equals(value) , ie: 然后,您可以在CompareParameterValue()方法中调用p.value.Equals(value)而不是调用p.value.Equals(value) ,即:

if (ObjectsEqual(p.value, value))
    ...

Make a interface and then use your obj as IYourInterface . 创建一个接口,然后将obj as IYourInterface If obj doesnt implement IYourInterface it will return null so you can check for that, an implementation could look like so: 如果obj没有实现IYourInterface ,它将返回null,以便您可以进行检查,实现可能如下所示:

public interface IYourInterface
{
   public int value;
   public string modified;
}

public static void Equals(object obj1, object obj2)
{
   var eq1 = obj1 as IYourInterface;
   var eq2 = obj2 as IYourInterface;
   //check if both objects implement the interface
   if(eq1 == null || eq2 == null)
   {
      return false;
   }
   else
   {
      return /*your checks*/
   }
}

Remember to implement the IEquatable interface explicitly since thats what you're doing here. 请记住要明确实现IEquatable接口,因为那就是您在这里所做的。 If you cant use an interface you probably have a underling issue in your code. 如果您不能使用接口,则代码中可能存在基础问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM