IL 约束调用
IL constrained call
对于此代码:
class Program
{
static void Main()
{
Console.WriteLine(new MyStruct().ToString());
}
struct MyStruct { }
}
C# 编译器生成 constrained callvirt
IL 代码。
This文章说:
For example, if a value type V overrides the Object.ToString() method, a call V.ToString() instruction is emitted; if it does not, a box instruction and a callvirt Object.ToString() instruction are emitted. A versioning problem can arise <...> if an override is later added.
所以,我的问题是:如果编译器将生成 box
代码而不是约束调用,为什么在这种情况下会出现问题?
box
指令创建了相关实例的副本。允许值类型的实例方法修改调用它们的实例,如果这样做,则在副本上静默调用方法是错误的做法。
static class Program
{
static void Main()
{
var myStruct = new MyStruct();
Console.WriteLine(myStruct.i); // prints 0
Console.WriteLine(myStruct.ToString()); // modifies myStruct, not a copy of myStruct
Console.WriteLine(myStruct.i); // prints 1
}
struct MyStruct {
public int i;
public override string ToString() {
i = 1;
return base.ToString();
}
}
}
对于此代码:
class Program
{
static void Main()
{
Console.WriteLine(new MyStruct().ToString());
}
struct MyStruct { }
}
C# 编译器生成 constrained callvirt
IL 代码。
This文章说:
For example, if a value type V overrides the Object.ToString() method, a call V.ToString() instruction is emitted; if it does not, a box instruction and a callvirt Object.ToString() instruction are emitted. A versioning problem can arise <...> if an override is later added.
所以,我的问题是:如果编译器将生成 box
代码而不是约束调用,为什么在这种情况下会出现问题?
box
指令创建了相关实例的副本。允许值类型的实例方法修改调用它们的实例,如果这样做,则在副本上静默调用方法是错误的做法。
static class Program
{
static void Main()
{
var myStruct = new MyStruct();
Console.WriteLine(myStruct.i); // prints 0
Console.WriteLine(myStruct.ToString()); // modifies myStruct, not a copy of myStruct
Console.WriteLine(myStruct.i); // prints 1
}
struct MyStruct {
public int i;
public override string ToString() {
i = 1;
return base.ToString();
}
}
}