C# 中的友元程序集

发布于 2024-08-30 13:20:33 字数 2496 浏览 7 评论 0原文

我正在尝试使用 [InternalsVisibleTo()] 属性创建一些“朋友程序集”,但我似乎无法让它工作。我已按照 Microsoft 的说明创建签名的好友程序集,我可以'不知道我哪里错了。所以我将在这里详细说明我的步骤,希望有人能发现我故意的错误......?

创建强名称密钥并提取公钥,因此:

sn -k StrongNameKey
sn -p public.pk
sn -tp public.pk

将强名称密钥添加到每个项目并启用签名。

创建一个名为 Internals 的项目和一个具有内部属性的类:

namespace Internals
{
    internal class ClassWithInternals
    {
        internal string Message { get; set; }
        public ClassWithInternals(string m)
        {
            Message = m;
        }
    }
}

创建另一个名为 TestInternalsVisibleTo 的项目:

namespace TestInternalsVisibleTo
{
    using Internals;

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            var c = new Internals.ClassWithInternals("Test");
            Console.WriteLine(c.Message);
        }
    }
}

编辑 Internals 项目的 AssemblyInfo.cs 文件,并添加必要的属性:

[assembly: AssemblyTitle("AssemblyWithInternals")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Internals")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("41c590dc-f555-48bc-8a94-10c0e7adfd9b")]

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly: InternalsVisibleTo("TestInternalsVisibleTo PublicKey=002400000480000094000000060200000024000052534131000400000100010087953126637ab27cb375fa917c35b23502c2994bb860cc2582d39912b73740d6b56912c169e4a702bedb471a859a33acbc8b79e1f103667e5075ad17dffda58988ceaf764613bd56fc8f909f43a1b177172bc4143c96cf987274873626abb650550977dcad1bb9bfa255056bb8d0a2ec5d35d6f8cb0a6065ec0639550c2334b9")]

最后...构建!

我收到以下错误:

错误 CS0122:“Internals.ClassWithInternals”由于其保护级别而无法访问
错误 CS1729:“Internals.ClassWithInternals”不包含采用 1 个参数的构造函数 错误CS1061:“Internals.ClassWithInternals”不包含“Message”的定义,并且找不到接受“Internals.ClassWithInternals”类型的第一个参数的扩展方法“Message”(您是否缺少using指令或程序集引用? )

基本上,就好像我没有使用InternalsVisibleTo 属性一样。现在,我不会陷入责怪工具的陷阱,那么这是怎么回事呢?有人吗?

I'm trying to create some 'friend assemblies' using the [InternalsVisibleTo()] attribute, but I can't seem to get it working. I've followed Microsoft's instructions for creating signed friend assemblies and I can't see where I'm going wrong. So I'll detail my steps here and hopefully someone can spot my deliberate mistake...?

Create a strong name key and extract the public key, thus:

sn -k StrongNameKey
sn -p public.pk
sn -tp public.pk

Add the strong name key to each project and enable signing.

Create a project called Internals and a class with an internal property:

namespace Internals
{
    internal class ClassWithInternals
    {
        internal string Message { get; set; }
        public ClassWithInternals(string m)
        {
            Message = m;
        }
    }
}

Create another project called TestInternalsVisibleTo:

namespace TestInternalsVisibleTo
{
    using Internals;

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            var c = new Internals.ClassWithInternals("Test");
            Console.WriteLine(c.Message);
        }
    }
}

Edit the AssemblyInfo.cs file for the Internals project, and add teh necessary attribute:

[assembly: AssemblyTitle("AssemblyWithInternals")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Internals")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("41c590dc-f555-48bc-8a94-10c0e7adfd9b")]

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly: InternalsVisibleTo("TestInternalsVisibleTo PublicKey=002400000480000094000000060200000024000052534131000400000100010087953126637ab27cb375fa917c35b23502c2994bb860cc2582d39912b73740d6b56912c169e4a702bedb471a859a33acbc8b79e1f103667e5075ad17dffda58988ceaf764613bd56fc8f909f43a1b177172bc4143c96cf987274873626abb650550977dcad1bb9bfa255056bb8d0a2ec5d35d6f8cb0a6065ec0639550c2334b9")]

And finally... build!

I get the following errors:

error CS0122: 'Internals.ClassWithInternals' is inaccessible due to its protection level
error CS1729: 'Internals.ClassWithInternals' does not contain a constructor that takes 1 arguments
error CS1061: 'Internals.ClassWithInternals' does not contain a definition for 'Message' and no extension method 'Message' accepting a first argument of type 'Internals.ClassWithInternals' could be found (are you missing a using directive or an assembly reference?)

Basically, it's as if I had not used the InternalsVisibleTo attrbute. Now, I'm not going to fall into the trap of blaming the tools, so what's up here? Anyone?

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

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

发布评论

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

评论(1

心作怪 2024-09-06 13:20:33

您在 InternalsVisibleTo 属性中的程序集名称后面缺少一个逗号。

它应该是:

[assembly: InternalsVisibleTo("TestInternalsVisibleTo, PublicKey=....];

程序集引用可能很挑剔,不幸的是,编译器在以下情况下不会尝试验证它们:它们以字符串形式出现在属性内部。如果您拼写错误程序集引用或提供无效密钥 - 一切都会编译 - 但只是不起作用。

You're missing a comma after the assembly name in your InternalsVisibleTo attribute.

It should be:

[assembly: InternalsVisibleTo("TestInternalsVisibleTo, PublicKey=....];

Assembly references can be finicky, and unfortunately the compiler does not attempt to verify them when they appear inside of attributes as strings. If you mispell the assembly reference or provide an invalid key - everything will compile - but just not work.

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