C# 由于其保护级别,无法访问公共类

C# 由于其保护级别,无法访问公共类,c#,C#,我有一个公共类,我正试图在另一个项目中引用它。它有一个构造函数: namespace Stuff { struct Vector { public double x { get; set; } public double y { get; set; } public double z { get; set; } public Vector (double ex, double why, double zee)

我有一个公共类,我正试图在另一个项目中引用它。它有一个构造函数:

namespace Stuff
{
    struct Vector
    {
        public double x { get; set; }
        public double y { get; set; }
        public double z { get; set; }

        public Vector (double ex, double why, double zee) 
        {
            this.x = ex;
            this.y = why;
            this.z = zee;
        }

由于保护级别错误,我一直无法访问

这就是我在另一个项目中引用它的方式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Stuff;

namespace ProjectileMotion
{
    class Projectile
    {
        private Vector acceleration = new Vector(0, 0, -9.8); //the acceleration is a vector now.


类“Vector”在一个名为“stuff”的项目中,它需要一个更好的名称。

您需要将
结构定义为
public

public struct Vector { ... }
仅因为构造函数是公共的,并不意味着类/结构也是公共的


对于当前代码,
struct
只能在包含的程序集中访问,因为默认的访问修饰符是
internal
。但是,在该程序集中,类随处可见。

向量
结构声明为public,然后再试一次,它显然不是
public
您的结构不是类,它也不是public。要使其公开,请添加
public struct Vector
,您需要将
struct Vector
替换为
public struct Vector
。按原样,
Vector
仅在同一DLL中可用,因为默认的访问器是
internal
。默认情况下,没有访问修饰符的任何类或结构定义都是内部的,而不是公共的。