如何访问 CIL (MSIL) 中的对象属性?
你看,我绝对是个初学者。 假设我在堆栈上有一个字符串对象,并且想要获取其中的字符数 - 它的 .Length 属性。 我怎样才能得到隐藏在里面的int32数字?
提前谢谢了!
I'm an absolute beginner, you see. Say I have a string object on the stack and want to get the number of characters in it - its .Length property. How would I get the int32 number hidden inside?
Many thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
伊利诺伊州确实没有房产这样的东西。 只有字段和方法。 C# 属性构造由编译器转换为
get_PropertyName
和set_PropertyName
方法,因此您必须调用这些方法来访问属性。代码 IL 的示例(调试)IL
正如
您所看到的,通过调用
get_Length
来访问 Length 属性。There's really no such thing as properties in IL. There are only fields and methods. The C# property construct is translated to
get_PropertyName
andset_PropertyName
methods by the compiler, so you have to call these methods to access the property.Sample (debug) IL for code
IL
As you can see the Length property is accessed via the call to
get_Length
.我作弊了...我拿了以下 C# 代码并在 ildasm/Reflector 中查看了它
相当于
I cheated ... I took the following C# code and took a look at it in ildasm/Reflector
is equivalent to