CLR call Virtual Methods
来源:互联网 发布:java源程序的扩展名是 编辑:程序博客网 时间:2024/04/30 11:05
CLR via C# has following rules:
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.
Take a look at following two examples:
Example1.
class BaseClass
{
internal void TestMethod1()
{
Console.WriteLine("BaseMethod1");
TestMethod2();
}
internal virtual void TestMethod2()
{
Console.WriteLine("BaseMethod2");
}
}
class SubClass : BaseClass
{
internal new void TestMethod1()
{
Console.WriteLine("SubMethod1");
TestMethod2();
base.TestMethod1();
}
internal override void TestMethod2()
{
Console.WriteLine("SubMethod2");
}
}
static void Main(string[] args)
{
SubClass sub = new SubClass ();
sub.TestMethod1();
}
What's the output on console?
SubMethod1
SubMethod2
BaseMethod1
SubMethod2
Example2:
class BaseClass
{
internal void TestMethod1()
{
Console.WriteLine("BaseMethod1");
TestMethod2();
}
internal virtual void TestMethod2()
{
Console.WriteLine("BaseMethod2");
}
}
class SubClass : BaseClass
{
internal new void TestMethod1()
{
Console.WriteLine("SubMethod1");
TestMethod2();
base.TestMethod1();
}
internal override void TestMethod2()
{
Console.WriteLine("SubMethod2");
}
}
class SubSubClass : SubClass
{
internal new void TestMethod1()
{
Console.WriteLine("SubSubMethod1");
TestMethod2();
base.TestMethod1();
}
internal override void TestMethod2()
{
Console.WriteLine("SubSubMethod2");
}
}
static void Main(string[] args)
{
SubSubClass sub = new SubSubClass();
sub.TestMethod1();
}
What's the output on console?
SubSubMethod1
SubSubMethod2
SubMethod1
SubSubMethod2
BaseMethod1
SubSubMethod2
- CLR call Virtual Methods
- 17.5.3 Virtual methods
- 17.5.3 Virtual methods
- virtual methods in tolua++
- mock non-virtual methods
- call Java methods from C++
- Meteor methods call apply 使用
- django view call model methods
- pure virtual function call
- Java 多态 (polymorphism) Virtual Methods
- R6025 - pure virtual function call
- R6025 pure virtual function call
- C++ pure virtual function call
- R6025 pure virtual function call
- JNI - Call Method(Callback Call java methods from c)
- Get CLR call stack in dump analysis
- Issues caused by virtual function call or function pointer call
- Pure virtual function call error探讨
- WT库学习2篇 新手上路(简单学习以及部署到IIS上)
- 编译CODEBLOCKS最新版本遇到问题搜集
- NERDTree 使用
- 2012给自己的信
- 又回到那个IDE的问题
- CLR call Virtual Methods
- linux下ImageMagick,Jmagick安装说明(linux64)
- ImageMagick安装配置 for java项目 (使用jmagick)
- 编程要从大体入手从细节结尾
- 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
- 框架和平台的区别以及重要性
- 关于android的一些感想
- 正则习点 --- 08
- make clean、make mrproer 以及make distclean的区别