c#公共属性get访问器从另一个命名空间不可访问

本文关键字:访问 另一个 命名空间 属性 get | 更新日期: 2023-09-27 18:04:03

我已经尝试使字段本身公开;我也尝试过使用公共获取,即使,据我所知,访问修饰符内的属性只会有效果,如果更多的限制。然而,我无法访问'问题。TestUnit中的Points(最后一行)属性。我得到一个"获取访问器不可访问"的警报。注意,我可以从同一名称空间中的另一个类访问它。我一定错过了一些非常基本的东西。

namespace Coordinates_Path
{
    public interface IProblem
    {
        abstract public List<Node> Points { get; set; }
        abstract public Object GetStartState();
        abstract public bool IsGoalState();
        abstract public Object GetSuccessor();
    }
    public class ShortestPathThroughCoordinates : IProblem
    {
        private Node startState;
        private List<Node> points;
        public List<Node> Points { get { return points; } private set; }
    //...
    //...
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Coordinates_Path;
using System.Linq;
using System.Collections.Generic;
namespace CoordPathTest
{
    [TestClass]
    public class KruskalTest
{
    [TestMethod]
    public void TestMST()
    {
        // ...
        IProblem problem = new ShortestPathThroughCoordinates("P1", coordDic);
        MSTKruskal kruskal = new MSTKruskal(problem.Points)

c#公共属性get访问器从另一个命名空间不可访问

如果你看

public class ShortestPathThroughCoordinates : IProblem
{
    public List<Node> Points { get { return points; } private set; }
    ...

所有被引用的类必须对调用程序集可见。检查确保Node也可见

把你的界面改成这样:

public interface IProblem
{
     List<Node> Points { get; set; }
     Object GetStartState();
     bool IsGoalState();
     Object GetSuccessor();
}

接口只定义公共成员,所以你不必声明它。接口的所有成员都必须被实现,因此也没有必要将它们声明为抽象的。

除非private List指向;是在代码的后面我们看不到的地方设置的你从来没有初始化这个变量所以你的get将为null;