繁体   English   中英

Assembly.LoadFrom在.NET Core和.NET Framework中的工作方式不同

[英]Assembly.LoadFrom works differently in .NET Core and .NET Framework

我有两个.csproj项目,它们的源代码完全相同:

using Newtonsoft.Json.Linq;
using System;
using System.Reflection;
class Program
{
    static void Main(string[] args)
    {
        Assembly jsonAssembly = Assembly.LoadFrom(@"C:\path\to\Newtonsoft.Json.dll");
        Type reflectionType = jsonAssembly.GetType("Newtonsoft.Json.Linq.JObject");

        Console.Write("Types match: " + (reflectionType == typeof(JObject)));
        Console.Write("Typenames match: " + (reflectionType.FullName == typeof(JObject).FullName));

        JObject cast = (JObject)Activator.CreateInstance(reflectionType);
        Console.Write("Success!");
    }
}

这两个.csproj文件对Newtonsoft.Json的引用相同:

<Reference Include="Newtonsoft.Json">
  <HintPath>C:\path\to\Newtonsoft.Json.dll</HintPath>
</Reference>

这两个项目之间的唯一区别是,我使用.NET Core 2.0创建了第一个项目,而使用.NET Framework 4.6.1创建了第二个项目。

框架:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{A6D2C332-B60D-41F1-9983-DE10065973A1}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>ConsoleApp1</RootNamespace>
    <AssemblyName>ConsoleApp1</AssemblyName>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Newtonsoft.Json">
      <HintPath>C:\path\to\Newtonsoft.Json.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

核心:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="Newtonsoft.Json">
      <HintPath>C:\path\to\Newtonsoft.Json.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>

当我使用.NET Core运行以上代码时,得到以下响应:

Types match: True
Typenames match: True
Success!

当我使用.NET Framework运行上述代码时,得到以下响应:

Types match: False
Typenames match: True

Unhandled Exception: System.InvalidCastException: [A]Newtonsoft.Json.Linq.JObject cannot be cast to [B]Newtonsoft.Json.Linq.JObject

为什么通过反射加载程序集时.NET Framework和.NET Core的工作原理不同?

问题可能是,在.NET Framework中,您使用2个不同的程序集(磁盘上2个不同的文件)。 尝试以调试模式运行.NET Framework项目,并查看这些类型来自何处或添加这些行。

Console.Write("Reflection type module: " + (reflectionType.Module.FullyQualifiedName));
Console.Write("Static type module: " + (typeof(JObject).Module.FullyQualifiedName));

受沃尔特·布兰森(Walter Branson)的回答启发,我进行了一些研究,发现某些Load方法的行为。

    /* Loads closest assembly given by FullName loaded by default, in this case it is from the same location as executable */
    Assembly jsonAssembly1 = Assembly.Load(@"Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed");

    /* Loads assembly exaclty from the file specified by path */
    Assembly jsonAssembly2 = Assembly.LoadFile($@"{pathToAssembly}\Newtonsoft.Json.dll");

    /* In NET.Framework loads same assembly as LoadFrom, but in .NET Core loads same assembly as Load */
    Assembly jsonAssembly3 = Assembly.LoadFrom($@"{pathToAssembly}\Newtonsoft.Json.dll");

我相信这是因为.NET的引用比较失败。 当您查看对象时,.NET中有一个不同的句柄。 当您查看核心中的对象时,它们都有相同的句柄。

我认为这是因为您正在从文件加载程序集。 尝试使用其完全限定的名称加载程序集。

Assembly jsonAssembly = Assebmly.Load("NewtonSoft.Json,Version=12.0.0.0,Culture=neutral,PublicKeyToken=30adfe6b2a6aeed")

.NET Framework会将您引用的.dll复制到其可执行目录。 因此,加载的.dll实际上是不同的。

尝试将.NET Framework项目中的路径从"C:\\path\\to\\Newtonsoft.Json.dll"更改为"Newtonsoft.Json.dll" ,结果将与.NET Core中的结果相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM