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

后端 未结 4 657
时光取名叫无心
时光取名叫无心 2021-01-04 00:23

TLDR: Everything is pretty much in the title.

Suppose that your project.json uses two packages that have a two types which are named th

相关标签:
4条回答
  • 2021-01-04 00:39

    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

    0 讨论(0)
  • 2021-01-04 00:50

    You can do it in this way:

    extern alias Lib1;
    extern alias Lib2;
    using System;
    using SpaceOne=Lib1::Space;
    using SpaceTwo=Lib2::Space;
    

    Example you can find here: https://github.com/ernado-x/owl/blob/master/src/Owl/Program.cs

    0 讨论(0)
  • 2021-01-04 00:58

    I had a problem in .Net Core 2.2 where the MySqlConnector type name and namespace had a collision and the answer was to create an alias. I think this should work for your needs too, just change MySqlConnector below accordingly: https://stackoverflow.com/a/48686823/479701

    <Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
        <ItemGroup>
          <ReferencePath Condition="'%(FileName)' == 'MySqlConnector'">
            <Aliases>MySqlConnectorAlias</Aliases>
          </ReferencePath>
        </ItemGroup>
      </Target>
    

    then in your cs file before usings:

    extern alias MySqlConnectorAlias;
    

    then reference your type like this:

    MySqlConnectorAlias::MySql.Data.MySqlClient.MySqlConnection
    
    0 讨论(0)
  • 2021-01-04 01:00

    There is no support for this at the moment. Feel free to file an issue in dotnet cli repo https://github.com/dotnet/cli/issues

    0 讨论(0)
提交回复
热议问题