Skip to content

Call 'Type.GetType' after 'Assembly.LoadFrom' throw 'System.IO.FileLoadException' #69596

@yejinmo

Description

@yejinmo

Description

When call 'Type.GetType' after 'Assembly.LoadFrom', throw 'System.IO.FileLoadException'

Assembly assembly = Assembly.LoadFrom("Core.dll");
var type = Type.GetType("Foundation.NSObject, Xamarin.iOS");
Console.WriteLine("hello world");

Crash:

Unhandled exception. System.IO.FileLoadException:
File name: 'Xamarin.iOS, Culture=neutral, PublicKeyToken=null'
 ---> System.ArgumentException: The path is empty. (Parameter 'path')
   at System.IO.Path.GetFullPath(String path)
   at System.Reflection.Assembly.LoadFromResolveHandler(Object sender, ResolveEventArgs args)
   at System.Runtime.Loader.AssemblyLoadContext.InvokeResolveEvent(ResolveEventHandler eventHandler, RuntimeAssembly assembly, String name)
   at System.Runtime.Loader.AssemblyLoadContext.OnAssemblyResolve(RuntimeAssembly assembly, String assemblyFullName)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, StackCrawlMarkHandle stackMark, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack type, ObjectHandleOnStack keepalive)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext)
   at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, StackCrawlMark& stackMark)
   at System.Type.GetType(String typeName)
   at Test.Program.Main(String[] args) in D:\Test\Test\Program.cs:line 12

Reproduction Steps

  1. Create .Net Standard 2.0 dll project, write something or nothing, compile
using System;

namespace Core
{
    public class Class1
    {

    }
}
  1. Create .Net 6 console project, code:
using System;
using System.IO;
using System.Reflection;

namespace Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.LoadFrom("Core.dll");
            var type = Type.GetType("Foundation.NSObject, Xamarin.iOS");
            Console.WriteLine("hello world");
        }
    }
}
  1. Publish, using .Net 6 win-x64 SingleFile SelfContained
  2. Copy Core.dll to Test.exe root directory
  3. Run Test.exe

Expected behavior

Output:

hello world

Actual behavior

Crash:

Unhandled exception. System.IO.FileLoadException:
File name: 'Xamarin.iOS, Culture=neutral, PublicKeyToken=null'
 ---> System.ArgumentException: The path is empty. (Parameter 'path')
   at System.IO.Path.GetFullPath(String path)
   at System.Reflection.Assembly.LoadFromResolveHandler(Object sender, ResolveEventArgs args)
   at System.Runtime.Loader.AssemblyLoadContext.InvokeResolveEvent(ResolveEventHandler eventHandler, RuntimeAssembly assembly, String name)
   at System.Runtime.Loader.AssemblyLoadContext.OnAssemblyResolve(RuntimeAssembly assembly, String assemblyFullName)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, StackCrawlMarkHandle stackMark, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack type, ObjectHandleOnStack keepalive)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext)
   at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, StackCrawlMark& stackMark)
   at System.Type.GetType(String typeName)
   at Test.Program.Main(String[] args) in D:\Test\Test\Program.cs:line 12

Regression?

No response

Known Workarounds

This is not the solution!
If not used Assembly.LoadFrom everything ok.

            // Assembly assembly = Assembly.LoadFrom("Core.dll");
            var type = Type.GetType("Foundation.NSObject, Xamarin.iOS");
            Console.WriteLine("hello world");

Configuration

  • .Net 6
  • Windows / Linux
  • x64
  • Single File

pubxml:

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
  <PropertyGroup>
    <Configuration>Release</Configuration>
    <Platform>Any CPU</Platform>
    <PublishDir>bin\Release\net6.0\publish\linux-x64\</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <TargetFramework>net6.0</TargetFramework>
    <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishTrimmed>false</PublishTrimmed>
  </PropertyGroup>
</Project>

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions