Operator Overloading with Interface-Based Programming in C#

后端 未结 2 1795
一个人的身影
一个人的身影 2020-12-04 14:55

Background

I am using interface-based programming on a current project and have run into a problem when overloading operators (specifically the Equality and Inequa

2条回答
  •  忘掉有多难
    2020-12-04 15:57

    We ran into the same problem, and found an excellent solution: Resharper custom patterns.

    We configured ALL of our users to use a common global pattern catalog in addition to their own, and placed it into SVN so that it can be versioned and updated for everyone.

    The catalog included all patterns known to be wrong in our system:

    $i1$ == $i2$ (where i1 and i2 are expressions of our interface type, or derived.

    the replace pattern is

    $i1$.Equals($i2$)

    and the severity is "Show as error".

    Similarly we have $i1$ != $i2$

    Hope this helps. P.S. Global catalogs is the feature in Resharper 6.1 (EAP), will be marked as final very soon.

    Update: I filed a Resharper Issue to mark all interface '==' a warning unless it is comparing to null. Please vote if you think it is a worthy feature.

    Update2: Resharper also has [CannotApplyEqualityOperator] attribute that can help.

提交回复
热议问题