.NET反思:查找使用的类型类型、NET

2023-09-03 02:53:12 作者:只想当个好男人@

我一直在拉我的头发试图解决这个问题。我所试图做的是要建立的对象是如何在一个位的code我的工作正在使用的地图。把它看成是一个增强的查找使用实例。表明这一点的最简单方法是例如:

I have been pulling my hair out trying to solve this. What I am attempting to do is to build a 'map' of how objects are being used in a bit of code I am working on. Think of it as an enhanced Find Usages. The easiest way to show this is by example:

public class MasterClass
{
    Type1 type1;
    Type2 type2;
    Type3 type3;
    void InitializeData()
    {
        type1 = new Type1(this);
        type2 = new Type2(this);
        type3 = new Type3(this);
    }
}

public class Type1
{
    private MasterClass _master;
    public Type1(MasterClass master)
    {
        _master = master;
    }
    public void Something()
    {
        //use _master.type2 and _master.type3 here
    }
}

public class Type2
{
    private MasterClass _master;
    public Type2(MasterClass master)
    {
        _master = master;
    }
    public void Something()
    {
        //use _master.type3 here
    }
}

public class Type3
{
    private MasterClass _master;
    public Type3(MasterClass master)
    {
        _master = master;
    }
    public void Something()
    {
        //use _master.type1 and _master.type2 here
    }
}

我所试图做的就是一个映射或报告说,在这个例子的情况下,会看到这样的:

What I am trying to do is get a mapping or report that, in the example's case, would give something like:

使用类型1:{3类}

Type1 used by: {Type3}

使用类型2:{类型1,类型3}

Type2 used by: {Type1, Type3}

使用类型3:{类型1,类型2}

Type3 used by: {Type1, Type2}

如果我能得到它变成一本字典,然后我回家。 : - )

If I can get it into a dictionary then I am home. :-)

我已经试过什么:

我试图去在组件,每一种类型,每一种方法则拉动IL阵列,然后试图解析,没有运气的操作数。我甚至试着去在源文件中的一些常规的前pressions但我有成千上万的班去了,写在几种不同的风格,这意味着我将错过了一些参考。

I have tried going over the assemblies, each type, each method then pulling the IL arrays and then trying to parse the operands with no luck. I have even tried going over the source files with some regular expressions but I have thousands of classes to go over, written in several different styles which means I will missed some references.

我可以使用反射器和放大器;&安培; / || ReSharper的得到一次一个参考,但我想获得一次全部。

I can use Reflector &&/|| Resharper to get a single reference at a time but I would like to get them all at once.

有什么建议?

推荐答案

这并不容易。您将获得的MethodInfo,的PropertyInfo,字段信息等的实例为所有如下:

It's not easy. You will have to get an instance of MethodInfo, PropertyInfo, FieldInfo, etc for all of the following:

方法 类型 属性(get和set) 事件(添加和删除)

然后,你需要解析的IL和调用ResolveMethod实例的模块实例被检查的类型是,并且看看是否实例对应 MethodInfo的为你的方法,你正试图确定使用。

Then you'll need to parse the IL and call the ResolveMethod instance on the Module instance that the type being examined is in, and look to see if the instance corresponds to the MethodInfo for your method that you are trying to determine the use of.

编辑:

像查找所有引用在Visual Studio中,这只会发现的直接引用。如果您也想找个间接引用(例如:方法1引用方法2,和method2参考method3 - 你想看到有从方法1至method3的连接),你需要做到这一点用递归。

Like "Find All References" in Visual Studio, this will only find direct references. If you also want to find indirect references (ex: method1 references method2, and method2 references method3 - you would like to see that there is a connection from method1 to method3), you'll need to do this with recursion.