简体   繁体   English

如何计算可以用不同方法修改的属性?

[英]How to calculate the attributes that can be modified by different methods?

We have a class with a set of attributes and methods (that of course can call other class methods [not attribute methods]). 我们有一个带有一组属性和方法的类(当然可以调用其他类方法[不是属性方法])。

We need to create a tool that takes as an input the source code of the class and one method name and it should return the list of possibly modified class attributes by the code on the method chain. 我们需要创建一个工具,该工具以类的源代码和一个方法名作为输入,并应通过方法链上的代码返回可能修改的类属性的列表。 For example: 例如:

private int a;
private int b;
private int c;

public void M1()
{
    if (a == 2)
    {
        this.M3();
    }
}

public void M2()
{
    a = 3;
    this.M3();
}

public void M3()
{
    if (a == 7)
    {
        b = 1;
    }
    else
    {
        c = 2;
    }
}

In this case asking for M1 it should return b and c , asking for M2 it should return a , b and c and asking for M3 it should return b and c . 在这种情况下,请求M1应该返回bc ,请求M2应该返回abc,而请求M3它应该返回bc

I have no idea on where to being with this as I never parsed code before ... thanks in advance. 我不知道该在哪里使用它,因为我之前从未解析过代码……在此先感谢。

EDIT: just to be clear, I don't need to evaluate the different paths, if I check M3 I'll always say that it affects b and c, I only care about possible changes, not the real ones. 编辑:为了清楚起见,我不需要评估不同的路径,如果我检查M3,我总是说它会影响b和c,我只关心可能的更改,而不是实际的更改。

I think your tool should run in a two fold manner, a first query to identify which variables are assigned to in each method and which methods are called in each method. 我认为您的工具应以两种方式运行,即第一个查询,以识别在每个方法中分配给哪些变量以及在每个方法中调用哪些方法。 The second query would take the method name, and trace the calls to the other methods, compiling which variables are assigned to, from each method subsequently traced to be called. 第二个查询将使用方法名称,并跟踪对其他方法的调用,并从随后跟踪要调用的每个方法中编译将变量分配给哪个方法。

To detect an assignment to a variable (and I suggest simplifying the code in your parse, examining line-by-line and removing whitespace) look for an assignment operator ('=', but not '==', '*=', '+=' etc. and I think -> and others can assign as well, MSDN will tell you the full possibilities, try regular expressions if you know them), then you would need to identify the variable being assigned to, ie the variable name directly to the left of that operator (in most cases). 要检测变量的赋值(我建议简化解析中的代码,逐行检查并删除空格),请寻找赋值运算符(“ =”,而不是“ ==”,“ * =”, '+ ='等,我想->和其他人也可以赋值,MSDN会告诉您全部的可能性,如果您知道正则表达式,请尝试使用正则表达式),那么您需要确定要分配给的变量,即变量直接在该运算符的左侧命名(在大多数情况下)。 Store a record of these variable assignments per method. 存储每个方法的这些变量分配的记录。

To detect a method call in code, you simply have to look for a method name within the method you are examining, then recursively look through that second method to find other methods and so forth... You could do the same as above to detect the method calls, then using a data structure of your own thinking, to loop through and find in that manner. 要检测代码中的方法调用,您只需在要检查的方法内查找方法名称,然后递归地浏览第二个方法以查找其他方法,依此类推...您可以执行与上述相同的操作来检测该方法调用,然后使用您自己认为的数据结构,以这种方式循环查找。

On top of that you have to worry about commented-out code, but if this is an early stages project, best not to worry yet :) 最重要的是,您必须担心注释掉的代码,但是如果这是一个早期项目,最好不要担心:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何重新排序具有不同属性的测试方法,以便它们在代码中一起出现? - How can you reorder test methods with different attributes so they are together in the code? Web API路由引擎是否足够聪明,因此可以将相同的路由属性应用于不同的方法? - Is the Web API routing engine smart enough so that identical routing attributes can be applied to different methods? 如何在C#中计算Macauley修正持续时间? - how to calculate Macauley modified duration in C#? 如何使用方法属性调用方法? - How to call methods with method attributes? 如何使用属性中定义的方法? - How to use methods defined in Attributes? 可以创建与属性相同样式的方法吗? - Can you create methods in the same style as attributes? 如何向 ASP.NET 上的帐户添加不同的属性? - How can I add different attributes to the account on ASP.NET? 如何测试通过公共方法修改的私有字段 - How to test private fields that are modified via public methods 如何使用同一计时器在不同的时间间隔执行不同的方法 - How can I execute different methods on different intervals of time with the same timer 如何在同一个控制器中使用不同的参数调用不同的GET方法? - How can I call different GET Methods in the same Controller utilizing different parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM