我将一个 (C#) 结构转换为一个类,需要检查该类型的所有使用,以确保以前的隐式复制和现在的引用行为不会产生不良影响。
有没有办法找到所有引用,其中使用/涉及此特定类型?
我试过对该类型查找所有引用
,并获取明确说明类型名称的所有位置,这是一个好的开始。
但是,我没有得到返回和修改此类型实例的位置。特别是我使用 var
将返回值分配给隐式类型变量的行,或对先前定义的变量的所有分配。
我可以使用任何功能或技巧吗?我想,这种结构到类转换的特殊情况经常发生。也许您对如何找到所有可能的问题有一些建议?
最佳答案
您可以手动重命名类...每个错误都是使用它的地方。但我认为 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
}
所以它和Find all References
一样...
另一种解决方案,基于Visual Studio的FxCop/代码分析:
程序集的默认命名空间(您可以在属性中设置)必须是 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);
}
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>
基于此,可以很容易地为我肯定忘记的其他极端情况添加其他规则:-)
我使用的测试文件:
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)
{
}
}
请注意,调试规则很棘手...使用 FxCopCmd.exe
进行调试非常容易,但如果直接由 Visual Studio 调用则不可能(我认为,不确定)进行调试。
关于c# - Visual Studio : find all references of a specific type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30277230/