Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过NDepend查找仅在特殊类中使用的代码_C#_Dependency Injection_Ndepend_Static Code Analysis - Fatal编程技术网

C# 通过NDepend查找仅在特殊类中使用的代码

C# 通过NDepend查找仅在特殊类中使用的代码,c#,dependency-injection,ndepend,static-code-analysis,C#,Dependency Injection,Ndepend,Static Code Analysis,我正在尝试使用NDepend清除代码库中的死代码。由于我们使用的是依赖项注入,因此我希望找到仅在从注册表基派生的类中使用的接口(和实现): public class PresenterRegistry : Registry { public PresenterRegistry() { For<IExamplePresenter>().Use<ExamplePresenter>(); } } 公共类PresenterRegistry

我正在尝试使用NDepend清除代码库中的死代码。由于我们使用的是依赖项注入,因此我希望找到仅在从注册表基派生的类中使用的接口(和实现):

public class PresenterRegistry : Registry
{
    public PresenterRegistry()
    {
        For<IExamplePresenter>().Use<ExamplePresenter>();
    }
}
公共类PresenterRegistry:注册表
{
公共演示者注册()
{
For().Use();
}
}
有没有办法做到这一点


谢谢

我不知道你要什么

我想查找仅在从注册表基派生的类中使用的接口(和实现):以下查询匹配从
Microsoft.Win32.registry
派生的任何类型使用的应用程序接口和类:

let registryDerived = Application.Types.Where(t => t.DeriveFrom("Microsoft.Win32.Registry"))
from t in Application.Types.UsedByAny(registryDerived)
select t
..在下一个查询中,您还可以获得在上一个查询中匹配的接口的派生类型和实现:

let registryDerived = Application.Types.Where(t => t.DeriveFrom("Microsoft.Win32.Registry"))
from t in Application.Types.UsedByAny(registryDerived)
let tDerived = t.DerivedTypes
let tImpl = t.TypesThatImplementMe
select new { t, tDerived, tImpl }
。。。或将其全部列出:

let registryDerived = Application.Types.Where(t => t.DeriveFrom("Microsoft.Win32.Registry"))

let tUsed = Application.Types.UsedByAny(registryDerived)
let tDerived = tUsed.SelectMany(t => t.DerivedTypes)
let tImpl = tUsed.SelectMany(t => t.TypesThatImplementMe)

from t in tUsed.Union(tDerived).Union(tImpl)
select t

顺便说一句,
Microsoft.Win32.Registry
是密封的,所以它不是您要讨论的类。但是您可以用自己的类名替换它,前缀为namespace。

尝试了一点之后,我创建了一个查询,它以我需要的方式工作:

// <Name>Interfaces registered but potentially not used</Name>
warnif count > 0 
from t in JustMyCode.Types
from i in JustMyCode.Types
where t.DeriveFrom("StructureMap.Configuration.DSL.Registry")
   && i.IsInterface
   && t.IsUsing(i)
   && i.NbTypesUsingMe < 3 // one using for implementation, one in registry
select i
//已注册但可能未使用的接口
如果计数大于0,则警告
从JustMyCode.Types中的t开始
从JustMyCode.Types中的i开始
其中t.DeriveFrom(“StructureMap.Configuration.DSL.Registry”)
&&i.接口
&&t.IsUsing(i)
&&i.NbTypesUsingMe<3//一个用于实现,一个在注册表中
选择i
没有我预期的那么多代码:-)此查询没有涵盖任何可能发生的情况,但它是一个良好的开端

不过:帕特里克,谢谢你的帮助


不客气,Rico:)顺便说一句,这个代码规则可以用O(N)而不是O(N^2)(N是JustMyCode.Types的数量)重写。这种优化是通过神奇的方法实现的。此规则还提供了更详细的结果

warnif count > 0 
let registryDerived = JustMyCode.Types.Where(t => t.DeriveFrom("StructureMap.Configuration.DSL.Registry"))
from i in JustMyCode.Types.UsedByAny(registryDerived)
where i.IsInterface &&
      i.NbTypesUsingMe < 3 // one using for implementation, one in registry
select new { i, 
             registryDerivedUser = i.TypesUsingMe.Intersect(registryDerived),
             i.TypesUsingMe }
warnif计数>0
让registryDerived=JustMyCode.Types.Where(t=>t.DeriveFrom(“StructureMap.Configuration.DSL.Registry”))
从JustMyCode.Types.UsedByny中的i开始(Registry派生)
i.i.接口在哪里&&
i、 NbTypesUsingMe<3//一个用于实现,一个在注册表中
选择新的{i,
registryDerivedUser=i.TypesUsingMe.Intersect(registryDerivedUser),
i、 类型使用ME}

谢谢您的回复!这并不是我所需要的:上面的例子给出了从注册表基类型使用的所有类型。但是我需要知道只在注册中心使用的代码。你明白我的意思吗?提前谢谢!仅在注册表中使用的代码您是指.NET Fx类Microsoft.Win32.Registry和Microsoft.Win32.Registry使用的类以及。。。等等或者你是说在注册表中访问的密钥?你好。不,我指的是在StructureMap.Configuration.DSL.Registry类型中使用的代码。它用于配置依赖项注入容器。我的目标是找到只在这些类中使用的类型和接口,而不在我们的代码库中的其他地方使用。这意味着找到的类型只是注册的,而不是使用的,因此可以正确删除的是死代码。因为我们使用依赖注入和TDD,所以很难找到死代码,因为代码至少被测试类和DI容器的注册使用,如上所述。谢谢好吧,你从来没有提到过StructureMap,所以我对注册表的含义感到困惑?!到目前为止,NDepend没有读取StructureMap或其他DI Fx文件。但是您仍然可以编写一个程序,1)读取DI Fx文件2)使用NDepend.API解析在DI Fx文件中找到的类型,然后查看它们的用法。好的,很抱歉丢失了信息。但我仍然认为这是不相关的。我们用于配置从注册表基类继承的StructureMap类(不是xml,请参阅我最初文章中的示例)。将我的问题与具体用例(StructureMap)联系起来可能有点混乱。同样的问题可能是:我需要找到仅在MyOwnBaseClass的任何派生中使用的接口(如上面示例中的IExamplePresenter)。你现在明白我的意思了吗?再次为误会感到抱歉。