Pro .NET 5 Custom Libraries: Implementing Custom .NET Data Types 1st Edition Roger Villela - The ebook is available for quick download, easy access to content
Pro .NET 5 Custom Libraries: Implementing Custom .NET Data Types 1st Edition Roger Villela - The ebook is available for quick download, easy access to content
com
https://textbookfull.com/product/pro-net-5-custom-libraries-
implementing-custom-net-data-types-1st-edition-roger-
villela/
OR CLICK HERE
DOWLOAD EBOOK
https://textbookfull.com/product/pro-c-7-with-net-and-net-core-andrew-
troelsen/
textbookfull.com
The publisher, the authors and the editors are safe to assume that the
advice and information in this book are believed to be true and accurate
at the date of publication. Neither the publisher nor the authors or the
editors give a warranty, expressed or implied, with respect to the
material contained herein or for any errors or omissions that may have
been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
1. .NET Platform
Roger Villela1
(1) Sao Paulo, Sã o Paulo, Brazil
This chapter provides an overview of .NET 5 (previously .NET Core) and describes the fundamental
architectural and the engineering features that you should expect in any implementation of .NET 5
(regardless of hardware, operating system, or execution system).
Acronyms
The following acronyms are introduced in this chapter:
Base Class Library (BCL)
Common Intermediate Language (CIL)
Common Language Infrastructure (CLI)
Common Language Runtime (CLR)
Common Type System (CTS)
Framework Class Library (FCL) (Although not specific to the .NET Framework implementation, the
term is used for the full range of .NET types available in an official distribution of .NET.)
Intermediate Language (IL)
Microsoft Intermediate Language (MSIL)
Virtual Execution System (VES)
Windows Presentation Foundation (WPF) (a.k.a. execution engine)
Figure 1-3 Some .NET types (for example, System.Array) are abstract data types and are implemented partially by code
generation of the compiler (for example, a C# compiler)
Figure 1-4 System.ValueType is a reference type, and System.Int32 is a value type derived from System.ValueType, which
inherits from the System.Object reference type
In the C# programming language, because C# treats System.Object as the base class, we do not
need to use the System.Object root data type explicitly when we do not have another class as the
base data type.
In fact, the execution environment of the CLR (the VES) assumes this; therefore, most
programming languages do not require that System.Object be informed explicitly in this scenario.
However, it is good programming practice to explicitly use the base data type in such cases. Otherwise,
this can become error prone when using more than one programming language in a project,
potentially resulting in erroneous perceptions about the inheritance model supported by the .NET
execution environment and the transfer of the desired feature to the programming language and the
adoption of different programming languages (all because of basic resources of syntax).
#region Namespaces
using System;
#endregion
namespace ConsoleClient {
public static class Program : System.Object {
public static void Main() {
return;
}
};
};
Listing 1-1 Typical Source Code in the C# Programming Language for a Console Application with an Entry-Point
Member Method Called Program.Main()
.entrypoint
// Code size 1 (0x1)
.maxstack 8
ret
.NET Platform
Microsoft .NET is the official commercial name for the group of technologies and tools designed and
implemented based on what is in the ECMA-335 standard specification.
Common Language Runtime, as the name suggests, is an implementation based on the CLI
standard specification, and an implementation of the CLR has a set of elements for a fundamental
architectural model. Each element has a fundamental set of conceptual definitions and rules to be
followed, and engineering mechanisms to be implemented, independently of the target operating
system and hardware platforms.
When we are implementing a CLR environment and technologies of a .NET platform, we are
creating software elements for a platform that is a nonspecific hardware-based computer (more
specifically, a software-only computer, and more commonly known as a virtual computer ). This
description includes when planning and implementing custom data types, custom components,
custom controls, custom libraries, and specialized tools and frameworks.
For this text, we are using a .NET 5 implementation of the CLR for the sample projects and
respective source code.
You can check for the most up-to-date versions of .NET 5 at the official Microsoft website:
https://dotnet.microsoft.com/download/dotnet/5.0.
You can also use GitHub to access the source code of the runtime, libraries, and frameworks made
using the CLR components and technologies and BCL fundamental library, as we have with Windows
Forms (Microsoft Windows only), Windows Presentation Foundation (Microsoft Windows only),
ASP.NET Core (Windows, Apple MacOS, Linux), and the .NET SDK Installer:
https://github.com/dotnet/runtime
https://github.com/dotnet/aspnetcore
https://github.com/dotnet/winforms
https://github.com/dotnet/wpf
https://github.com/dotnet/installer
Independently or together, these abstract aspects focus on management of data types. So,
reasonably, that form of environment and its components is known as a managed environment .
As mentioned previously, in this book we use a .NET 5 implementation of the CLR for the sample
projects and respective source code. So, whenever you see CLR mentioned, this means the .NET 5
running on Microsoft Windows 10 2004 and Microsoft Visual Studio 2019 16.7.3 or more recent
(Community, Professional, Enterprise). The following programming languages are used in this book:
C#
MSIL
For example, when we are developing some application and choose the System.String
reference type, we are using one of the fundamental types available through the BCL.
However, the string reference type exists only because the CTS has the string fundamental built-
in type defined on it, which is one of the platform-specific fundamental built-in types upon which
string operations are built. In fact, the string content (value of) in any instance is made up of a
sequence of values of the CTS char platform fundamental built-in type, which is System.Char
fundamental data type in the BCL. These platform fundamental built-in types, BCL fundamental types,
and any other types derived or based on them follow the rules described by the unified type system.
In the CLI specification, this unified type system is the CTS, which describes rules about
conceptual, structural, and behavioral elements that must be followed by the CLI itself and specialized
tools (such as compilers and runtime environments).
You’ll learn more information about these types in Chapter 2 and in discussions about the CLR
throughout this book. For now, though, Table 1-1 shows the types defined by the CTS and described by
the metadata.
System.Object object
System.String string
System.Single float32
System.Double float64
System.SByte int8
System.Int16 int16
System.Int32 int32
System.Int64 int64
System.IntPtr native int
System.TypedReference typedref
System.Byte unsigned uint8
A type system model describes the necessary rules related to each type’s conceptual, structural,
and behavioral characteristics.
A value is a bit pattern used to represent types such as numbers (for example, integer numbers and
float-pointing numbers).
Listing 1-4 shows examples in C# for two variables for instances of the System.UInt32 BCL
value type (and not a simple value).
Remember that this way of work is defined by CTS and supported by VES in the CLR. From the
perspective of the type system and execution environment, it is necessary that an object be declared,
defined, and implemented to work within the CLR.
Table 1-3 describes the fundamental built-in types defined by CTS. As the table shows, the root
object type is accessible through the object keyword of the CIL. So that programming languages
such as C#, C++/CLI projection, F#, VB.NET, and others can access this root object type of the
platform, there is a library of fundamental types that is part of the CLI specification. This foundational
library is the BCL.
This root object type is the System.Object reference type. When declaring a variable of the
object type (CTS model definition) or System.Object (BCL) reference type using any high-level
programming language such as C#, C++/CLI projection, F#, VB.NET, and so on, the compiler generates
an intermediate code using the object keyword of the CIL. Table 1-4 summarizes and helps you
understand and memorize this sequence in a straightforward way.
.NET Module
When we use C++ to write code, the result of the compiled and linked code is a binary file in a specific
format. In this case, we are working with PE/COFF (Portable Executable / Common Object File
Format), which is used by the Microsoft Windows operating system. When we use C# to write code, or
when we use any other programming language or group of extensions that adhere to the CLI
specification, the resulting binary file is in the same PE/COFF format. However, that resulting binary
file has some data structures changed/included to support the requirements described by CLI
specification and aspects of the Microsoft Windows operating system. This is called the CLI PE/COFF
module.
Currently, on Microsoft Windows, the CLI PE/COFF module can have .EXE, .DLL, .netmodule,
.WinMD, and .UWP extensions created and recognized by the operation system or development tools.
In addition, it can have any other extension that can be registered and recognized by the operating
system or specialized tools (for software development or not).
In fact, the use of an extension is not required, but it is a good practice and the accepted standard.
If we are using .NET 5 or .NET Core (not the old Windows-only .NET Framework) in a different
operating system and on a different hardware platform, the extensions and file formats used are
specific to such software and hardware environments. However, the fundamental structural resources
defined in CLI as a starting point are the same.
One VES responsibility is to load the CLI PE/COFF modules. Doing so includes verifying some
structural rules about the file format and guaranteeing that all information is as expected. The VES
uses the metadata information in the CLI PE/COFF modules to verify that the structural aspects are
recognized by the rules that it knows as valid, required, or optional. If the structural elements exist
and are valid, the next step is to apply the rules based on the nature of the elements and the context of
use.
For example, if the element is a managed type, the execution system needs to verify whether it is a
value type or a reference type.
If the element is an assembly reference type, one responsibility of this type is to describe various
characteristics of the managed module (structural and behavioral), such as the relationships it has
with other managed modules and what managed types are in it (and in any other managed module).
.NET Assemblies
People often wonder what a .NET assembly is exactly. Put simply, and as defined and described by the
CLI, an assembly is a logical unit for management and deployment of resources designed to work
together. In an implementation of CLR, assemblies can be static or dynamic.
Static Assemblies
Static assemblies are those stored in a storage device, such as a typical hard disk. In Microsoft
Windows, the file format of each module is the CLI PE/COFF. These assemblies have typical .NET 5
types and other specialized resources (audio/video files, localization support files, images, and
custom files created specifically for the application), depending on the purpose of each application.
.NET 5 and .NET Core include the following assemblies and modules, for example:
Assembly mscorlib
Module mscorlib.dll
Module System.Runtime.dll
Module netstandard.dll
Assembly System.Activities (part of Microsoft Windows Workflow Foundation)
Module System.Activities.dll
Assembly System.Diagnostics.Debug
Module System.Diagnostics.Debug.dll
Module System.dll
Module netstandard.dll
Dynamic Assemblies
Dynamic assemblies are created dynamically at runtime and are created via specialized API calls of
.NET 5/Core. These dynamic assemblies are created and executed directly in memory. However, the
dynamic assembly can be saved in a storage device, but only after being executed.
In a typical project, though, we have many files—binary files with executable code or binary files
with other types of data (for example, images)—that are part of the software. Therefore, the
description, verification, and reinforcement of the relations and dependencies among them are made
in part by the metadata.
Metadata is partly responsible for making resources available to perform these tasks.
2. Open (as administrator) one of the developer command prompts installed and configured by
Microsoft Visual Studio 2019.
3. Copy the following sequence of MSIL code into the file RVJ.ProDotNETCustomLibs.il and save the
file:
.assembly RVJ.ProDotNETCustomLibs.Buffers {
.ver 1:0:0:0
}
4. In the developer command prompt, write the following command:
If the code compiles without error, the output will be a binary file with the name
RVJ.ProDotNETCustomLibs.dll.
By following these steps, we have created a single-file static assembly, with only the assembly
manifest.
ildasm RVJ.ProDotNETCustomLibs.dll
With the module RVJ.ProDotNETCustomLibs.dll loaded by the ILDasm.exe tool, we see the
screen shown in Figure 1-5.
Now double-click in the manifest. A new window will open with information about the assembly
manifest, as shown in Figure 1-6.
Figure 1-6 ILDASM showing the assembly manifest of a single-file static assembly
Implementing the entrypoint Method
We have created a single-file static assembly, with only the assembly manifest. If we want to create an
.EXE, we need to change the source code. Using the same RVJ.ProDotNETCustomLibs.il, update the
source code to include a managed method that is the entry point:
.ver 1:0:0:0
.entrypoint
ret
}
As you can see, the name of the .entrypoint method does not need to be main.
To build this code, use the following command:
After the code compiles without error and with the binary generated, we can use the ILDasm.exe
tool to load the module RVJ.ProDotNETCustomLibs.exe, and then we have more than just the
assembly manifest, as shown in Figure 1-7.
As shown in Figure 1-8, we have created a single-file static assembly, with the assembly manifest
and one method (in this case, the entry-point method). When RVJ.DotNETProCustomLibs.exe
runs, it runs like any other .NET managed executable.
Figure 1-8 ILDASM showing a single-file static assembly, with the assembly manifest and one managed method
Listing 1-5 shows an example of managed instructions from one of the sample projects that comes
with the companion content of this book. The .module directive indicates the name of the binary
module (in this case, RVJ.ProDotNETCustomLibs.exe). The .assembly directive describes
which assemblies make this a logical unit of management and deployment of resources designed to
work together. The .assembly RVJ.ProDotNETCustomLibs.Buffers (without the extern
keyword) describes that this assembly is in the current module. The use of .assembly extern
directive describes to the assembly the types that your .assembly or .module are referencing.
For example, .assembly extern System.Runtime indicates that the assembly
RVJ.ProDotNETCustomLibs.Buffers is using one or more types of the assembly mscorlib.
The highlighted CIL instructions are the same that you can read in the
RVJ.ProDotNETCustomLibs.dll or RVJ.ProDotNETCustomLibs.exe modules. Chapter 2
discusses these and other instructions in more detail (with even fuller detail following in subsequent
chapters).
.assembly RVJ.ProDotNETCustomLibs.Buffers
{
.ver 1:0:0:0
}
.module RVJ.ProDotNETCustomLibs.exe
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
.method public static void MyEntryPointMethod() cil managed {
.entrypoint
// Code size 1 (0x1)
.maxstack 8
IL_0000: ret
Discovering Diverse Content Through
Random Scribd Documents
— Teidän on kaiketi ikävä siten?
— Olen kyllä, mutta minä en pidä niistä… ne ovat niin ikäviä, niin
hirmuisen ikäviä! Ja he kirjottavat aina sellaisesta, jonka minä itsekin
tunnen, enkä huonommin heitä. He eivät osaa keksiä mitään
mieltäkiinnittävää, ja kaikki on heillä pelkkää totuutta.
— Ei suinkaan, mutta…
— Mistä hyvästä?
— Mutta eihän heillä ole edes leipää sen vertaa, että saisivat
syödä kylläkseen!
— Niin. Mutta jollei sisarenne ole itse siitä teille puhunut… niin
ehkei siitä tulekaan mitään. Ettehän puhu hänelle tästä?
— Kylläpä kävelittekin!
Hän oli tyytymätön itseensä ja nuhteli samalla itseään siitä, että oli
menettänyt kykynsä hillitä mielenliikutustaan ja että oli tänään
käyttäytynyt vakavalle miehelle sopimattomalla tavalla. Kahden
kesken itsensä kanssa hän oli aina ankarampi ja periaatteellisempi
itselleen kuin ihmisten seurassa. Ja nyt hän alkoi tutkiskella itseään.
— No, mitä sinä pidät Varjenkasta? — kysyi sisar, kun hän oli tullut
terassille.
— Kiitoksia.
— Minun on hauska.
— En tiedä.
— En lue runoja. Onko hän hyvä mies? Niin, tietysti hän on hyvä.
— En sano varmasti, että on, mutta sen minä voin sanoa itseäni
imartelematta, että hän voi korvata minun menneisyyteni… Hän
rakastaa minua… Minä olen vähitellen muodostanut itselleni pienen
elämänfilosofian… ehkä se näyttää sinusta hieman julmalta.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com