Visual Studio:查找特定类型的所有引用

azt*_*azt 9 .net c# visual-studio

我将一个(C#)结构转换为一个类,需要经历所有类型的使用,以确保前隐式副本和现在的引用行为没有不良影响.

有没有办法找到使用/涉及这种特定类型的所有引用?

我尝试Find all References了这种类型,并获得明确说明类型名称的所有位置,这是一个良好的开端.

但是,我没有获得返回和修改此类型的实例的位置.特别是我使用返回值分配给隐含类型变量的行var,或者对先前定义的变量的所有赋值.

我可以使用任何功能或技巧吗?我想,这种结构到类转换的特殊情况经常发生.也许你有一些建议如何找到所有可能的问题?

xan*_*tos 5

您可以手动重命名该类...每个错误都是使用它的地方.但我认为Visual Studio将在一定数量的错误后停止.

您可以将[Obsolete]其标记为类,其所有属性及其所有方法.然后每次使用它们时都会发出警告.

请注意,[Obsolete]"技巧"有一些限制:

[Obsolete]
public class MyClass
{
}

public static void Test1(MyClass test2) // This line has a warning
{
    var y = test2; // ***This line doesn't have a warning***
    MyClass z = test2; // This line has a warning
}
Run Code Online (Sandbox Code Playgroud)

所以它和Find all References...一样

另一种解决方案,基于Visual Studio的FxCop/Code Analysis:

这是一个自定义规则,基于http://blogs.msdn.com/b/codeanalysis/archive/2010/03/26/how-to-write-custom-static-code-analysis-rules-and的说明-integrate了他们,成为视觉工作室,2010.aspx

程序集的默认命名空间(您可以在属性中设置)必须是MyCustomFxCopRules.平台目标是x86.

using Microsoft.FxCop.Sdk;

namespace MyCustomFxCopRules
{
    public class StructAssignmentFinder : BaseIntrospectionRule
    {
        public StructAssignmentFinder()
            : base("StructAssignmentFinder", "MyCustomFxCopRules.RuleMetadata", typeof(StructAssignmentFinder).Assembly)
        {
            var ms = new MyStruct();
            var tt = ms;
        }

        public override TargetVisibilities TargetVisibility
        {
            get
            {
                return TargetVisibilities.All;
            }
        }

        public override ProblemCollection Check(ModuleNode module)
        {
            Visit(module);
            return Problems;
        }

public override void VisitAssignmentStatement(AssignmentStatement assignment)
{
    // You could even use FullName
    if ((assignment.Source != null && assignment.Source.Type != null && assignment.Source.Type.Name.Name == "MyStruct") ||
        (assignment.Target != null && assignment.Target.Type != null && assignment.Target.Type.Name.Name == "MyStruct"))
    {
        Problem problem = new Problem(GetNamedResolution("Struct", assignment.Target.Type.Name.Name), assignment);
        Problems.Add(problem);
    }

    base.VisitAssignmentStatement(assignment);
}

public override void VisitConstruct(Construct construct)
{
    Method targetMethod = (Method)((MemberBinding)construct.Constructor).BoundMember;

    if (targetMethod.Parameters.Any(x => x.Type.Name.Name == "MyStruct"))
    {
        Problem problem = new Problem(GetNamedResolution("ParameterType", "MyStruct", targetMethod.Name.Name), construct);
        Problems.Add(problem);
    }

    base.VisitConstruct(construct);
}

public override void VisitMethodCall(MethodCall call)
{
    Method targetMethod = (Method)((MemberBinding)call.Callee).BoundMember;

    if (targetMethod.ReturnType.Name.Name == "MyStruct")
    {
        Problem problem = new Problem(GetNamedResolution("ReturnType", targetMethod.ReturnType.Name.Name, targetMethod.Name.Name), call);
        Problems.Add(problem);
    }

    if (targetMethod.Parameters.Any(x => x.Type.Name.Name == "MyStruct"))
    {
        Problem problem = new Problem(GetNamedResolution("ParameterType", "MyStruct", targetMethod.Name.Name), call);
        Problems.Add(problem);
    }

    base.VisitMethodCall(call);
}
Run Code Online (Sandbox Code Playgroud)

RuleMetadata.xml(它必须是一个嵌入的资源)

<?xml version="1.0" encoding="utf-8" ?>
<Rules FriendlyName="Rules about Structs">
  <Rule TypeName="StructAssignmentRule" Category="MyRules" CheckId="CR1000">
    <Name>Struct Assignment Finder</Name>
    <Description>Struct Assignment Finder</Description>
    <Url></Url>
    <Resolution Name="Struct">There is an assignment of struct '{0}'.</Resolution>
    <Resolution Name="ReturnType">'{0}' is the return type for a call to '{1}'.</Resolution>
    <Resolution Name="ParameterType">'{0}' is a parameter type for a call to '{1}'.</Resolution>
    <Email></Email>
    <MessageLevel Certainty="100">Warning</MessageLevel>
    <FixCategories>NonBreaking</FixCategories>
    <Owner></Owner>
  </Rule>
</Rules>
Run Code Online (Sandbox Code Playgroud)

基于此,很容易为其他角落情况添加其他规则,我肯定忘了:-)

我正在使用的测试文件:

public struct MyStruct
{

}

class Test
{
    public Test()
    {
        var ms = new MyStruct();
        var ms2 = ms;

        ms3 = ms;
        ms = ms3;

        ms4 = ms;
        ms = ms4;

        ms4 = ms4;

        new MyObject(default(MyStruct));
    }

    public MyStruct ms3;
    public MyStruct ms4 { get; set; }
}

public class MyObject
{
    public MyObject(MyStruct par1)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,调试规则很棘手...... FxCopCmd.exe使用Visual Studio直接调用时,调试很容易(但我认为不确定).