Skip to content

How to add extern alias for three same namspace assemblies #39

Closed

Description

@dhinesh-v

Activity

  1. zivkan commented on Mar 25, 2020

    @zivkan
    Member

    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.

  2. dhinesh-v commented on Mar 26, 2020

    @dhinesh-v
    Author

    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.

  3. zivkan commented on Mar 26, 2020

    @zivkan
    Member

    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.

  4. dhinesh-v commented on Mar 27, 2020

    @dhinesh-v
    Author

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

  5. zivkan commented on Mar 27, 2020

    @zivkan
    Member

    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:

    image

    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.

  6. zivkan commented on Mar 27, 2020

    @zivkan
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions