FxCop TypeNode.IsDerivedFrom 方法对于来自不同程序集的类型意外工作

发布于 2024-09-08 23:48:21 字数 646 浏览 2 评论 0原文

我在程序集“A”中有基本类型“Base”,在程序集“B”中有派生类型“Child”。

我正在使用 FxCop 在程序集“B”中查找从“Base”派生的类型,如下所示:

var baseAssemblyNode = AssemblyNode.GetAssembly("A.dll"), true, true, false);
var baseType = baseAssemblyNode.Types.Single(t => t.Name.Name == "Base");

var assemblyNode = AssemblyNode.GetAssembly("B.dll"), true, true, false); 
var derivedTypes = assemblyNode.Types.Where(t => t.IsDerivedFrom(baseType));

但结果集合为空。

我手动找到类型“Child”,发现它的基本类型不等于找到的“Base”类型。

这是预料之中的,因为这些类型节点是不同的对象,但在 Name 属性中具有深度 它们具有相同的 UniqueKey 和 Name - 我期望 FxCop 通过这些属性对 IsDerivedMethod 中的类型执行比较。但这不起作用。

解决这个问题有哪些选择?

I have base type "Base" in assembly "A" and derived type "Child" in assembly "B".

I'm using FxCop to find in assembly "B" types derived from "Base" like this:

var baseAssemblyNode = AssemblyNode.GetAssembly("A.dll"), true, true, false);
var baseType = baseAssemblyNode.Types.Single(t => t.Name.Name == "Base");

var assemblyNode = AssemblyNode.GetAssembly("B.dll"), true, true, false); 
var derivedTypes = assemblyNode.Types.Where(t => t.IsDerivedFrom(baseType));

But the result collection is empty.

I found type "Child" manually and saw that it's base type is not equal to found "Base" type.

It was expected cause these type nodes are different objects, but in depth in Name property
they have equal UniqueKey and Name - I expected that FxCop performs comparison for types in IsDerivedMethod by these properties. But it doesn't work.

What are the options to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-09-15 23:48:21

如果类型通过引用相关,这应该可以解决问题:

public override ProblemCollection Check(TypeNode type)
    {
        var winFormsReference=type.DeclaringModule.ContainingAssembly.AssemblyReferences
            .SingleOrDefault(ar => ar.Assembly.Name.StartsWith("System.Windows.Forms"));
        if (winFormsReference==null)
            return null;
        var controlType=winFormsReference.Assembly.Types.Single(t => t.FullName=="System.Windows.Forms.Control");
        _currentTypeFullName=type.FullName;
        if (type.IsDerivedFrom(controlType)==false||_shouldCheckType(type)==false)
            return null;

        var initializer = type.Members.OfType<Method>( ).SingleOrDefault(x => x.Name.Name=="InitializeComponent");
        if (initializer==null)
            Problems.Add(new Problem(NotSetResolution( ), type));
        else
            VisitMethod(initializer);
        return Problems;
    }

这就是我用来查找从 System.Windows.Forms.Control 继承的所有类,然后确保将某些属性设置为 InitializeComponent()

If the types are related by a reference this should do the trick:

public override ProblemCollection Check(TypeNode type)
    {
        var winFormsReference=type.DeclaringModule.ContainingAssembly.AssemblyReferences
            .SingleOrDefault(ar => ar.Assembly.Name.StartsWith("System.Windows.Forms"));
        if (winFormsReference==null)
            return null;
        var controlType=winFormsReference.Assembly.Types.Single(t => t.FullName=="System.Windows.Forms.Control");
        _currentTypeFullName=type.FullName;
        if (type.IsDerivedFrom(controlType)==false||_shouldCheckType(type)==false)
            return null;

        var initializer = type.Members.OfType<Method>( ).SingleOrDefault(x => x.Name.Name=="InitializeComponent");
        if (initializer==null)
            Problems.Add(new Problem(NotSetResolution( ), type));
        else
            VisitMethod(initializer);
        return Problems;
    }

This is what I am using to find all classes that inherit from System.Windows.Forms.Control and then ensure certain properties are being set to the team standards in the InitializeComponent().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文