On Tue, Jul 12, 2011 at 9:03 AM, torman <[email protected]> wrote:
> In the MethodReference "SPSecurity.RunWithElevatedPrivileges" I can
> find the
> type of the delegate (in my case "CodeToRunElevated") and I can find
> 4
> MethodDefinitions (Constructor, BeginInvoke, Invoke, EndInvoke).
> The "Invoke" call contains 2 Parameters: the callback (ASyncCallback)
> and
> an object.
>
> But there is no information, which can tell me, that the callback is
> pointing
> to method "<ContainingMethodName>b__0". The body of all 4 methods is
> null.
>
> Is there any extension in Mono.Cecil, which could help me to get the
> body
> of my delegate method? I believe, I'm not the first one with this
> problem :-)
I'm afraid you're confused :)
You're mixing the concept of a delegate as a glorified function
pointer at runtime, and it's static representation.
The delegate type does have an Invoke method, and it's implemented by
the runtime. Now, in the binary, you have a structure like:
class Foo {
void Bar ()
{
SPSecurity.RunWithElevatedPrivileges(delegate { Console.WriteLine
("BANG !"); })
}
}
If the signature of RunWithElevatedPrivileges if:
public static void RunWithElevatedPrivileges (CodeToRunElevated code);
with
public delegate void CodeToRunElevated();
The compiler will turn it into
class Foo {
static CodeToRunElevated generatedCode;
void Bar ()
{
if (generatedCode == null)
generateCode = new CodeToRunElevated(Foo_GeneratedMethod);
SPSecurity.RunWithElevatedPrivileges(generatedCode);
}
static void Foo_GeneratedMethod()
{
Console.WriteLine ("BANG !");
}
}
So, if you want to know what code is passed to
RunWithElevatedPrivileges, you have to analyze the stack where
RunWithElevatedPrivileges is actually called. And internally,
RunWithElevatedPriviliges will simply call Invoke on the delegate
instance.
In this case, you'll see that it's being passed a field, which is
initialized with the method Foo_GeneratedMethod. That's how you map
this kind of anonymous method. Now it can be a bit more tricky when
the anonymous delegate closes on variables or fields.
Jb
--
--
mono-cecil