Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to `extern alias` an assembly with .Net core?

Tags:

TLDR: Everything is pretty much in the title.

Suppose that your project.json uses two packages that have a two types which are named the same (same name, same namespace).

How to use one of thoses types ?

With classing .Net, that's easy. Just use an extern alias.

But how do you do that using .net core ? I dont see any option in package.json that would let me define an alias like I would do in an assembly reference property window.

The only mention of this issue i managed to find is here

[edit] It seems that there is an open issue here

like image 893
Olivier Avatar asked Mar 09 '16 18:03

Olivier


People also ask

How to use extern alias c#?

Using Visual Studio dll to your project in Visual Studio. Open a property tab and change the Aliases from global to GridV1 and GridV2 respectively. extern alias GridV1; extern alias GridV2; Now you can create alias for a namespace or a type by using alias directive.

Can we create alias of a namespace in C#?

You can also create an alias for a namespace or a type with a using alias directive.

What is extern keyword in C#?

The extern modifier is used to declare a method that is implemented externally. A common use of the extern modifier is with the DllImport attribute when you are using Interop services to call into unmanaged code. In this case, the method must also be declared as static , as shown in the following example: C# Copy.

Which of the following keyword is used to reference two assemblies with the same fully qualified type names?

Question 3: Which of the following keyword is used to reference two assemblies with the same fully qualified type names? This creates the external aliases GridV1 and GridV2 . To use these aliases from within a program, reference them by using the extern keyword.


1 Answers

I believe the issue you are trying to link to is here:
https://github.com/NuGet/Home/issues/4989

You can workaround by using an MSBuild Target. drop this snippet into Directory.Build.targets:

<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> 

and then use it in your csproj in your PackageReference nodes like this:

<ItemGroup>     <PackageReference Include="StackExchange.Redis.StrongName" Version="1.2.6" Alias="signed" /> </ItemGroup> 

added this as a comment to the GitHub issue:
https://github.com/NuGet/Home/issues/4989#issuecomment-426666530

like image 149
Dave Thieben Avatar answered Oct 13 '22 00:10

Dave Thieben