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