csproj: Reference a dll file and Librarys - unity3d

Im new to csproj and msbuild and im trying to Build a small third Party PlugIn for Unity. The Problem is that the view Libs im using are not in Unity. So i have to build a separate PlugIn for Unity. Can somebody show me how to Reference a dll and a lib in csproj. The following Attributes im using are:
<ItemGroup>
<Reference Include="System.Threading" />
<Reference Include="System.Windows.Storage" />
<Reference Include="./bin/Windows.Devices.Enumeration.dll" />
</ItemGroup>
Also i tried PackageReference with Nuget. But at first i want to use the normal way to reference libs with csproj. Or do i have to use nuget? Don`t know. For the build i use Visual Studio Code with the specific PlugIns.
Thanks and Cheers.

You can use dlls in your Unity project by putting them in the Assets/Plugins folder.
https://docs.unity3d.com/Manual/SpecialFolders.html
If you want to have your code build to a separate dll, you can use assembly definition files.
https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html

Related

create multi target nuget package

hi i want create multi target nuget package. Everything seems to work well Except that whene i create wpf NetCore3 app and install my package The .NET Framework dll is used and The NetCore3 library is not used
<files>
<file src="lib\netcore\Control.dll" target="lib\netcore" />
<file src="lib\net48\Control.dll" target="lib\net48" />
<file src="lib\net40\Control.dll" target="lib\net40" />
<file src="lib\net40\Microsoft.Windows.Shell.dll" target="lib\net40" />
</files>
Is this lib\netcore correct?
You should use the same TFM in the package as your csproj has in the <TargetFramework> element. If your csproj has <TargetFramework>netcore</TargetFramework>, then sure, use lib/netcore/whatever.dll. But if your csproj has <TargetFramework>netcoreapp3.0</TargetFramework>, then you should use lib/netcoreapp3.0/whatever.dll.
However, SDK style projects, the only type that work with .NET Core 3.0, support multi-targeting (change <TargetFramework> to <TargetFrameworks>, then use a semicolon delimited list netcoreapp3.0;net48;net40), and NuGet's pack targets know how to pack these projects automatically. So there is no need to create a nuspec yourself, which minimises the risk of making these types of mistakes.
So, just as NuGet's docs on creating multi-targeting packages says, just use dotnet pack to create your package, and let NuGet figure out what lib/* folders to use. Avoid using nuspec files. Any other metadata you specify in the nuspec can specified via MSBuild properties in your csproj.
netcore is a Microsoft Store TFM.
For your .NET Core 3 (netcoreapp3.0) WPF app, you'd need to multi-target with netstandard or netcoreapp in your NuGet package.
For example:
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>

Nuget packages bundled in teamcity not installing documentation files

So basically I am building nuget packages in TeamCity via a .proj file that runs a "pack" target:
<MSBuild
Projects="$(MSBuildProjectDirectory)\PROJNAME.csproj"
Targets="Rebuild;pack"
Properties="Configuration=$(Configuration);Version=$(BUILD_NUMBER)" />
With an artifact output of:
PROJNAME\bin\Release\PROJNAME.%build.number%.nupkg
This works nicely for basic consuming of the nuget package, however I am having trouble getting the documentation xml files to work.
I have looked inside the output nupkg and I see that the documentation xml is actually bundled and included in the package, however the problem is that when I finally restore nuget packages in my consuming project, the dll gets copied across as expected, however the documentation does not.
I wondered if this is because of the TC generated .nuspec file, and if I may need to abandon teamcities nuspec and create my own, however I was hoping to avoid this, given it works nicely the way it is, and handles versioning etc.
Is there a simple way to include documentation xml when the package is restored?
In the end i found it came down to 3 things, being:
Ensure projects configuration is set to generate documentation.
Either by adding code manually such as:
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"><DocumentationFile>bin\$(Configuration)\netstandard2.0\Project.documentation.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <DocumentationFile>bin\$(Configuration)\netstandard2.0\Project.documentation.xml</DocumentationFile>
</PropertyGroup>
Or alternatively via the Visual Studio Project properties menu, if you are doing it through VS also make sure you do it for all configurations (as depicted as A in the picture below):
Add EnableDocumentationFile to your .csproj file, eg:
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>project</RootNamespace>
<Configurations>Debug;Release</Configurations>
<EnableDocumentationFile>true</EnableDocumentationFile>
</PropertyGroup>
and most importantly let your project know (again in your .csproj) that it should be copying over the documentation file, and use PackageFlatten if you want it to appear at the same level as your package dll:
<ItemGroup>
<None Remove="bin\$(Configuration)\netstandard2.0\Project.documentation.xml" />
</ItemGroup>
<ItemGroup>
<Content Include="bin\$(Configuration)\netstandard2.0\Project.documentation.xml">
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
<PackageFlatten>true</PackageFlatten>
</Content>
</ItemGroup>

nuget package with legacy DLLs

I want to create a nuget package that contains besides .NET assemblies "legacy" DLLs (non .NET DLLs), which are referenced or accessed by the assembly DLLs.
Where do I put these DLLs? How does the users program (exe), which uses my package, get access to these DLLs?
I can only think of two ways the users program can access these DLLs: either there is a PATH environment variable set to the appropriate directory or these DLLs are copied into the bin/Release and bin/Debug directories of the users project.
How would I deal with x86 and x64 versions of the legacy DLLs?
I would look at adding a custom MSBuild .targets to your NuGet package. Inside this .targets file you can define any MSBuild tasks you need which will allow you to hook into the build of the project. So you could have the non-.NET dlls copied into the output directory using MSBuild.
The PostSharp NuGet package is one example that copies extra files around during the build. However its MSBuild .targets file is fairly complex. You would not need to do anything as complicated as that. Probably something simple similar to:
<PropertyGroup>
<PrepareForRunDependsOn>$(PrepareForRunDependsOn);MyPostBuildTarget</PrepareForRunDependsOn>
</PropertyGroup>
<ItemGroup>
<ExtraFile Include="dlls\*.dll" />
</ItemGroup>
<Target Name="MyPostBuildTarget">
<Copy SourceFiles="#(ExtraFile)" DestinationFolder="$(OutputPath)" />
</Target>
The above is only an idea of what you could do.

NuGet Hide Files in Visual Studio

Question:
Is there a way to copy the files over in the NuGet, but have it not load the folder into the Visual Studio solution?
Explanation:
I am working on a NuGet Package that adds files to be used by the user. So I add the files to the NuGet Packages like so:
<files>
<file src="Commands\*" target="Content\Commands" />
<file src="Settings\*" target="Content\Settings" />
<file src="Scripts\*" target="Content\Scripts" />
</files>
By doing this all the folders are visible when you add the NuGet to your Visual Studio project.
The problem is that I do not want anyone to manipulate the "Scripts" folder. So I want it to not be visible or loaded into the Visual Studio Solution. But I need it in the packages folder for it to work properly. I know people will alter it if they choose to, but I am just trying to see if I can hide it so that they do not have it in their solution.
I have been looking at the documentation and I do not think it allows it, but was curious if anyone knows a way to achieve it.

how to tell NuGet to install package per project and not solution

I have a class library project which is reused across different solutions.
I would like NuGet to add a toolkit reference to the class library project and store it in the project folder not in the solution folder.
Example:
I have
D:\Projects\MyClassLibrary
D:\Projects\Solution1
D:\Projects\Solution2
Using NuGet, I want to add a toolkit to the MyClassLibrary project.
Right now, if I have Solution1 open, NuGet is adding the toolkit to
D:\Projects\Solution1\packages
I would like to tell NuGet to install the toolkit in the MyClassLibrary\packages folder instead.
How to do this?
You can change the default packages folder location since nuget 2.1:
<configuration>
<config>
<add key=" repositoryPath" value=" C:\myteam\teampackages" />
</config>
...
</configuration>
reference: Specify ‘packages’ Folder Location
-----------------UPDATE-------------------------
you have to enable nuget automatic package restore for the solution.
When you do it nuget create a .nuget folder in the solution root.
Inside that folder there are the file NuGet.Config where you can put this configuration.
-----------------(last) UPDATE 2020-------------------------
nuget has changed since and this answer does not apply anymore