Cannot get Type declared in a .NET Framework assembly from a .NET Core 2 assembly

0

I have a .NET Core 2 console app which is performing some maintenance/upgrade work on the DB underlying a ASP.NET website running on .NET Framework 4.7.1.

The console app needs to read in the assembly-qualified type names of classes which are declared in the .NET Framework assemblies. It does this using Type.GetType("[assembly-qualified name]";

This code works perfectly well for the built-in .NET types but it throws an exception when it comes to types defined in our own assemblies. Confusingly, I can declare a variable of one of these types in the .NET Core console app but I'm unable to instantiate a Type object referencing the same type.

For example, this works...

var cs = Common.CustomerManagement.Person.CustomerStatus.ApprovedCustomer;

but this...

var ct = Type.GetType("Common.CustomerManagement.Person+CustomerStatus, Common, Version=1.0.6793.16016, Culture=neutral, PublicKeyToken=null;");

...throws the exception...

System.IO.FileLoadException: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type, ObjectHandleOnStack keepalive)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
at System.Type.GetType(String typeName)

I realise that in the code which works I'm referencing an enum on the type and in the code which doesn't work I'm referencing just the type, but the working code shows that the referenced .NET Framework assembly can be loaded whereas the non-working code suggests that the type is not available to the .NET Core console app.

c#
types
.net-core
asked on Stack Overflow Aug 7, 2018 by awj

1 Answer

0

The problem was that the type name is the Type.AssemblyQualifiedName which includes a version number.

It's the version number which was causing the problem. Once I removed the version number and left the other information in the type name, it worked.

answered on Stack Overflow Aug 7, 2018 by awj

User contributions licensed under CC BY-SA 3.0