Modifying Project Dependency Programatically - c#

Is there a way to add project dependency programmatically? For some reason the project in my solution file the project dependency is not set. What I am thinking of
Open the file and load it as xml document
Traverse through the references . Get the names of the dependency
Get the guid of those project reference
Prepare the project dependency section element
Modify the file by adding this section
Do the same for other 60+ projects
If I am not wrong we can do this by using MSBUILD classes as well .
Any better suggestions ?
On the same line I need to modify the path of one dll in all these project . I don’t want to do it manually .

Eventually I wrote a utility which reads the csproj and sln file as string[] and using Loops/Linq updated the lines and then wrote it back System.IO
Also JetBrains and MSBuild engine has class to find the dependency of a project , what dll its refrerencing etc etc.
If anyone needs help with this then let me know

Related

In C#, how to `Paket Add` dependencies to all projects in a folder even when they are in separate solutions?

Say I have a packet.dependencies file in a root folder full of different solutions adding up to hundreds of projects, each with a paket.references file.
Is there an easy way to go about adding the same package/same version in all the projects with a single command (or close) with paket?
In my use case, I'm trying to add an analyzer to every C# project we have. It would be tedious to add the same package and version to hundreds of different C# projects...
It was actually just paket add <package ID>, if ran in the root folder with the paket.dependencies file, it will actually find all the .csproj files and add the dependency.
It was the following line in the docs that threw me off:
By default packages are only added to the solution directory, but not on any of its projects. It's possible to add the package to a specific project:
But it will actually add the dependency to all solutions and projects.

Copy references to class library ASP.NET

I created a new class library in my project, and moved my Models folder to that. Now I need to get all the references like EntityFramework, DataAnnotations etc to my new class library.
Is there an easy way to do that(like copying them), without going into Nuget packages and downloading them one by one?
Yes you can easily copy all references from one project to the other. While in VisualStudio double click the project and it should open the .csproj file. (If you are not in VisualStudio Right click edit the .csproj file.) Copy all lines enclosed in a ItemGroup tag which say 'PackageReference' into the 'new' project you copied the models to.
Make sure you also rename all Namespaces of the files you moved so that they match the new library name. Link: how to change namespace of entire project?

Build project's components to own .dlls

I have project, which contains lot of classes. I use this project as plugin base for one of my application. This app can load all these plugins from one .dll builded from this project.
Problem is, I need to use these plugins in third-side app. This app can load only one plugin per .dll. I have very few options here. As far as I know, I could create new project for each file and build them. But it is sloppy way how to solve it.
So, is there any way, how to build one project for each of its classes or for groups of classes?
It sounds to me as task for some script. Is possible to achieve this for example with psake or powershell?
Thanks in regards
Well, I believe that they are already doing that in their own bin directories.
That`s if you mean assemblies as components. If you are talking about classes, then you should place them in separate assemblies and use the same approach.
You can right click on assembly and go to properties, build where you can see the 'output path' field as well as 'XML documentation' checkbox which is required for third-side apps to see your XML comments while using .dll-s.
One way I can think of is to create a 'template' project file based on the Plugin project you already have. You can make a copy of the plugin.csproj and then delete all included plugin classes. Using psake and Powershell you can dynamically add the plugin class you need an assembly for and then call MSBuild to build the csproj.
<ItemGroup>
<Compile Include="MyFirstPlugin.cs" />
</ItemGroup>
Adding xml-nodes like this to an csproj-file is easy to do in Powershell.
The pseudo code for the ps script could be something like this;
define list of plugin cs-files
for each plugin cs file
copy template.csproj to plugin.csproj
add xml node to include plugin.cs file
call msbuild on plugin.csproj
delete plugin.csproj file

Adding a link to an external project, and its dependencies

I am trying to publish two different executables using ClickOnce - where one is executing the other, I have looked around and found a suggestion to add a link to the executable in the project files and setting it to content and copy always - now, this works great except, the dependencies used by that executable aren't copied -
Is there a simpler way to get it to copy the dependencies without creating links to every dll or config file?
What you need to do is to add a reference to the other executable, instead of including it as a link.

open a .csproj with monodevelop

I've downloaded a framework with samples in .csproj project format.
How can I open them in MonoDevelop?
I'm interested in using some classes in that framework.
It has a folder structure like: Accord.Statistics.Models and a main folder Accord with a subfolder Statistics with a subfolder Models with a file ModelFoo.cs
I want to use that file (that begin with
namespace Accord.Statistics.Models
) in a MonoDevelop Solution under Ubuntu.
If I copy the folder or single files inside my new Solution I get the error
Are you missing a using directive or an assembly reference?
How can I do?
Create a new empty solution, copy your projects and their sources into the solution folder, then right click on the solution in the solution explorer
("View" → "Pads" → "Solution") and choose "Add" → "Add Existing Project".
Rather than opening the VS project file, you may be better off making a MonoDevelop project file and adding the code files and references necessary (typically Accord.dll, Accord.Statistics.dll, or something along those lines) to your new project. That is, of course, if MonoDevelop doesn't have an import function.
Generally, the "missing using or reference" error comes when you use a symbol defined outside of the current project. If you're actually using something from another assembly, you need to add it as a reference to the project, so the metadata is imported and used to link. If it's from the same project, you may need to import the namespace with a using Accord.Statistics.Models statement.
First of all, I would use a separate solution file for MonoDevelop because MD sometimes puts slightly different settings in there. The same applies to .csproj files, so if you want your project compilable with both VS and MD, watch out that you don't commit any project file changes that don't work in VS.
Regarding your problem: Remove the references using MD and re-add them. MD adds references in a way that they're compatible with both VS/MD (my experience).

Categories