在什么情况下我会运行在C#中? [英] Under what context am I running in C#?

查看:129
本文介绍了在什么情况下我会运行在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道......

I was wondering...

当我有一个code是这样的:

When I have a code like this:

lock (obj)
{
    MyCode.MyAgent();
}

能否 MyAgent 包含code,它的确认的是下一个运行锁定块?

Can MyAgent contain code that recognizes it is running under a lock block?

什么:

for (int i=0;i<5;i++)
{
   MyCode.MyAgent();
}

能否 MyAgent 包含code,它的确认的是下一个循环运行块?

Can MyAgent contain code that recognizes it is running under a loop block ?

可以是同样的问题问使用块,不安全 code,等... - 还有你的想法......

The same question can be asked for using blocks, unsafe code , etc... - well you get the idea...

这是可能在C#中?

这仅仅是一个理论问题,我不是想达到什么...只是知识。

推荐答案

这不是完全不可能的,你可以使用堆栈跟踪类来获得一个参考给调用者的方法和MethodInfo.GetMethodBody()来获得该IL方法。

It isn't completely impossible, you can use the StackTrace class to get a reference to the caller method and MethodInfo.GetMethodBody() to get the IL of that method.

但你永远不会得到这一可靠的,刚刚即时编译器的优化会给你一个非常很难搞清楚究竟在何处调用的位置。法身体内联将使得该方法完全消失。循环展开将使其无法获得有关循环索引的想法。优化器使用CPU寄存器来存储局部变量和方法参数,因此无法得到可靠的变量值。

But you'll never get this reliable, the just-in-time compiler's optimizer will give you a very hard time figuring out exactly where the call is located. Method body inlining will make the method disappear completely. Loop unrolling will make it impossible to get any idea about the loop index. The optimizer uses cpu registers to store local variables and method arguments, making it impossible to get a reliable variable value.

且不说参与反编译IL锡纸咀嚼的努力。这一切都不是附近刚好路过一个参数传递给方法的简单性和速度的任何地方。

Not to mention the tin foil chewing effort involved in decompiling IL. None of this is anywhere near the simplicity and speed of just passing an argument to the method.

这篇关于在什么情况下我会运行在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆