Comments (6)
Try something like this
<Target Name="AddCustomAliases" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'Assembly1' AND '%(ReferencePath.NuGetPackageId)' == 'ClassLib2'">
<Aliases>alias1</Aliases>
</ReferencePath>
<ReferencePath Condition="'%(FileName)' == 'Assembly2' AND '%(ReferencePath.NuGetPackageId)' == 'ClassLib2'">
<Aliases>alias2</Aliases>
</ReferencePath>
<ReferencePath Condition="'%(FileName)' == 'Assembly3' AND '%(ReferencePath.NuGetPackageId)' == 'ClassLib2'">
<Aliases>alias3</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
Change the assembly and package ids as necessary.
from samples.
Hi @zivkan ,
I have tried your solution, but only one alias is working, Other aliases are not working and showing below error,
The extern alias 'LatestCore' was not specified in a /reference option
Also I have tried below solution,
<Target Name="AddPackageAliases" BeforeTargets="ResolveReferences" Outputs="%(PackageReference.Identity)">
<PropertyGroup>
<AliasPackageReference>@(PackageReference->'%(Identity)')</AliasPackageReference>
<AliasName>@(PackageReference->'%(Alias)')</AliasName>
</PropertyGroup>
<ItemGroup>
<ReferencePath Condition="'%(FileName)'=='$(AliasPackageReference)'">
<Aliases>$(AliasName)</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
<ItemGroup>
<PackageReference Include="KeyGen_Old.Core" Version="1.0.0" Alias="OldCore" />
<PackageReference Include="KeyGen.Core" Version="2.0.0" Alias="Core" />
<PackageReference Include="KeyGen_Latest.Core" Version="3.0.0" Alias="LatestCore" />
</ItemGroup>
But same problem happens.
from samples.
This worked for me at build, but intellisense doesn't know about the aliases
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGet.Frameworks" Version="5.5.0" />
<PackageReference Include="NuGet.Versioning" Version="5.5.0" />
</ItemGroup>
<Target Name="AddCustomAliases" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'NuGet.Frameworks' AND '%(ReferencePath.NuGetPackageId)' == 'NuGet.Frameworks'">
<Aliases>alias1</Aliases>
</ReferencePath>
<ReferencePath Condition="'%(FileName)' == 'NuGet.Versioning' AND '%(ReferencePath.NuGetPackageId)' == 'NuGet.Versioning'">
<Aliases>alias2</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
</Project>
extern alias alias1;
extern alias alias2;
using System;
namespace aliasTest
{
public class Class1
{
public void DoNothing()
{
var netstandard2_0 = alias1.NuGet.Frameworks.FrameworkConstants.CommonFrameworks.NetStandard20;
var allStable = alias2.NuGet.Versioning.VersionRange.AllStable;
}
}
}
If you need to debug, I suggest running a build with the -bl
option (dotnet build -bl
or msbuild -bl
), then open the msbuild.binlog
file with the MSBuild binary log viewer. You can then look around to understand why the ReferencePath items are not getting the correct aliases metadata.
from samples.
@zivkan Thank you so much for helping me here, I'm really sorry that i didn't clearly explain the issue.
I can able to alias two assemblies which has different namespace. But i want to alias three assemblies which have same namespace.
from samples.
My example shows the concept. I don't know two packages on nuget.org that contain types in the same namespace, so I used NuGet.Versioning
and NuGet.Frameworks
as an example. But you can see in the C# file that it uses alias1
and alias2
and the code compiles, meaning the compiler was passed both alias names.
As I mentioned in the last paragraph of my example, you can use MSBuild's binary logs to check the ReferencePath items have the correct metadata:
Similarly on the Csc
task, I can see /reference:alias1=C:\git\test\globalPackages\nuget.frameworks\5.5.0\lib\netstandard2.0\NuGet.Frameworks.dll /reference:alias2=C:\git\test\globalPackages\nuget.versioning\5.5.0\lib\netstandard2.0\NuGet.Versioning.dll
If you're getting the same alias on the assembly from different packages, I assume that you removed the AND '%(ReferencePath.NuGetPackageId)' == 'NuGet.Frameworks'
part of the condition.
You can also add <Message Text="whatever"
to add tracing information to help you debug your target.
from samples.
Closing as you're also engaging with us at NuGet/Home#4989
In case it helps, msbuild doesn't know anything about .NET namespaces, that's a compiler issue. MSBuild is just a scripting language to run a bunch of tasks (the compiler is one task), by getting lists of files and generating task input parameters. So, it has no way of knowing whether two assemblies you want to use aliases on have the same or different namespaces inside.
from samples.
Related Issues (18)
- Fails to Copy Satellite Assemblies HOT 2
- Copyrights? HOT 1
- Linked assemblies should not be copied to output directory HOT 5
- Invalid XML in NativePackage .targets files in ExtensionSDKasNuGetPackage Sample HOT 7
- WinRT information: Class not registered HOT 1
- Is there a content-file sample that base on .netstandard project HOT 1
- ContentFilesExample problems HOT 1
- Querying most downloaded packages from nuget.org HOT 1
- The Target element tag is not closed HOT 1
- No (non-framework) satelitte references added to Project for Nuget 2.8.3
- Are the NuGet libraries worth using? HOT 8
- How do you run CatalogReaderExample.dll from a command prompt, outside Visual Studio? HOT 3
- Provide a sample to demonstrate custom credential provider
- Icon still not showing up HOT 9
- Can't correctly embed interop types for packagereference HOT 2
- Sample depends on wrong version of https://www.nuget.org/packages/ClassLibrary/1.0.0/ HOT 2
- Is this only for UWP? What about WPF or Xamarin Forms? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from samples.