【发布时间】:2015-08-21 14:25:39
【问题描述】:
我正在从 Tutorialspoint.com 进行 C# 封装。我读了这个
What is the difference between Public, Private, Protected, and Nothing?1来自 Stackoverflow 的问题。我阅读了答案,并且理解了 teoric 中的访问说明符。现在我想在 Visual Studio 中用这个主题制作控制台应用程序。
公开
同一程序集或引用它的其他程序集中的任何其他代码都可以访问该类型或成员。
私人
类型或成员只能被同一类或结构中的代码访问。
受保护
类型或成员只能被同一类或结构中的代码或派生类中的代码访问。
内部
同一程序集中的任何代码都可以访问类型或成员,但不能从另一个程序集中访问。
受保护的内部
类型或成员可以被同一程序集中的任何代码访问,也可以被另一个程序集中的任何派生类访问。
具有公共访问说明符的变量或方法可以从相同的程序集和不同的程序集访问。但这个车站在内部描述上有所不同。内部类型变量和方法只能访问相同的程序集,但不能访问 C# 中的不同程序集。我想在 C# 中测试这个站。所以我创建了两个项目并在彼此之间调用方法或变量。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TutorialsPoint.Encapsulation
{
public class PublicEncapsulation
{
//member variables
public double length;
public double width;
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
}
上面的代码是我的“PublicEncapsulation.cs”,我应该从其他程序集中调用它的成员。我的其他程序集项目的类是 Program.cs。我想从 Program.cs(其他程序集)连接 PublicEncapsulation.cs 的成员。如何在 c# 中从其他程序集执行此调用操作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.Collections;
namespace CallOtherAssemblyVariablesOrMethods
{
class Program
{
static void Main(string[] args)
{
/*Call PublicEncapsulation.cs's members in there.*/
}
}
}
上面的类是 Program.cs。我想在这里调用其他 assembly PublicEncapsulation.cs 的成员。
【问题讨论】:
-
嗯...我虽然它应该与 What is .Net Assembly 基于标题重复,但您的帖子清楚地表明您知道如何创建多个程序集...可能您需要更新标题(和可能显示代码而不是图像)。
-
"我应该从其他程序集中调用它的成员" 那么不要将它们标记为内部。我很难理解你想要什么。
标签: c# .net .net-assembly access-specifier class-members