English 中文(简体)
如何在IComparer.Compare方法上将对象强制转换为Class类型
原标题:How to cast object to Class type on IComparer.Compare method

我正在实施

<code>int IComparer.Compare(对象x,对象y)

来自IComparer接口。我知道这些对象的类型是Class1,它的一个成员是class1Instance.myDate,类型是DateTime。

我想做的事情大致如下:

DateTime.Compare( (Class1)x.myDate, (Class1)y.myDate);

但是用这种方式铸造是行不通的。除了以下内容,还有其他选择吗?

Class1 x2 = x as Class1;
最佳回答

它可能只是强制转换与方法调用的优先顺序。你试过这样的东西吗?

DateTime.Compare( ((Class1)x).myDate, ((Class1)y).myDate);
问题回答

请尝试以下操作:

DateTime.Compare( ((Class1)x).myDate, ((Class1)y).myDate); 

最好的解决方案是扩展Comparer<;类别1>

Comparer<;T>;是一个抽象类,它同时实现了IComparer的泛型和非泛型版本。扩展它的优点是不必实现非泛型的IComparer。Compare方法;基类已经合理地实现了它。

public class Class1Comparer: Comparer<Class1>
{
   public override int Compare(Class1 x, Class1 y)
   {
      //null-checks here          
      return DateTime.Compare(x.myDate, y.myDate);
   }
}

这可能会使头发分叉,但您可以这样做:

DateTime.Compare((x作为Class1).myDate,(y作为Class1(.myDate))

((Class1)x).myDate

无异常方式:

Class1 c1 = null;
if (x is Class1) c1 = (Class1)x;

如果您希望您的比较器是只读类型的,请使用通用版本。

http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx

排序次序

因此,您将实现为:

public class MyComparer : 排序次序<Class1>
{
    public int Compare( Class1 x, Class1 y )
    {
        // do comparison stuff here.
    }
}

您可能需要执行以下操作:

DateTime.Compare( ((Class1)x).myDate, (Class1)y).myDate);

它的方法是尝试将日期字段强制转换为Class1

如果出于某种原因不必比较异构类型,我总是建议将泛型和更通用的方法(嗯,嗯,“generic”应该是正确的英语单词,愚蠢的.NET术语)结合在一个类中,从非泛型调用泛型。大多数泛型排序都支持泛型方法,但如果出现非泛型类型(例如ArrayList),则非泛型方法将用于非泛型类型。这也是两行的问题,在逻辑上是合理的,所以在这种情况下我不支持YAGNI。

检查空参数也是一个好主意,即使您不期望它们。我有几次因为没有做到这一点而被抓住,它们甚至可能被一些算法“人为”引入。

public class Class1Comparer : IComparer<Class1>, IComparer
{
  public int Compare(Class1 x, Class1 y)
  {
    if(x == null)
      return y == null ? 0 : -1;
    if(y == null)
      return 1;
    return DateTime.Compare(x.myDate, y.myDate);
  }
  public int Compare(object x, object y)
  {
    //This has no type-checking because you said above it isn t needed. I would normally add some just in case.
    return Compare((Class1)x, (Class1)y);
  }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...