简体   繁体   中英

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.)

我认为不解析IL代码是不可能的。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM