28
Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Embed Size (px)

Citation preview

Page 1: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Chapter 1

IDE and Tools for

Developing CLR-based Programs

Yingcai Xiao

Page 2: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Integrated Development Environment (IDE)

(1) IDE: all-in-one programming tools (2) Includes: editor, compiler, linker, loader, debugger, profiler, project organizer, context-sensitive help, form designer, programming code libraries, application framework templates, …

(3) IDE you may have used: Eclipse, jGRASP, NetBeans, BlueJ

(4) Microsoft IDEs:

MS Visual Studio 6.0: last non .NET version.

MS Visual Studio 7.0 => MS Visual Studio .NET

MS Visual Studio 8.0 => MS Visual Studio .NET 2005

MS Visual Studio .NET 2010Start->All Programs->Microsoft Visual Studio 2010->

Microsoft Visual Studio 2010 DocumentationLearn how to use VS 2010 there.

Page 3: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Create your first C# program

Setup:• If you are on a CS computer, refer to the following link to setup

the Z drive. http://www.cs.uakron.edu/%7Exiao/lab-use.html

• Use C drive if you have problem mount the Z drive. Remember to copy your code to a flash drive and delete what’s on the C drive before logging out from the CS computer.

• Use C drive if you are on a home computer.

• In the following examples, replace drive letter Z with C if you are using the C drive.

Page 4: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Create your first C# program using Visual Studio .NET

(1) Create a projectStart->All Programs->Microsoft Visual Studio 2010-> Microsoft Visual Studio 2010Start Page -> New ProjectsVisual C#->Windows->Console ApplicationName: HelloSolution name: Hello

Location: Browse Folders->Computer->Z->WP (New Folder) -> Select Folder(Default on winserv1: C:\users\xiaotest\documents\visual studio 2010\Projects)OK

(2) Code System.Console.WriteLine ("Hello, world!");

System.Console.WriteLine ("Hit any key to end.");System.Console.Read();

(3) RunF5 (Debug->Start Debugging)Z:\WP\Hello\bin\Debug\bin

Page 5: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

VS Project Directory

z:\WP\Hello ( the solution directory)Hello.sln (solution info)Hello (project directory, there can be more than one project)

z:\WP\Hello->Hello ( the project directory)Hello.csproj (C# project info)Program.cs (the C# source file)bin (directory for the executables to be delivered)obj (directory for the object files, working directory)Properties (AssemblyInfo.cs)

z:\WP\Hello->Hello->bin (directory for the executables to be delivered)Release->Hello.exe (the non-debugable executable, smaller)Debug->Hello.exe (the debugable executable, larger)Debug->vshost.exe (VS hosting process for debugging)

Page 6: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Create the same program using Notepad

(1) Edit (create the source code using Notepad)Start->All Programs->Microsoft Visual Studio 2010->Visual Studio

Tools->Microsoft Visual Studio Command Prompt (2010)Z:mkdir WPcd WP mkdir Hello2cd Hello2notepad Hello.cs & (background process)Copy code from the next page.

(2) Compile (using the C-Sharp Compiler)csc /target:exe /out:Hello.exe Hello.cs(csc Hello.cs)

(3)Excute Hello.exe

Page 7: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Create the same program using Notepad

namespace Xiao{

class MyApp{ static void Main () { System.Console.WriteLine ("Hello, world"); System.Console.WriteLine ("Hit any key to end.");

System.Console.Read(); // Don’t call “exit”. Why?

}}

}

Page 8: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Inside .NET Programs: FCL & CLR

FCL (.NET framework class library):

object-oriented API for writing managed applications

more than 7,000 classes in named spaces:

e.g. System.Windows.Forms, System.IO

stored as DLLs (Dynamically Linked Libraries).

http://msdn.microsoft.com/en-us/library/w0x726c2(v=vs.110).aspx

CLR (common language runtime): execution engine for managed applications.

Page 9: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

.NET Architecture for Language and Platform Independence(fan-in and fan-out on MSIL)

Source Code for Language 1

Language 1 Compiler on OS1

OS1

CLR on OS1

OS2

Language 2 Compiler on OS2

Binary Code for OS2Binary Code for OS1

CLR on OS2

Source Code for Language 2

MSIL Code Confirming CTS (Managed Code)

Page 10: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Managed and Unmanaged Code

Managed Code: application code whose every action is subject to approval by the CLR (common language runtime) and its compiler conforms to the CLS (common language specification) and supports the CTS (common type system).

Unmanaged Code: native machine code that runs without CLR.

Advantages of running managed code through CLR:Type safe (type checked by CLR at runtime)Light weight (multiple applications per process)Garbage collection

Page 11: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

An Example Assembly

Page 12: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Contents of a managed module:• CIL instructions generated from the source code• Metadata describing code inside the module• A CLR header containing important information about the module• A Windows Portable Executable (PE) file header

Metadata: a collection of tables that describe the code.• TypeDef• Class Names

Assembly: is a collection of one or more files (modules) grouped together to form a logical unit.

Manifest: metadata for an assembly.Name, version, data types exported.

AL (Assembly Linker): joins files into assemblies.

Module/Metadata-Assembly/Manifest

Page 13: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Reflection: runtime understanding of the code (data types, program organizations, …)

Reflection APIs: programming tools in System.Reflection namespace to read metadata.

ILDASM: a program in .NET SDK to view the metadata using the reflection APIs

DIA SDK: Debug Interface Access SDK The Microsoft Debug Interface Access Software Development Kit (DIA SDK) providesaccess to debug information stored in program database (.pdb) files(http://msdn.microsoft.com/library/en-us/diasdk/html/vsoriDebugInterfaceAccessSDK.asp)

Reflection

Page 14: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

CIL

CIL: Common Intermediate Language (e.g. MSIL)Other languages are merely syntactic devices for producing CIL.

Microsoft provides CIL compilers for five languages: C#, J#, C++, Visual Basic, and JScript.

Other companies provide: Perl, Python, Eiffel, and, even COBOL.

Managed applications are Applications that target the .NET framework, written in one of the high-level languages, and translated into CIL code (managed code) by the compilers.

Page 15: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

CIL cont.

CIL compilers: translate code from other languages into CIL code.

CIL is like a pseudo-assembly language that defines a native instruction set for the CLR (a virtual processor), there are

about 100 instructions: ADD, BEQ (branch if equal), RET, BOX.…

Page 16: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

The code translated into CIL

ldc.i4.3 // Load a 32-bit (i4) 3 onto the stackstloc.0 // Store it in local variable 0 (a)ldc.i4.7 // Load a 32-bit (i4) 7 onto the stackstloc.1 // Store it in local variable 1 (b)ldloc.0 // Load local variable 0 onto the stackldloc.1 // Load local variable 1 onto the stackadd // Add the two and leave the sum on the stackstloc.2 // Store the sum in local variable 2 (c)

int a = 3; int b = 7; int c = a + b;

Each file is compiled into a managed module that can be run by CLR.

Page 17: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Using ILDASM

In the VS Command Prompt window, cd z:\WP\Hello2, ildasm Hello.exe

.method private hidebysig static void Main() cil managed{ .entrypoint // Code size 30 (0x1e) .maxstack 8 IL_0000: nop IL_0001: ldstr "Hello, world" IL_0006: call void [mscorlib]System.Console::WriteLine(string) IL_000b: nop IL_000c: ldstr "Hit any key to end." IL_0011: call void [mscorlib]System.Console::WriteLine(string) IL_0016: nop IL_0017: call int32 [mscorlib]System.Console::Read() IL_001c: pop IL_001d: ret} // end of method MyApp::Main

Page 18: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Understanding CIL

Examples in CS, V++ and VB at T:Xiao\Windows Programming\Examples\C1\Simplehttp://www.cs.uakron.edu/~xiao/windows/Examples.zip

Check out the source code and the “executables.” Appreciate the statement: “Other languages are merely syntactic devices for producing CIL.”

Page 19: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Understanding CIL: CSCA.cs

class Compute {

public int Factorial(int f){int i;int result = 1;for (i=2; i <=f; i++)result = result * i;return result;}

}

Page 20: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Understanding CIL: CSCA.cs

class ComputeFactorial{ static void Main(string[] args) { Compute c = new Compute(); int v = 5; System.Console.WriteLine("{0} factorial: {1}", v, c.Factorial(v)); System.Console.ReadLine(); }}

Page 21: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Understanding CIL: CSCA.exe

.method public hidebysig instance int32 . . .Factorial(int32 f) cil managed{ // Code size 24 (0x18) .maxstack 2 .locals init (int32 V_0, int32 V_1, int32 V_2) IL_0000: ldc.i4.1 IL_0001: stloc.1 IL_0002: ldc.i4.2 IL_0003: stloc.0 IL_0004: br.s IL_000e IL_0006: ldloc.1 IL_0007: ldloc.0 IL_0008: mul

IL_0009: stloc.1 IL_000a: ldloc.0 IL_000b: ldc.i4.1 IL_000c: add IL_000d: stloc.0 IL_000e: ldloc.0 IL_000f: ldarg.1 IL_0010: ble.s IL_0006 IL_0012: ldloc.1 IL_0013: stloc.2 IL_0014: br.s IL_0016 IL_0016: ldloc.2 IL_0017: ret} // end of method Compute::Factorial

Page 22: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Understanding CIL: CSCA.exe

.method private hidebysig static void Main(string[] args) cil managed{ .entrypoint // Code size 43 (0x2b) .maxstack 4 .locals init (class Compute V_0, int32 V_1) IL_0000: newobj instance void Compute::.ctor() IL_0005: stloc.0 IL_0006: ldc.i4.5 IL_0007: stloc.1 IL_0008: ldstr "{0} factorial: {1}" IL_000d: ldloc.1 IL_000e: box [mscorlib]System.Int32 IL_0013: ldloc.0 IL_0014: ldloc.1

IL_0015: callvirt instance int32 Compute::Factorial(int32) IL_001a: box [mscorlib]System.Int32 IL_001f: call void [mscorlib]System.Console::WriteLine(string, object, object) IL_0024: call string [mscorlib]System.Console::ReadLine() IL_0029: pop IL_002a: ret} // end of method ComputeFactorial::Main

Page 23: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Understanding CIL: VBCA.vb

Module Module1

Class Compute Function Factorial(ByVal F As Integer) As Integer Dim I As Integer Dim Result As Integer = 1 For I = 2 To F Result = Result * I Next Return Result End Function End Class

Page 24: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Understanding CIL: VBCA.vb

Sub Main() Dim C As Compute = New Compute() Dim V As Integer = 5 System.Console.WriteLine("{0} factorial: {1}", V, C.Factorial(V)) System.Console.ReadLine() End Sub

End Module

Page 25: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Understanding CIL: VBCA.exe

. .method public instance int32 Factorial(int32 F) cil managed{ // Code size 28 (0x1c) .maxstack 2 .locals init (int32 V_0, int32 V_1, int32 V_2, int32 V_3) IL_0000: nop IL_0001: ldc.i4.1 IL_0002: stloc.2 IL_0003: ldc.i4.2 IL_0004: ldarg.1 IL_0005: stloc.3 IL_0006: stloc.1 IL_0007: br.s IL_0012 IL_0009: ldloc.2 IL_000a: ldloc.1

IL_000b: mul.ovf IL_000c: stloc.2 IL_000d: nop IL_000e: ldloc.1 IL_000f: ldc.i4.1 IL_0010: add.ovf IL_0011: stloc.1 IL_0012: ldloc.1 IL_0013: ldloc.3 IL_0014: ble.s IL_0009 IL_0016: ldloc.2 IL_0017: stloc.0 IL_0018: br.s IL_001a IL_001a: ldloc.0 IL_001b: ret} // end of method Compute::Factorial

Page 26: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Understanding CIL: VBCA.exe

.method public static void Main() cil managed{ .entrypoint .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) // Code size 46 (0x2e) .maxstack 4 .locals init (class VBCA.Module1/Compute V_0, int32 V_1) IL_0000: nop IL_0001: newobj instance void VBCA.Module1/Compute::.ctor() IL_0006: stloc.0 IL_0007: ldc.i4.5 IL_0008: stloc.1 IL_0009: ldstr "{0} factorial: {1}"

IL_000e: ldloc.1 IL_000f: box [mscorlib]System.Int32 IL_0014: ldloc.0 IL_0015: ldloc.1 IL_0016: callvirt instance int32 VBCA.Module1/Compute::Factorial(int32) IL_001b: box [mscorlib]System.Int32 IL_0020: call void [mscorlib]System.Console::WriteLine(string, object, object) IL_0025: nop IL_0026: call string [mscorlib]System.Console::ReadLine() IL_002b: pop IL_002c: nop IL_002d: ret} // end of method Module1::Main

Page 27: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Three Components of CLR

(1) A just-in-time (JIT) compiler which converts CIL code into native binary machine code.

(2) A CTS: Common Type System which defines data types supported by .NET.

(3) A CLS: Common Language Specification which defines the language rules supported by .NET.

Page 28: Chapter 1 IDE and Tools for Developing CLR-based Programs Yingcai Xiao

Chapter Summary

(1) .NET Framework is a platform for CLR applications (Windows, Web, anywhere CLR is supported)

(2) Applications that target the .NET Framework are managed applications.

(3) They’re (a) made of CIL and metadata, (b) JIT compiled at run

time, and (c) executed by the CLR.

(4) Languages such as C# .NET are syntactic tools for generating CIL.

(5) IDEs are all-in-one software development tools.