接口的IL代码
我正在使用 LinqPad,因为我正在查看为以下界面和生成的 IL 代码(编译器优化打开)。实现该接口的类:
public interface IStockService
{
[OperationContract]
double GetPrice(string ticker);
}
public class StockService : IStockService
{
public double GetPrice(string ticker)
{
return 93.45;
}
}
IL 代码:
IStockService.GetPrice:
StockService.GetPrice:
IL_0000: ldc.r8 CD CC CC CC CC 5C 57 40
IL_0009: ret
StockService..ctor:
IL_0000: ldarg.0
IL_0001: call System.Object..ctor
IL_0006: ret
令人惊讶的是,我没有看到该接口的任何 IL 代码。这是因为 LinqPad 的工作方式还是 C# 编译器以不同的方式处理接口?
I am using LinqPad and in that I was looking at the IL code(compiler optimization switched on) generated for the following Interface & the class that implements the interface:
public interface IStockService
{
[OperationContract]
double GetPrice(string ticker);
}
public class StockService : IStockService
{
public double GetPrice(string ticker)
{
return 93.45;
}
}
IL Code :
IStockService.GetPrice:
StockService.GetPrice:
IL_0000: ldc.r8 CD CC CC CC CC 5C 57 40
IL_0009: ret
StockService..ctor:
IL_0000: ldarg.0
IL_0001: call System.Object..ctor
IL_0006: ret
Surprisingly I don't see any IL code for the interface. Is this because of the way LinqPad works or the C# compiler treats the interfaces in a different manner?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IL 用于方法体。接口不包含任何方法体,因此没有 IL。
在这种情况下(简单地说)方法体是任何可执行代码。接口中的任何内容都是可执行的,因为它只是一个契约。由接口的实现者来提供实现,该实现通常包含 IL。
IL is for method bodies. An interface does not contain any method bodies, thus no IL.
A method body in this case (put simply) is any executable code. Nothing in an interface is executable since it is just a contract. It's up to the implementor of the interface to provide an implementation, which will usually contain IL.