繁体   English   中英

Visual Studio 2015中C#中的未签名的朋友程序集

[英]Unsigned Friend Assembly in C# in Visual Studio 2015

我在Visual Studio 2015(C#,WPF)中创建了最小的解决方案,以从另一个程序集中访问一个程序集中的内部类。

到目前为止,还没有运气。

MainWindow.xaml.cs

using System.Windows;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("FriendAssemblyTestLibrary")]

namespace FriendAssemblyTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            GlobalData.Member = "hi";  // this works fine
        }
    }
}

GlobalData.cs

namespace FriendAssemblyTest
{
    internal static class GlobalData
    {
        internal static string Member { get; set; }
    }
}

Class1.cs

namespace FriendAssemblyTestLibrary
{
    public class Class1
    {
        public Class1()
        {
            GlobalData.Member = ""; // this generates: "The name 'GlobalData' does not exist in the current context"
        }
    }
}

解决方案是FriendAssemblyTest,它包含两个项目FriendAssemblyTest和FriendAssemblyTestLibrary(类库)。

如果需要,可以在以下位置下载压缩解决方案: https : //drive.google.com/open?id=0Bwmvskxz-sI1TTBzamQ0WEpLTUE

对我来说令人困惑的是,在Visual Studio 2015中似乎无法设置/ out编译器选项,但是朋友程序集示例似乎坚持要求这样做。

感谢任何人可以发送我的帮助。

谢谢! 汤姆

谢谢所有回复的人。

我想我终于解决了“全局变量”的难题(我敢肯定,在::)之前已经解决了很多次。

没有什么超级酷的东西,但是对于任何想要一个体面的起点在其解决方案中存储类型化全局变量的人来说,都非常重要。

我为全局数据类创建了第3个项目,并以这种方式进行了连接,以避免出现循环引用问题。

MainWindow.xaml.cs

using System.Windows;

using FriendAssemblyTestLibrary;

namespace FriendAssemblyTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            GlobalData.GlobalData.Member = "hi";
            GlobalData.GlobalData.TestDecimal = 3.67M;

            var x = new Class1();
        }
    }
}

GlobalData.cs

namespace GlobalData
{
    public static class GlobalData
    {
        public static string Member { get; set; }
        public static decimal TestDecimal { get; set; }
    }
}

Class1.cs

namespace FriendAssemblyTestLibrary
{
    public class Class1
    {
        public Class1()
        {
            GlobalData.GlobalData.Member = "";
            GlobalData.GlobalData.TestDecimal = 7.6M;
        }
    }
}

解决方法如下: https : //drive.google.com/open?id=0Bwmvskxz-sI1czVaTzRNUTBKdG8

为了完整起见,这是一个URL参考,描述了全局变量的一些利弊。

http://c2.com/cgi/wiki?GlobalVariablesAreBad

对我来说,这似乎是一个很好的资源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM