BACS 387 - University of Northern Colorado

Download Report

Transcript BACS 387 - University of Northern Colorado

.NET Archetecture Basics
Learn the components that make up the .NET
Framework.
 Learn the basic steps that take place when
compiling a C# program.
 Learn about the Common Intermediate Language
(CIL) that is generated by .NET compilers.
 Learn about some tools available that allow you to
use CIL code.

7/17/2015
OO Programming
2

.NET is the Microsoft method to ensure multiple
platform, multiple-language programs can be generated
from common source code.

All .NET languages are compiled into an intermediate
language called CIL (Common Intermediate Language).

The CIL, along with other program components, are
contained in a collection called an assembly.

The components in the assembly are compiled “just in
time” (JIT) into executable code by a special compiler
created for each platform architecture.
7/17/2015
OO Programming
3
7/17/2015
OO Programming
4
This step only happens when the code
unit is called to run. At run time, the
CLR:
 Checks the assembly’s security
characteristics
 Allocates space in memory
 Sends the code to the JIT compiler.
 The JIT (Just in Time) compiler
translates the CIL into native code at
runtime. This code is cached in case it is
needed again.
 “Native Code” is binary code
designed to run on a specific hardware
platform.
 A separate Common Language
Runtime (CLR) is needed for each
hardware platform.

7/17/2015
OO Programming
5
The core of the .NET Framework is the Common
Language Runtime (CLR).
 It works directly with the operating system and
manages program execution.
 It also provides the following important services:





Automatic garbage collection
Security and authentication
Access to an extensive library of pre-written components
through the Base Class Library (BCL)
The BCL is what you load onto your computer when
you install the .NET Framework.
7/17/2015
OO Programming
6
7/17/2015
OO Programming
7





Managed code share common data types (i.e.,
Common Type System – CTS).
This means that managed programs can all share
data.
They also share the Common Language
Specification (CLS) which specifies the rules,
properties, and behaviors of all .NET compliant
languages.
This includes data types, class construction, and
parameter passing rules.
Because of this consistency, Assemblies from multiple
.NET languages can be seamlessly merged.
7/17/2015
OO Programming
8
7/17/2015
OO Programming
9



Type – Base class that represents any type.
Value Type – Base class that represents any value type.
Reference Types – Any data types that are accessed through a reference and
stored on the heap.
 Includes most of the standard primitive types, which represent numbers, Boolean values, or
characters.










Built-in Value Types - Enumerations – Sets of enumerated values.
User-defined Value Types – Types that have been defined in source code and are
stored as value types. In C#, any struct.
Interface Types – Interfaces.
Pointer Types – Pointers.
Self-describing Types – Data types that provide information about themselves for
the benefit of the garbage collector.
Arrays – Any type that contains an array of objects.
Class Types – Types that are self-describing but not arrays.
Delegates – Types that are designed to hold references to methods.
User-defined Reference Types – Types that have been defined in source code and
are stored as reference types. In C#, any class.
Boxed Value Types – A value type that is temporarily wrapped in a reference so
that it can be stored on the heap.
7/17/2015
OO Programming
10
By using the CTS (Common Type System) and CIL
(Common Intermediate Language), programs can
be written in any language that follows the .NET
specifications.
 Compilation to CIL can occur at any time on any
platform.
 JIT (Just In Time) runtime compilation converts the
generic CIL into machine specific native code.
 In this way, you have the ability to mix languages on
any platform that supports the .NET specifications.

7/17/2015
OO Programming
11
7/17/2015
OO Programming
12
The power of the .NET framework is that you are
not tied to any specific platform or any specific
language.
 Web-sites exist that can provide you with
information on the various .NET languages that are
currently available. Many are free.
 Some good web-sites are:
http://dotnetlanguages.net/DNL/Resources.aspx
http://www.dotnetpowered.com/languages.aspx
http://en.csharp-online.net/.NET_Languages

7/17/2015
OO Programming
13
As was stated earlier, CIL is an intermediate code
that all .NET source programs are translated to prior
to execution.
 Tools exist in Visual Studio that allow you to
examine the CIL code.
 While this is considered an advanced skill, it is useful
to know how to access this code.
 This skill can be used to debug complex problems or
to tune code to be more efficient.

7/17/2015
OO Programming
14
The Microsoft disassembler is found by going to the
Start menu and selecting the “Microsoft Windows
SDK”, followed by “Tools”, followed by “IL
Disassembler.”
 From this program, you can open .exe and .dll files
to see the CIL code that is used when the program
runs.
 While you cannot change this code directly via this
tool, it is very instructive to examine it for execution
and efficiency information.

7/17/2015
OO Programming
15


VB Source Code:
Module Module1
Sub Main()
Dim X As Integer = 10
Dim Y As Integer = 20
Dim Z As Integer = X * Y
Console.WriteLine("Answer is:" _
& "{0}", Z)
Console.ReadLine()
End Sub
End Module
7/17/2015
CIL Code Generated
.locals init ([0] int32 X,
[1] int32 Y,
[2] int32 Z)
IL_0000: nop
IL_0001: ldc.i4.s 10
IL_0003: stloc.0
IL_0004: ldc.i4.s 20
IL_0006: stloc.1
IL_0007: ldloc.0
IL_0008: ldloc.1
IL_0009: mul.ovf
IL_000a: stloc.2
IL_000b: ldstr “Answer is: {0}"
IL_0010: ldloc.2
IL_0011: box
[mscorlib]System.Int32
IL_0016: call
void
[mscorlib]System.Console::WriteLine(string, object)
IL_001b: nop
IL_001c: call
string
[mscorlib]System.Console::ReadLine()
IL_0021: pop
IL_0022: nop
IL_0023: ret
OO Programming
16

C# Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int X = 10;
int Y = 20;
int Z = X * Y;
Console.WriteLine("Answer is: {0}", Z);
Console.ReadLine();
}
}
}
7/17/2015

CIL Code Generated
.locals init ([0] int32 X,
[1] int32 Y,
[2] int32 Z)
IL_0000: nop
IL_0001: ldc.i4.s 10
IL_0003: stloc.0
IL_0004: ldc.i4.s 20
IL_0006: stloc.1
IL_0007: ldloc.0
IL_0008: ldloc.1
IL_0009: mul
IL_000a: stloc.2
IL_000b: ldstr "Answer is: {0}"
IL_0010: ldloc.2
IL_0011: box
[mscorlib]System.Int32
IL_0016: call
void
[mscorlib]System.Console::WriteLine(string, object)
IL_001b: nop
IL_001c: call
string
[mscorlib]System.Console::ReadLine()
IL_0021: pop
IL_0022: ret
OO Programming
17

VB CIL Code

.locals init ([0] int32 X,
[1] int32 Y,
[2] int32 Z)
IL_0000: nop
IL_0001: ldc.i4.s 10
IL_0003: stloc.0
IL_0004: ldc.i4.s 20
IL_0006: stloc.1
IL_0007: ldloc.0
IL_0008: ldloc.1
IL_0009: mul.ovf
IL_000a: stloc.2
IL_000b: ldstr “Answer is: {0}"
IL_0010: ldloc.2
IL_0011: box
[mscorlib]System.Int32
IL_0016: call
void
[mscorlib]System.Console::WriteLine(string, object)
IL_001b: nop
IL_001c: call
string
[mscorlib]System.Console::ReadLine()
IL_0021: pop
IL_0022: nop
IL_0023: ret
7/17/2015
C# CIL Code
.locals init ([0] int32 X,
[1] int32 Y,
[2] int32 Z)
IL_0000: nop
IL_0001: ldc.i4.s 10
IL_0003: stloc.0
IL_0004: ldc.i4.s 20
IL_0006: stloc.1
IL_0007: ldloc.0
IL_0008: ldloc.1
IL_0009: mul
IL_000a: stloc.2
IL_000b: ldstr "Answer is: {0}"
IL_0010: ldloc.2
IL_0011: box
[mscorlib]System.Int32
IL_0016: call
void
[mscorlib]System.Console::WriteLine(string, object)
IL_001b: nop
IL_001c: call
string
[mscorlib]System.Console::ReadLine()
IL_0021: pop
IL_0022: ret
OO Programming
18

For Loop Test

using System;
namespace ConsoleForTest
{
class Program
{
static void Main()
{
int[] myarray = { 1, 2, 3, 4, 5 };
for(int i = 0; i < myarray.Length; i++)
{
Console.WriteLine("Value is {0}",
Convert.ToString(myarray[i]));
}
}
}
}
7/17/2015
ForEach Loop Test
using System;
namespace ConsoleForEachTest
{
class Program
{
static void Main()
{
int[] myarray = { 1, 2, 3, 4, 5 };
foreach (int val in myarray)
{
Console.WriteLine("Value is
{0}", Convert.ToString(val));
}
}
}
}
OO Programming
19
For Test
ForEach Test
.method private hidebysig static void Main() cil managed
{
.entrypoint
// Code size
59 (0x3b)
.maxstack 3
.locals init ([0] int32[] myarray,
[1] int32 i,
[2] bool CS$4$0000)
IL_0000: nop
IL_0001: ldc.i4.5
IL_0002: newarr
[mscorlib]System.Int32
IL_0007: dup
IL_0012: stloc.0
IL_0013: ldc.i4.0
IL_0014: stloc.1
IL_0015: br.s
IL_0030
IL_0017: nop
IL_0018: ldstr
"Value is {0}"
IL_001d: ldloc.0
IL_001e: ldloc.1
IL_001f: ldelem.i4
IL_0020: call
string [mscorlib]System.Convert::ToString(int32)
IL_0025: call
void [mscorlib]System.Console::WriteLine(string,
object)
IL_002a: nop
IL_002b: nop
IL_002c: ldloc.1
IL_002d: ldc.i4.1
IL_002e: add
IL_002f: stloc.1
IL_0030: ldloc.1
IL_0031: ldloc.0
IL_0032: ldlen
IL_0033: conv.i4
IL_0034: clt
IL_0036: stloc.2
IL_0037: ldloc.2
IL_0038: brtrue.s
IL_0017
IL_003a: ret
} // end of method Program::Main
.method private hidebysig static void Main() cil managed
{
.entrypoint
// Code size
66 (0x42)
.maxstack 3
.locals init ([0] int32[] myarray,
[1] int32 val,
[2] int32[] CS$6$0000,
[3] int32 CS$7$0001,
[4] bool CS$4$0002)
IL_0000: nop
IL_0001: ldc.i4.5
IL_0002: newarr
[mscorlib]System.Int32
IL_0007: dup
IL_0012: stloc.0
IL_0013: nop
IL_0014: ldloc.0
IL_0015: stloc.2
IL_0016: ldc.i4.0
IL_0017: stloc.3
IL_0018: br.s
IL_0035
IL_001a: ldloc.2
IL_001b: ldloc.3
IL_001c: ldelem.i4
IL_001d: stloc.1
IL_001e: nop
IL_001f: ldstr
"Value is {0}"
IL_0024: ldloc.1
IL_0025: call
string [mscorlib]System.Convert::ToString(int32)
IL_002a: call
void [mscorlib]System.Console::WriteLine(string,
object)
IL_002f: nop
IL_0030: nop
IL_0031: ldloc.3
IL_0032: ldc.i4.1
IL_0033: add
IL_0034: stloc.3
IL_0035: ldloc.3
IL_0036: ldloc.2
IL_0037: ldlen
IL_0038: conv.i4
IL_0039: clt
IL_003b: stloc.s
CS$4$0002
IL_003d: ldloc.s
CS$4$0002
IL_003f: brtrue.s
IL_001a
IL_0041: ret
} // end of method Program::Main
7/17/2015
OO Programming
20

Microsoft developed and sells several .NET
languages. They also provide (for free) the .NET
Framework and several IDE tools.
Others have developed useful tools you may want to
look at:
.NET Editors:

.NET Environments:

Other Tools:


 Notepad++ http://notepad-plus.sourceforge.net
 SharpDevelop http://sourceforge.net/projects/sharpdevelop/
 mono .NET project http://www.mono-project.com
 gnu .NET project http://www.dotgnu.org
 Lutz reflector http://www.red-gate.com/products/reflector/
 FxCop http://code.msdn.microsoft.com/default.aspx?SiteEntry=gdn
 nDoc http://sourceforge.net/projects/ndoc
7/17/2015
OO Programming
21
The .NET Framework is composed of several important
components including: the Common Language Runtime
(CLR), Common Type System (CTS), Base Class Library
(BCL), and Just In Time (JIT) compilation.
 .NET programs follow a logical, predictable set of steps
toward compilation.
 Common Intermediate Language (CIL) is the method
that .NET uses to ensure language and platform
independence.
 Several tools exist (and are free) to help you develop
and debug .NET projects.

7/17/2015
OO Programming
22