c# - CA1033 with Properties -
when run code analysis (vs2013) 'microsoft managed recommend rules' rule set, warnings class library of type ca1033: 'interface methods should callable kid types'. don't understand rule in situation:
/// object has chemical formula public interface ichemicalformula { /// chemical formula of object chemicalformula chemicalformula {get;} } public class chemicalformula: ichemicalformula { chemicalformula ichemicalformula.chemicalformula { { homecoming this; } } } the docs recommends making protected method same name deriving types can access it, cannot name method same enclosing type. recommend making class sealed, don't want sealed in case. time ignore rule, or there appropriate way handle it?
edit
to add together clarification why class/interface designed way, have class, peptide contains ichemicalformula[] array store modifications. not every modification derives straight chemicalformula, need implement ichemicalformula interface. therefore, if modify instance of peptide withsome molecule (h2o example), chemicalformula class needs implement ichemicalformula.
this description of rule:
consider base of operations type explicitly implements public interface method. type derives base of operations type can access inherited interface method through reference current instance (this in c#) that cast interface. if derived type re-implements (explicitly) inherited interface method, base of operations implementation can no longer accessed. phone call through current instance reference invoke derived implementation; causes recursion , eventual stack overflow.
i think should consider evaluating usage of property. illustration tdd used figure out interface. there possible usages (and invalid ones) below. not yet sure intend accomplish looking @ those.
in example, let's class, newchemicalforumla derived chemicalforumula, , references chemicalformula, mean?
public class newchemicalformula: chemicalformula { public void method() { console.writeline("{0}", chemicalformula.gettype()); // compile error console.writeline("{0}", this.chemicalformula.gettype()); // same above, compile error console.writeline("{0}", ((ichemicalformula)this).chemicalformula.gettype()); // works, intend? } } now outside class, there 2 possibilities:
when have handle derived class:
new newchemicalformula().chemicalformula.gettype() // error or
// works, intend achieve? ((ichemicalformula)new newchemicalformula()).chemicalformula.gettype() when have handle ichemicalformula already. in case, chemicalformula seems redundant:
ichemicalformula formula = new newchemicalformula(); console.writeline("{0}", formula.gettype()); // works, returns newchemicalformula console.writeline("{0}", formula.chemicalformula.gettype()); // works, returns newchemicalformula console.writeline("{0}", formula.chemicalformula.method()); // compile error formula.chemicalformula.method() leads error because must cast newchemicalformula before can utilize method(). because property returns this doesn't help solve problem.
so fxcop warning worth considering, , evaluating design.
c# code-analysis stylecop
No comments:
Post a Comment