How to create fxcop rule to check that a class call init in its constructor on another class?
Let's say I have class A which instantiates class B. class B has a constructor and several init methods (enforced through a class interface). How to write fxcop rule to check that 开发者_运维技巧at least one init method is called after instantiating B.
What if you load it at runtime from internet you cannot use constructor so the init.
This sort of rule is nearly impossible to write except for the most trivial cases. For example, if you want to ensure that a B init method is called immediately after invoking a B constructor, that can be done relatively easily (although branching logic can cause problems even there). However, there wouldn't be much need for a separate init method if one only needed to support such trivial usage. What is generally more interesting to detect are cases where one attempts to do something else with a B instance before invoking its init method, and that's not really a job for a static verifier like FxCop.
Have you considered throwing an exception from B if it is not initialized at entry into another method (much like one might throw an ObjectDisposedException from a disposed instance)?
精彩评论