How Does Assembly Know Which Data Member To Access?
C# is a compiled language. This means that when a developer creates C# source files, those files need to be compiled before they can run. Y'all tin can non write a C# file, and then run that file or combination of files similar you can with say a JavaScript or PHP file. This is where assemblies come up into play for a compiled linguistic communication. Assembly files are the consequence of the C# compiler reading in human being readable C# source files, and so producing a file the application can employ. In fact a C# program volition often consist of many assembly files working together.
Compiling A C# Source File
You lot actually don't need a fancy IDE to write and compile a C# program. Of class information technology is many times easier since something like Visual Studio will exercise a lot of the heavy lifting for you. Let's see how to apply the C# compiler right from the command line using the csc.exe program.
In the command line, we can use notepad.exe to create a unproblematic keto.cs plan file. We'll make a program that just outputs, "I Like Keto". Check out this uncomplicated program hither. We don't get any syntax highlighting or assist at all like with Visual Studio.
Now, nosotros desire to compile this source code file right from the command line. Well csc.exe is the plan we want to use and it lives at C:WindowsMicrosoft.NETFramework>v4.0.30319csc equally seen here.
To compile our C Precipitous source file, nosotros can simply telephone call the csc.exe programme from the command line and pass in the name of the file that we want to compile. Hither nosotros compile the keto.cs file into a keto.exe file.
Cool! The compiler has created an executable file that tin now be run from the control line. In fact, we merely created our very first Assembly in C#! Yes, it is an exe file, just technically it tin be referred to as an Assembly. A .Net associates is a file that contains compiled code which can now execute within the Common Linguistic communication Runtime. Hither is our assembly in all of its glory.
.Cyberspace assemblies are files which have either a .exe or a .dll extension. The first blazon is a file that is executable, like we only saw in a higher place. Dll files are a little flake different. A dll file means it is what is known every bit a dynamic link library. Dll files are not executed like an exe file, but they can be loaded by other programs and run the code contained in them. These types of files are used oft for code reuse. The .Cyberspace Framework is a great example of code reuse. Common classes like Console and DateTime in .Net live inside an assembly the .NET framework provides named mscorlib.dll.
The Global Assembly Cache
The .NET assemblies are part of the Global Assembly Enshroud on a Windows computer. The Global Assembly Cache is located at the C:WindowsassemblyGAC> directly similar nosotros meet here.
The assemblies stored in the GAC can be used by any .NET applications running on the computer. Common assemblies like mscorlib and others alive in the GAC making 1 copy available for any applications that need it on the computer.
Working With Assemblies In Visual Studio
When building a solution in Visual Studio, what is happening is all of the source lawmaking files are collected together and built into an assembly. In this scenario it is being built into an executable assembly, or a file with a .exe extension. This file gets created by the C# compiler in the bin/Debug directory. Allow's accept a peek:
C:UsersusersourcereposStocksStocksbinDebug>dir Book in drive C is OSDisk Volume Serial Number is F232-B880 Directory of C:UsersusersourcereposStocksStocksbinDebug 02/05/2019 01:43 PM. 02/05/2019 01:43 PM .. 02/06/2019 05:29 PM 5,632 Stocks.exe 02/04/2019 04:46 PM 189 Stocks.exe.config 02/06/2019 05:29 PM 22,016 Stocks.pdb three File(s) 27,837 bytes 2 Dir(south) 83,911,729,152 bytes free
Nosotros can run across that Stocks.exe is a file that the C# compiler created. It is an associates which can be run considering of it's .exe extension. The reason for this is because when we created this project, nosotros chose to create a Panel Application with the name of Stocks. Visual Studio has a bunch of options that get configured based on the blazon of projection yous choose. The assembly name is the same as the project name, so in this instance it is Stocks.
The extension given to the assembly is controlled by the Output type setting. For either the Windows Awarding or Panel Application, we volition go that .exe extension. If we choose the Form Library option, the assembly would non longer get an .exe extension but instead a .dll extension.
Simply for grins, nosotros tin change the output type to Form Library and so build the solution again. Now when we look at the directory contents in the Debug folder, sure plenty we see Stocks.dll instead of Stocks.exe.
C:UsersusersourcereposStocksStocksbinDebug>dir Volume in drive C is OSDisk Volume Series Number is F232-B880 Directory of C:UsersusersourcereposStocksStocksbinDebug 02/08/2019 04:53 PM. 02/08/2019 04:53 PM .. 02/08/2019 04:53 PM 5,120 Stocks.dll 02/04/2019 04:46 PM 189 Stocks.dll.config 02/08/2019 04:53 PM nineteen,968 Stocks.pdb three File(s) 25,277 bytes ii Dir(s) 83,904,868,352 bytes costless
This type of file can not be run straight. In fact if we endeavor to do so we get a bulletin from Visual Studio proverb, "A project with an Output Blazon of Clas Library cannot exist started straight. In order to debug this project, add an executable projection to this solution which references the library projection. Set the executable project as the startup project."
Become To Assembly Definition
A keen way to learn more about an assembly you are interested in is to correct-click on the assembly name in Visual Studio, and then choose "Become To Definition". Here, y'all can see we correct-click on the Console assembly and and then Go To Definition.
Visual Studio will then open a new window with an outline of all the bachelor methods in that class. This is not the source lawmaking, just almost similar a collection of interfaces. The file shows you what you tin can invoke from the Console class, much like when you're typing and intellisense tries to judge what method you want to use. Notice at the very summit of the file we tin meet what assembly it is using.
#region Associates mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 // C:Programme Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1mscorlib.dll
0 Response to "How Does Assembly Know Which Data Member To Access?"
Post a Comment