Reputation: 156
I'm debugging a large application that I didn't write, that runs on Windows but hangs on Linux+MONO. The application consists of a C# Core that contains a homebrew ~700-line ScriptCompiler class that compiles more C# scripts at runtime.
After compiling all the "scripts", the ScriptCompiler goes through all the assemblies and looks for Configure() methods to invoke. One of those Configure methods is hanging when run on a Linux system using Mono. Here's a relevant snippet:
for (int i = 0; i < types.Length; ++i)
{
MethodInfo m = types[i].GetMethod(method, BindingFlags.Static | BindingFlags.Public);
if (m != null)
{
invoke.Add(m);
}
}
followed shortly by
invoke.Sort(new CallPriorityComparer());
for (int i = 0; i < invoke.Count; ++i)
{
invoke[i].Invoke(null, null);
}
This is a huge application so I can't just go making changes willy-nilly. What I'd like to do is insert a line into that loop that spits out the name (or type, I guess) of the object whose Configure method is currently being called to stdout. Something like:
invoke[i].ParentObject.GetType();
(obviously that's not valid but hopefully that gets my point across).
Is this even possible?
Upvotes: 0
Views: 139
Reputation: 25221
You can get the name of the type in which the method referred to by invoke[i]
is declared with:
invoke[i].DeclaringType.Name
You can then log this out as you see fit.
As noted by CSharpie, this is different from the ReflectedType
- if the Configure
method is derived from a base class, DeclaringType
will give you the base class, not necessarily the actual runtime type that was reflected.
As the Configure
method is likely implemented explicitly by each of the types you're iterating over (and you are doing this for debugging purposes anyway), it likely won't make a great deal of difference which one you use in your case.
DeclaringType
may be somewhat more useful to you, though, as it will tell you precisely where you can find the implementation (e.g. if you reflect a class A : B
but the Configure
method is implemented in B
, DeclaringType
will be B
while ReflectedType
will be A
).
Upvotes: 2
Reputation: 9467
You are looking for the ReflectedType-Property if i dont get you wrong. (You wanted the Type of the "ParentObject" and by this i guess you dont care if the method is from a derived baseclass)
invoke[i].ReflectedType
As mentioned, there is a difference to DeclaringType though. this will give you the type that really does implement the method. This could be some baseclass. So make sure to check which one you really need.
Upvotes: 2