Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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
Visual studio 2010 如何查找C#类文件的所有依赖项(类文件)_Visual Studio 2010_C# 4.0_Mono_Nunit_Code Analysis - Fatal编程技术网

Visual studio 2010 如何查找C#类文件的所有依赖项(类文件)

Visual studio 2010 如何查找C#类文件的所有依赖项(类文件),visual-studio-2010,c#-4.0,mono,nunit,code-analysis,Visual Studio 2010,C# 4.0,Mono,Nunit,Code Analysis,我在C#中有许多测试类(在Mono上编译) 与通常将所有单元测试编译成一个大程序集不同,我希望将各个类文件编译成单独的程序集。为此,我想进行依赖性分析,以便自动生成单独的程序集 我所寻找的类似于类依赖性分析器,它存在于假设您想在mono上运行nunit测试,那么应该可以正常工作(我很高兴在mono 2.10.6上使用nunit 2.5和2.4) 一个常见的错误是只复制或跟踪包含测试的.dll文件。与任何程序一样,测试也有依赖项,在您的情况下,它们至少是nunit.framework.dll加上您

我在C#中有许多测试类(在Mono上编译)

与通常将所有单元测试编译成一个大程序集不同,我希望将各个类文件编译成单独的程序集。为此,我想进行依赖性分析,以便自动生成单独的程序集


我所寻找的类似于类依赖性分析器,它存在于

假设您想在mono上运行nunit测试,那么应该可以正常工作(我很高兴在mono 2.10.6上使用nunit 2.5和2.4)

一个常见的错误是只复制或跟踪包含测试的.dll文件。与任何程序一样,测试也有依赖项,在您的情况下,它们至少是
nunit.framework.dll
加上您希望测试的类/程序集(及其任何依赖项)

但是,如果您只想找到给定dll引用的程序集(它需要运行的东西),则可以非常轻松地执行此操作:

using System.Reflection;

void PrintRefs( string dllfile ){
  var asm = Assembly.ReflectionOnlyLoad ( dllfile );

  foreach ( var aname in asm.GetReferencedAssemblies() ){
    Console.WriteLine( aname.Name );
  }
}

注意,这只会找到程序或库编译时使用的程序集名称,而不会找到运行时可能动态加载的程序集名称。

查看Mono Cecil

该库能够在实际的部件映像上“反射”(不是一个很好的名称)以进行分析。这假设您愿意编译成一个“大”程序集,以便使用Mono.Cecil运行依赖性分析

编辑事实上,您可以简单地使用Cecil复制“大”程序集,同时过滤掉其中的部分。这样,您就不会有编译单独程序集的复杂性;查看了解如何在Cecil中往返(读取->操作->保存)程序集的示例

我之前发布了大量关于如何使用Mono Cecil进行“高级”搜索(本质上是静态调用树搜索)的示例:

对您最有用的绝对最小值可能是:

var types = assemblies
    .SelectMany(assembly => assembly.MainModule.Types.Cast<TypeDefinition>());

var dependencies = types.ToDictionary(
    key => key,
    typedef => new HashSet<string>(typedef.Methods.Cast<MethodDefinition>()
                .Where(method => null != method.Body) // ignore abstracts and generics
                .SelectMany(method => method.Body.Instructions.Cast<Instruction>())
                .Select(instr => instr.Operand)
                .OfType<TypeReference>().Distinct()
          //    .Where(type => !type.Namespace.StartsWith("System"))
                .Select(type => type.Name)));

foreach (var entry in dependencies)
{
     Console.WriteLine("{0}\t{1}", entry.Key.Name, string.Join(", ", entry.Value.ToArray()));
}
第一类样本输出(所需类型):

类似的查询,但使用程序集名称查找:

SegmentSoortKenmerk     Lspo.Business
OperatorValue
Koppelpad       Lspo.Business
RedenWaarschuwing
RelExceptions
GecontroleerdDocument   Lspo.Business
OwiExtraKenmerk Lspo.Business
Entiteit        Lspo.Business

请举例说明这个问题。目前还不清楚您是否希望在windows上编译Mono单元测试;您想“为每个依赖项生成一个DLL”(依赖项通常已经是DLL)…您是对的,很难理解这个senario。我已经写过,我想建立单独的类文件,这意味着我不希望整个NUnit测试项目都有一个dll,但我希望NUnit测试项目的每个测试类文件都有一个dll。老实说,我想这只是措辞上的问题。不管怎样,我已经将我的答案添加到链接,作为额外的灵感
SegmentSoortKenmerk     SegmentSoortKenmerk
OperatorValue
Koppelpad       Koppelpad, CodeLeidendVolgend
RedenWaarschuwing
RelExceptions
GecontroleerdDocument   GecontroleerdDocument, GecontroleerdDocument[]
OwiExtraKenmerk OwiExtraKenmerk, Gegeven, BackofficeRelatie
Entiteit        Entiteit, SleutelSysteemObject[], EniteitType
SegmentSoortKenmerk     Lspo.Business
OperatorValue
Koppelpad       Lspo.Business
RedenWaarschuwing
RelExceptions
GecontroleerdDocument   Lspo.Business
OwiExtraKenmerk Lspo.Business
Entiteit        Lspo.Business