How can I Find out what BCL types a custom type is using its method?
If I have a class, eg T1, and I want to know what classes in the BCL it is using, how could I do this?
I came up with this:
T1.GetType().GetMethods().Where(x => x.DeclaringType.Namespace == "System");
But this will get all methods in my custom type,开发者_运维知识库 but I want to look inside each Method, so I was hoping I could do something like:
T1.GetType().GetMethods().BodyTypesUsed; to check if my method uses a type like Streamwriter.
How could I achieve this?
Well, you could call MethodBase.GetMethodBody
- that would let you see the types of the local variables. I don't know whether it would show you any "incidentally used" types though...
(You can use the parameter types as well as the return types too, of course.)
I don't think it is possible without parsing IL code.
Take a look at Reflector.NET now from Redgate. It lets you navigate the contents of an assembly and disassemble the IL into C#, VB.NET and other .NET languages.
It also lets you pick a type and analyze it in a number of ways, including the kind of thing you're after.
And it's free.
I have come up with the below:
MethodInfo mi = typeof(Class1).GetMethod("s1");
MethodBody mBody = mi.GetMethodBody();
This lets me inspect the method body and obviously see what's being used (similar to Jon's suggestion).
Thanks guys.
精彩评论