Detecting direct instantiation with nDepend

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 12:34:17

问题


With the nDepend API, would something like the following be possible?

I want to keep a watch out for instances where our object factory has been bypassed and a concrete class is being instantiated directly.

Obviously I'd need to be able to filter out things like:

StringBuilder stringBuilder = new StringBuilder();

perhaps by adding to the Where clause type names to exclude, or namespaces in which to check, but I want to make sure we see:

IMyCustomType item = ObjectFactory.Get<IMyCustomType>();

and not this:

MyCustomType item = new MyCustomType();

Thanks.


回答1:


Maybe such code rule below could help you, hopefully it is understandable enough to not have to comment it:

warnif count > 0

let ctors = Application.Namespaces.WithNameLike("Namespaces1*").ChildMethods().Where(m => m.IsConstructor)

let codeThatMustNotCallCtors = Application.Namespaces.WithNameLike("Namespaces2*").ChildMethods()

from m in codeThatMustNotCallCtors.UsingAny(ctors)
select new { m, ctorsCalled = m.MethodsCalled.Intersect(ctors ) }


来源:https://stackoverflow.com/questions/24655585/detecting-direct-instantiation-with-ndepend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!