簡體   English   中英

C#公共屬性使訪問器無法從另一個名稱空間訪問

[英]C# public property get accessor inaccessible from another namespace

我曾嘗試將這個領域公開。 我也嘗試過使用public get,即使據我所知,屬性內的訪問修飾符僅在更具限制性時才有效。 然而,我無法從TestUnit訪問“problem.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)

如果你看

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();
}

接口僅定義公共成員,因此您不必聲明它。 接口的所有成員都必須實現,因此也無需將它們聲明為抽象的。

除非私有列表點; 設置在代碼的更下方,我們看不到您從未初始化此變量,因此您的get將為null;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM