Monday, January 13, 2020

Type.GetType Functionality Has Not Changed in .NET Core

In the last article, I talked about how the behavior of Type.GetType() has changed between .NET Framework and .NET Core. This is incorrect.
The behavior of Type.GetType() is the same; it is the runtime behavior that is different.
As I mentioned in that article, "the fastest way to get a right answer is to post a wrong one". And that worked here. An anonymous commenter posted a good description of what is happening (unfortunately they posted anonymously so I'm unable to give them credit where it is due).

Assembly Loading
The confusion comes from the my interpretation of the word "loading" in the documentation.

Here is part of the "Remarks" section that was quoted in the last article (link to doc:

"You can use the GetType method to obtain a Type object for a type in another assembly if you know it's assembly-qualified name, which can be obtained from AssemblyQualifiedName. GetType causes loading of the assembly specified in typeName." (emphasis mine)
In this context, "loading" refers to loading the assembly into the context, not "loading" the assembly from the file system.

The difference really has to do with how assembly resolution works. I am not an expert in these details, so I won't go into them. The bottom line is that assembly resolution works differently in .NET Core compared to .NET Framework.

In either runtime, Type.GetType() causes the assembly to be loaded into the context. The difference in behavior that we see in the example from the previous article is due to assembly resolution and how assemblies are found on the file system.

I will be looking into the details to see how to make sense out of them.

The Recommended Solution
The "recommended" solution is to create a custom assembly load context so that you can handle this resolution yourself in any way you choose.

This approach is shown in the tutorial mentioned in the previous article: Create a .NET Core application with plugins.

This solution is a bit of overkill for my scenario. I don't really need a "plugin" architecture for this application. I need a dynamic loading solution. There will only be one data reader used by the application; which one is used is determined at runtime.

One reason to use a custom assembly context is to isolate the data reader dependencies from the rest of the application. If the data reader and main application need different versions of the same DLL, then loading everything into the same context will not work.

I will put together another version of this project using the plugin architecture, and we can look at the differences and benefits.

Update Jan 2020: Here is the article that describes using a custom assembly load context: Dynamically Loading Types in .NET Core with a Custom Assembly Load Context.

My frustration regarding changes in C# and .NET Core are increasing rather than decreasing.

I am a big fan of .NET Core. I like the simpler approach, the more understandable templates, the cross-platform functionality, the runtime speed, and many other things.

For the most part, when converting applications between .NET Framework and .NET Core, things just work. But when things don't work, it is extremely frustrating to find what went wrong.

Previously I wrote about a change to UseShellExecute that cause an unexpected delay in converting an application. I spent a bit of time simply locating the thing that changed.

In this case, I knew that the assembly loading had changed in .NET Core. I knew that it was possible to load assemblies in a way that makes it easy to unload them. I also knew that it was possible to load multiple versions of the same assembly using these new features. What I did not know was that the default behavior had changed.

In the code-space, Type.GetType() appeared to be the problem. But the real problem was deeper than that. GetType is *technically* working the same. But the overall behavior is different.

I Am Not an Expert
I started this blog in February 2009 with the statement "I am not an expert." That has not changed. I have a bit of programming experience. And I've written lots of applications that make users happy. I also like to think that I'm always looking for ways to improve and expand my knowledge and experience.

My blog, speaking, and videos are all there to share what I've learned -- passing on a technique that helped me understand a subject. Occasionally someone leaves a comment like the following:
"Speaking now in 2020, I can really see how video likes generate more likes. This (and the video before it) are by FAR the best resource for Lambda & LINQ that I have managed [to] find." [Source]
This is from a video on lambda expressions and LINQ that was published in 2015, and it is encouraging that people still find the information useful. That's why I continue.
Unfortunately, over the past couple years, I feel that Microsoft and .NET are leaving the non-experts behind. More and more features and changes require the .NET programmer to become an expert whether they like it or not.
This is discouraging to me since I am on a mission to help the non-expert programmer. That mission is not to create experts; it is to create programmers who can quickly and efficiently make their users happy. You should not need to be an expert to do that.

It's easy to say "just don't use the new features", but that is not always possible. Even if you do not use the new features, you need to understand them. That's because the sample code and tutorials that you find online use these features.

A big reason I've spent so much time trying to make lambda expressions understandable is not because I want people to use lambda expressions (although they are certainly free to). The reason I teach people about lambda expressions is because so many online samples use them. So even if you do not use them in your own code, you need to understand them so that these samples make sense.

With the changes to the frameworks and the languages (in particular Interfaces in C# 8), there is a lot to keep up with. Of the over 50 changes made to the C# language over the last few years, many developers are familiar with 2 or 3 of the more popular ones.

I have spoken with quite a few developers over the past year who do not know what .NET Standard is or why it is important. Some people would say these are bad developers, but I disagree. Many are developers who are focused on getting solutions to users. Some have said "We're not doing cross-platform so we don't care about .NET Core [and .NET Standard]". It's an unfortunately common misconception, and it results from not having enough information to understand the real impact of these things as we move forward to .NET 5.

I'm not advocating that the development environment should be frozen; just that we should take time to understand the impact of the changes that we're making. Like every technology, there are pros and cons. We need to make sure that the benefits are worth it and that we are not leaving a big part of the developer landscape behind.

If we are spending our time retooling and converting applications, when will we have time to actually deliver solutions to our users?

Bringing It Back Around
That was a bit of rant there. It is something that has been weighing on me for a while. And I don't like to point out problems without having a solution.

In this case, I'm going to keep learning, keep moving forward. And when I come across roadblocks, I will find ways around them. And you will be the first to hear about those solutions, even if it is an acknowledged hack or workaround. And when someone points out a better solution, I will pass that along as well.

I'm not ready to give up at this point; hopefully you aren't either.

No comments:

Post a Comment