InternalsVisibleTo 似乎被忽略

发布于 2025-01-07 11:29:22 字数 2238 浏览 0 评论 0原文

我正在尝试对 .net 中的私有函数进行单元测试。这个私有函数返回一个myClass类型的集合,它是一个内部类。

我使用了程序集属性 InternalsVisibleTo,以便我的测试项目知道类型 myClass

这是我要测试的代码:

namespace MyProject
{
    public class Class1
    {
         private List<myClass> myFunction()
         {
             return new List<myClass>();
         }

         internal class myClass
         {
             public int MyProperty { get; set; }
         }   
     }
 }

[TestMethod()]
[DeploymentItem("MyProject.dll")]
public void myFunctionTest()
{
    Class1_Accessor target = new Class1_Accessor(); 
    List<Class1_Accessor.myClass> expected = null; 
    List<Class1_Accessor.myClass> actual;
    actual = target.myFunction();
    Assert.AreEqual(expected, actual);
    Assert.Inconclusive("Verify the correctness of this test method.");
}

在我的程序集信息文件中:

[assembly: InternalsVisibleTo("MyProject.Test")]

那么为什么 Visual Studio 将列表的类型设置为 Class1_Accessor.myClass 因为我知道 myClass测试项目?

因此,我收到运行时错误(无法将类型 myClass 转换为类型 Class1_Accessor.myClass)。

因为 myFunction 是私有的,所以 VisualStudio 生成以下代码(这对于大多数代码来说都很好)

[Shadowing("MyProject.Class1")]
public class Class1_Accessor : BaseShadow
{
    protected static PrivateType m_privateType;

    [Shadowing(".ctor@0")]
    public Class1_Accessor();
    public Class1_Accessor(PrivateObject value);

    public static PrivateType ShadowedType { get; }

    public static Class1_Accessor AttachShadow(object value);
    [Shadowing("myFunction@0")]
    public List<Class1_Accessor.myClass> myFunction();

    [Shadowing("MyProject.Class1+myClass")]
    public class myClass : BaseShadow
    {
        protected static PrivateType m_privateType;

        [Shadowing(".ctor@0")]
        public myClass();
        public myClass(PrivateObject value);

        [Shadowing("MyProperty")]
        public int MyProperty { get; set; }
        public static PrivateType ShadowedType { get; }

        public static Class1_Accessor.myClass AttachShadow(object value);
    }
}

但是,我不明白为什么它包含 myClass 的新定义,因为它是一个内部类,不需要任何访问器。在我看来,这就是问题的根源。

I'm trying to unit test a private function in .net. This private function returns a collection of type myClass, which is an internal class.

I've used the assembly attribute InternalsVisibleTo, so that the type myClass is known to my Test project.

Here's the code I want to test:

namespace MyProject
{
    public class Class1
    {
         private List<myClass> myFunction()
         {
             return new List<myClass>();
         }

         internal class myClass
         {
             public int MyProperty { get; set; }
         }   
     }
 }

[TestMethod()]
[DeploymentItem("MyProject.dll")]
public void myFunctionTest()
{
    Class1_Accessor target = new Class1_Accessor(); 
    List<Class1_Accessor.myClass> expected = null; 
    List<Class1_Accessor.myClass> actual;
    actual = target.myFunction();
    Assert.AreEqual(expected, actual);
    Assert.Inconclusive("Verify the correctness of this test method.");
}

and in my assembly info file:

[assembly: InternalsVisibleTo("MyProject.Test")]

So why does Visual Studio set the type of the list to Class1_Accessor.myClass since myClass is known to my test project ?

Because of that I get a runtime error (cannot convert type myClass to type Class1_Accessor.myClass).

Because myFunction is private, VisualStudio generates the following code (which is fine for most of it)

[Shadowing("MyProject.Class1")]
public class Class1_Accessor : BaseShadow
{
    protected static PrivateType m_privateType;

    [Shadowing(".ctor@0")]
    public Class1_Accessor();
    public Class1_Accessor(PrivateObject value);

    public static PrivateType ShadowedType { get; }

    public static Class1_Accessor AttachShadow(object value);
    [Shadowing("myFunction@0")]
    public List<Class1_Accessor.myClass> myFunction();

    [Shadowing("MyProject.Class1+myClass")]
    public class myClass : BaseShadow
    {
        protected static PrivateType m_privateType;

        [Shadowing(".ctor@0")]
        public myClass();
        public myClass(PrivateObject value);

        [Shadowing("MyProperty")]
        public int MyProperty { get; set; }
        public static PrivateType ShadowedType { get; }

        public static Class1_Accessor.myClass AttachShadow(object value);
    }
}

However, I don't understand why it contains a new definition of myClass, since it is an internal class, which shouldn't need any accessor. This is the root of the problem in my opinion.

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

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

发布评论

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

评论(2

陌若浮生 2025-01-14 11:29:22

无法将类型 myClass 转换为类型 Class1_Accessor.myClass

说明了我们需要知道的一切:简单地说 - 您有两个不同的 myClass 定义。类型的范围由程序集决定; [InternalsVisibleTo(...)] 使其可以访问,但除此之外:一切如常。

找出为什么/在哪里你有第二个 myClass ,以及以下之一:

  • 消除歧义(命名空间限定等)
  • 重命名其中之一,
  • 如果它们含义相同并且不正确地重复,则删除其中之一

这正是与具有相同:

namespace A { class Foo {} }
namespace B { class Foo {} }

这是两个完全不相关的类,称为 Foo,并且它们之间的转换将失败。

cannot convert type myClass to type Class1_Accessor.myClass

says everything we need to know: simply - you have two different definitions of myClass. Types are scoped by assembly; the [InternalsVisibleTo(...)] makes it accessible, but beyond that: business as usual.

Find why/where you have a second myClass, and one of:

  • disambiguate (namespace-qualify etc)
  • rename one of them
  • remove one of them if they mean the same thing, and are incorrectly duplicated

This is exactly the same as having:

namespace A { class Foo {} }
namespace B { class Foo {} }

that is two completely unrelated classes called Foo, and casting between them will fail.

椵侞 2025-01-14 11:29:22

InternalsVisibleTo 不会使私有成员对友元程序集可见。它适用于标记为内部的成员/类型。请在此处查看文档。

更新

尝试一下(按照此doc):重新生成您的测试类,在“创建单元测试”对话框中,单击“设置”。在测试生成设置中,确保选中 Honor InternalsVisibleTo Attribute 复选框

InternalsVisibleTo does not make private members visible to friend assemblies. It works on members/types marked as internal. Check the docs here.

Update

Try this (as per this doc): regenerate your test class, in the Create Unit Test dialog box, click Settings. In the Test Generation Settings make sure that Honor InternalsVisibleTo Attribute check box is checked

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