在 C# 中,如何使用来自不同程序集的 Public Class 成员(方法或变量)

In C#, How Can I Use Public Class Members(Methods Or Variables) From Different Assembly

我正在研究来自 Tutorialspoint.com 的 C# 封装。我从 Whosebug 上读到了这个

What is the difference between Public, Private, Protected, and Nothing?1
问题。我阅读了答案并且理解了 teoric 中的访问说明符。现在我想在 visual studio.

中制作带有此主题的控制台应用程序

public

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

私有

The type or member can only be accessed by code in the same class or struct.

受保护

The type or member can only be accessed by code in the same class or struct, or in a derived class.

内部

The type or member can be accessed by any code in the same assembly, but not from another assembly.

受保护的内部

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

具有 public 访问说明符的变量或方法可从同一程序集和不同程序集访问。但是这个站在内部描述上是不同的。内部类型变量和方法只能访问相同的程序集,但不能访问 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',我应该从其他 assembly.My 调用它的成员,其他汇编项目的 class 是 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.*/
        }
    }
}

上方 class 是 Program.cs。我想在这里给其他大会 PublicEncapsulation.cs 的成员打电话。

我猜你的 Program.cs 你有这样的东西:

var typeFromOtherAssembly = new InternalEncapsulation();

// Here you expect a compiler error:
var area = typeFromOtherAssembly.GetArea();

// This should return a string.
var details = typeFromOtherAssembly.Display();

您认为 newDisplay() 会起作用,并且(内部)GetArea() 调用会显示编译器错误:

'InternalEncapsulation' does not contain a definition for 'GetArea' and no extension method 'GetArea' accepting a first argument of type 'InternalEncapsulation' could be found (are you missing a using directive or an assembly reference?)

但是您没有为 InternalEncapsulation class 指定访问修饰符,所以它是 internal:

Internal is the default if no access modifier is specified.

所以在 new InternalEncapsulation 你会得到另一个编译器错误:

InternalEncapsulation is inaccessible due to its protection level

所以你需要做到public:

public class InternalEncapsulation

2 天前我有一个简单的问题。我用 Whosebug 解决了我的问题。我想看看 internal 和 public 访问说明符之间的区别。然后我创建了两个项目来查看它们的区别。如果我可以调用 public 方法并且不能从其他程序集调用内部方法,那么 C# 控制台应用程序支持理论知识。我想这样做。但是我看不到其他项目的 public 成员。然后我在这个 How to use a Class from one C# project with another C# project 教程中找到了解决方案。我应该通过右键单击在项目中添加引用。

SOLUTION STEPS

1. In the 'Solution Explorer' tree, expand the 'CallOtherAssemblyVariablesOrMethods' project and then right-click the project and select 'Add Reference' from the menu.

2. On the 'AddReference' dialog, select the 'Projects' tab and select your 'TutorialsPoint ' project.

3. If you are using namespaces then you will need to import the namespaces for your 'CallOtherAssemblyVariablesOrMethods' types by adding 'using' statements to your files in 'TutorialsPoint'.

非常感谢大家...