了解如何在单个visual studio解决方案中设置依赖关系并构建许多项目的顺序 [英] to know how to set dependencies and build order of many projects in a single visual studio solution

查看:72
本文介绍了了解如何在单个visual studio解决方案中设置依赖关系并构建许多项目的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我手边有一个问题。我有很多项目可以说100个cs项目,我想编写一个C#程序来接受所有这些项目项目并将它们添加到一个可视化工作室解决方案文件中,并设置所有这些项目之间的依赖关系,从而设置构建顺序。我想编写一个程序,因为我有很多项目,所以我不想添加它们手动并根据引用设置它们之间的依赖关系。没有循环依赖关系,但在100个项目中,许多项目在不同的不同项目中被引用。所以我想以某种方式知道最独立的项目,并以某种方式继续进行相应的设置依赖关系通过

program.Please为我提供解决方案。请在C#中提供示例代码。



谢谢,

Mayank

Hi,

I have an issue at hand.I have many projects lets say 100 cs projects and I want to write a C# program that would take all those projects and add them in a single visual studio solution file and also set the dependencies between all those projects and hence set the build order.I want to write a program to do that as I have many projects so I don't want to add them manually and set the dependencies among them based on references.There are no cyclic dependencies but among the 100 projects many are referenced in different different projects.So I want to somehow know the most independent project and somehow proceeding like that set the dependencies accordingly through
program.Please provide me solution for this.Please give sample code also in C#.

Thanks,
Mayank

推荐答案

首先,目前尚不清楚你是如何拥有这么多具有​​依赖关系的项目的,但没有解决方案文件让我们说你从以前的开发者那里得到了不好的遗产,无论如何。



首先,你可以找到解决方案文件的结构:http://msdn.microsoft.com/en-us/library/bb165951%28v=vs.90% 29.aspx [ ^ ]。



将项目放在一个解决方案文件中应该足够了。一些简单的实验可以帮助找出细节。格式很简单。唯一的问题是:据我所知,它不是标准化的。



现在,项目及其依赖项。根据您的描述,如果您已编译项目,则不是100%清楚。所以,首先,我将快速解释如何使用编译的程序集来找出它们的依赖关系。您应该使用 System.Reflection.Assembly.LoadFrom 加载程序中的每个程序集,并找出使用 System.Reflection.Assembly.GetReferencedAssemblies引用的程序集。 。它将返回 System.Reflection.AssemblyName 对象的实例数组,并且对于每个程序集名称,将其与您获得的每个其他程序集的程序集名称进行比较。输出。如果加载了一个程序集文件,你可以从 System.Reflection.Assembly.GetName()中找到程序集名称,如果没有 - 使用静态函数 System.Reflection.AssemblyName.GetAssemblyName 按文件名查找程序集名称。请参阅:

http://msdn.microsoft.com /en-us/library/system.reflection.assembly.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.aspx [ ^ ]。



这样,您可以获得可用程序集文件之间的依赖关系。请注意,在这种方法中,我们从未使用文件系统上文件的位置,我们仅比较程序集名称,这些名称不是字符串,而是相对详细的结构,特别是如果它们是强名称。另请注意,对于没有强命名的程序集,您可以在无法找到正确的依赖项时遇到这种情况。有些项目是无关的。如果有人给了两个不同的程序集相同的名称(如果程序集没有强名称,或者如果它们的名称极不可能,那么或多或少可能),它不能保证你正确的依赖。



但是,根据您的描述,您的项目更有可能不会被编译。这是更困难的情况,但可以解决,因为程序集引用是在项目文件中规定的。如果所有文件都符合MSBuild项目文件标准,则可以读取这些项目文件并查找引用。它会给你依赖。对于这种方法,您拥有来自Microsoft的全面API,并且项目文件格式已经过很好的标准化和正式记录。



请从这里开始:

http://msdn.microsoft.com/en-us/library /gg145008%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/dd393574%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us /library/0k6kkbsd%28v=vs.110%29.aspx [ ^ ]。



-SA
First of all, it's not clear how you ended up having so many projects with dependencies, but without a solution file. Let's say you got bad legacy from previous developers, no matter.

First, you can find out how the solution file is structured: http://msdn.microsoft.com/en-us/library/bb165951%28v=vs.90%29.aspx[^].

It should be enough to put projects together in a solution file. Some simple experiments can help to find out the detail. The format is simple enough. The only problem is: to best of my knowledge, it is not standardized.

Now, projects and their dependencies. From your description it is not 100% clear if you have compiled projects or not. So, first, I'll quickly explain how to use compiled assemblies to find out their dependency. You should load each assembly in your program using System.Reflection.Assembly.LoadFrom and find out what assemblies are referenced using System.Reflection.Assembly.GetReferencedAssemblies. It will return an array of the instances of System.Reflection.AssemblyName objects, and, for each assembly name, compare it with assembly name of each of the other assemblies you got on output. If an assembly file is loaded, you can find assembly name from System.Reflection.Assembly.GetName(), if not — by using the static function System.Reflection.AssemblyName.GetAssemblyName to find an assembly name by a file name. Please see:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.aspx[^].

This way, you can get the dependencies between available assembly files. Note that in this approach we never used the location of the files on the file systems, we compare only the assembly names, which are not strings but the relatively detailed structures, especially if they are strongly named. Also note that for not strongly named assemblies, you can face the situation when you cannot find out correct dependency. Some projects are unrelated. If someone gave two different assemblies identical names (which is more or less likely if assemblies are not strongly named and extremely unlikely if they are), it won't guarantee you correct dependency.

But, from your description, it's more likely that your projects are not compiled. This is more difficult situation, but solvable, because assembly references are prescribed in the project files. If all your files comply with MSBuild project file standard, you can read those project files and find out references. It will give you dependencies. For this approach, you have comprehensive API from Microsoft, and the project file format is well standardized and formally documented.

Please start here:
http://msdn.microsoft.com/en-us/library/gg145008%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dd393574%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/0k6kkbsd%28v=vs.110%29.aspx[^].

—SA


右键单击解决方案并选择项目依赖项。在这里,您可以设置依赖性和构建顺序
Right click at solution and select project dependanciees. Here you can set dependancies and built order


这篇关于了解如何在单个visual studio解决方案中设置依赖关系并构建许多项目的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆