Type and Member Basics(Chapter 6 of CLR via C#)
2011-08-04 23:35
423 查看
The CLR offers two IL instructions for calling a method: The call IL instruction can be used to call static, instance, and virtual methods. When the call instruction is used to call a static method, you must specify the type that defines the method that the CLR should call. When the call instruction is used to call an instance or virtual method, you must specify a variable that refers to an object. The call instruction assumes that this variable is not null. In other words, the type of the variable itself indicates which type defines the method that the CLR should call. If the variable’s type doesn’t define the method, base types are checked for a matching method. The call instruction is frequently used to call a virtual method nonvirtually. The callvirt IL instruction can be used to call instance and virtual methods, not static methods. When the callvirt instruction is used to call an instance or virtual method, you must specify a variable that refers to an object. When the callvirt IL instruction is used to call a nonvirtual instance method, the type of the variable indicates which type defines the method that the CLR should call. When the callvirt IL instruction is used to call a virtual instance method, the CLR discovers the actual type of the object being used to make the call and then calls the method polymorphically. In order to determine the type, the variable being used to make the call must not be null. In other words, when compiling this call, the JIT compiler generates code that verifies that the variable’s value is not null. If it is null, the callvirt instruction causes the CLR to throw a NullReferenceException. This additional check means that the callvirt IL instruction executes slightly more slowly than the call instruction. Note that this null check is performed even when the callvirt instruction is used to call a nonvirtual instance method.
相关文章推荐
- CLR Via C# 3rd 阅读摘要 -- Chapter 6 - Type and Member Basics
- Primitive, Reference, and Value Types(Chapter 5 of CLR via C#)
- Shared Assemblies and Strongly Named Assemblies(Chapter 3 of CLR via C#)
- Constants and Fields(Chapter 7 of CLR via C#)
- CLR.via.Csharp.3rd Chapter 6: Type and Member Basics
- Building, Packaging, Deploying, and Administering Applications and Types(Chapter 2 of CLR via C#)
- CLR Via C# 3rd 阅读摘要 -- Chapter 14 - Chars, Strings, and Working with Text
- CLR Via C# 3rd 阅读摘要 -- Chapter 20 – Exceptions and State Management
- CLR Via C# 3rd 阅读摘要 -- Chapter 5 - Primitive, Reference, and Value Types
- CLR Via C# 3rd 阅读摘要 -- Chapter 7 - Constants and Fileds
- CLR Via C# 3rd 阅读摘要 -- Chapter 3 - Shared Assemblies and Strongly Named Assemblies
- CLR Via C# 3rd 阅读摘要 -- Chapter 4 - Type Fundamentals
- CLR Via C# 3rd 阅读摘要 -- Chapter 15 - Enumerated Types and Bit Flags
- Type Fundamentals(Chapter 4 of CLR via C#)
- CLR Via C# 3rd 阅读摘要 -- Chapter 22 – CLR Hosting and AppDomains
- CLR Via C# 3rd 阅读摘要 -- Chapter 23 – Assembly Loading and Reflection
- CLR Via C# 3rd 阅读摘要 -- Chapter 25 – Thread Basics
- CLR Via C# 3rd 阅读摘要 -- Chapter 2 - Building, Packaging, Deploying, and Administering Applications and Types
- The CLR’s Execution Model(Chapter 1 of CLR via C#)
- CLR Via C# 3rd 阅读摘要 -- Chapter 19 – Nullable Value Types