The CLR’s Execution Model

来源:互联网 发布:linux head命令 编辑:程序博客网 时间:2024/05/22 03:34

1.Depending on the platform switch, the C# compiler will emit an

assembly that contains either
a PE32 or PE32+ header (运行32位/64位), and the compiler will also emit

the desired CPU architecture (or
8 Part I CLR Basics
agnostic) into the header as well.
Microsoft ships two SDK command-line utilities, DumpBin.
exe and CorFlags.exe(设置Assembly以32 bit还是64 bit运行), that you can

use to examine the header information emitted in a managed module by the

compiler.一般情况下,Windows会根据Header信息来决定以什么样的方式来运行

Assembly.

2.JITCompiler is responsible for compiling a method’s IL code into

native CPU instructions.基于Stack方式来Execution.Verification 机制确保

Code是Safe。

The NGen.exe tool that ships with the .NET Framework can be used to

compile IL code to
native code when an application is installed on a user’s machine.


Struct应该确保每个值类型都有初始化
Because C# doesn’t allow value types with parameterless constructors,

compiling the following
type produces the following message: "error CS0573: 'SomeValType.m_x':

cannot
have instance field initializers in structs."
internal struct SomeValType {
// You cannot do inline instance field initialization in a value type
private Int32 m_x = 5;
}

internal struct SomeValType {
private Int32 m_x, m_y;
// C# allows value types to have constructors that take parameters.
public SomeValType(Int32 x) {
m_x = x;
// Notice that m_y is not initialized here.
}
}
When compiling this type, the C# compiler produces the following

message: "error
CS0171: Field 'SomeValType.m_y' must be fully assigned before control

leaves
the constructor."

解决方法:构造函数里面调用一个new();
// C# allows value types to have constructors that take parameters.
public SomeValType(Int32 x) {
// Looks strange but compiles fine and initializes all fields to 0/null
this = new SomeValType();
m_x = x; // Overwrite m_x’s 0 with x
// Notice that m_y was initialized to 0.
}


//经过试验,以下言论被证明错误!!!!!!
永远不要在值类型里写一个static constructor,因为CLR永远不会调用值类型的

静态构造函数!!!!
Important While you can define a type constructor within a value type,

you should never actually
do this because there are times when the CLR will not call a value type’

s static type constructor.
Here is an example:
internal struct SomeValType {
static SomeValType() {
Console.WriteLine("This never gets displayed");
}
public Int32 m_x;
}
public sealed class Program {
public static void Main() {
SomeValType[] a = new SomeValType[10];
a[0].m_x = 123;
Console.WriteLine(a[0].m_x); // Displays 123
}
}


Extension Method(不包括属性,事件,操作符,等等)对已有类型增加新的方法亮

点:1,Static Class 2.Static Method. 3参数前面加this关键字
public static class Test
    {
        public static int IndexOf(this StringBuilder sb, char value)
        {
            return 0;
        }
    }


Having immutable strings also means that there are no thread

synchronization issues when
manipulating or accessing a string. In addition, it’s possible for the

CLR to share multiple
identical String contents through a single String object.

For performance reasons, the String type is tightly integrated with the

CLR. Specifically, the
CLR knows the exact layout of the fields defined within the String type,

and the CLR accesses
these fields directly. This performance and direct access come at a

small development cost:
the String class is sealed, which means that you cannot use it as a base

class for your own
type.


AppDomains
When the CLR COM server initializes, it creates an AppDomain. An

AppDomain is a logical
container for a set of assemblies. The first AppDomain created when the

CLR is initialized is
called the default AppDomain; this AppDomain is destroyed only when the

Windows process
terminates.
1.Objects created by code in one AppDomain cannot be accessed directly

by code in
another AppDomain
2.AppDomains can be unloaded The CLR doesn’t support the ability to

unload a single
assembly from an AppDomain.
3.AppDomains can be individually secured
4.AppDomains can be individually configured

0 0