Join Ted Neward for an in-depth discussion in this video Demo: References, part of CLR Assemblies Deployment for .NET Developers.
- [Instructor] We can get these assembly references from a variety of different locations. We can either load the assembly directly using some static methods, assembly.load, load file, or load from, and again, this will be something that we talk about in more detail when we talk about assembly resolution in that other Lynda course. We can get ahold of this assembly reference in a variety of ways as I said. Most of these are going to be static methods present here on the assembly class. For example, we can say, let me get ahold of the assembly that is calling me.
In this case, if some other assembly were calling into HelloWorld, that would tell me the assembly that wasn't calling into me. We can get ahold of what is the current code that's executing? In other words, let me get ahold of my own assembly, since this is where I'm executing from, which in this case would return helloworld.exe or we can also use GetEntryAssembly, which is to say which is the assembly where main is defined. For hello world, either GetEntry or GetExecuting would work just fine and so either one of these would print out the full name of helloworld.exe.
Let's go ahead and run that, and I'll show you what I mean. Here we see HelloWorld, and again, this is the full name of the assembly. There's that four part naming convention. HelloWorld is the friendly name, version 184.108.40.206, the culture is neutral, which is to say there is no culture code applied, and because we didn't strongly name HelloWorld, public key token is null. There's another way to get ahold of assemblies as well. That's because, remember as we said earlier, every type knows the assembly from which it came.
I can go ahead and let's reference people here, and I will say ted.GetType, this will return the type for the class ted. That in turn will allow me to get the assembly from which it came, using the assembly property. Now if I do the same exercise, and we do another build, Ctrl + Shift + B, and then I'll go ahead and run this with Ctrl + F5.
Now what we see is People version 1.1, and again, build number and number of seconds, and notice that the number of seconds since midnight has changed since the last time we looked at it because time has passed. The culture is still neutral, but there we can also see the public key token that was used when we strongly named the people assembly. We can get a hold of this at run time, and once I have the assembly reference, I have the ability to navigate some of the contents of the assembly. This is taking us down the path of dot net reflection, which is a whole other course in its own right.
Go check the course catalog, you'll find it there, and you can explore it in greater detail.
- Reviewing the concept of assemblies
- Reviewing library assemblies in ildasm
- Working with assemblies and modules
- Working with assembly scope types
- Reviewing assembly names and versions
- Referencing assemblies from managed code