c# - Are there any code analysis tools to detect the == operator when it's comparing specific types? -



c# - Are there any code analysis tools to detect the == operator when it's comparing specific types? -

in legacy code-base, many technical reasons, replacing parameters have been of base of operations class type interface type. example:

public interface idomainobject { int id { get; } } public abstract class basedomainobject : idomainobject { public int id { get; protected set; } public override bool equals(object obj) { var domainobj = obj basedomainobject; homecoming domainobj != null && id.equals(domainobj.id); } public static bool operator ==(basedomainobject x, basedomainobject y) { homecoming !referenceequals(x, null) && !referenceequals(y, null) && x.equals(y); } public static bool operator !=(basedomainobject x, basedomainobject y) { homecoming !(x == y); } } public class mydomainobject : basedomainobject { public mydomainobject(int id) { id = id; } ... }

so everywhere in code, have variable of type basedomainobject have 1 of type idomainobject. however, running problems '==' operator--it doesn't work interfaces. interface types, '==' operator falls referenceequals().

the next code demonstrates problem:

// old style basedomainobject baseobj1a = new mydomainobject(1); basedomainobject baseobj1b = new mydomainobject(1); basedomainobject baseobj2 = new mydomainobject(2); assert.istrue(baseobj1a != baseobj2); assert.istrue(baseobj1a == baseobj1b); // succeeds // new style idomainobject iobj1a = new mydomainobject(1); idomainobject iobj1b = new mydomainobject(1); idomainobject iobj2 = new mydomainobject(2); assert.istrue(iobj1a != iobj2); assert.istrue(iobj1a == iobj1b); // fails

going using base of operations class not option--our interface generic co-variant (similar idomainobject<out t>)which necessary polymorphic behavior need. ideally replace of '==' .equals(). however, our codebase huge, , finding of '==' operators care mammoth task.

one thought write fxcop rule flag occurrences of variable of interface type (ie, idomainobject) used in '==' comparison. didn't work--fxcop doesn't back upwards this. thought write our own code analysis tool checks case, time consuming.

so question is, there kind of code analysis tool out there utilize find these '==' occurrences?

you can utilize custom search patterns resharper things this.

you consider doing "old fashioned way". alter references, prepare compilation errors.

c# .net operators code-analysis

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -