C# 限制对只读属性的方法调用的访问

C# 限制对只读属性的方法调用的访问,c#,properties,readonly,C#,Properties,Readonly,我有一个类,它定义了一个只读属性,有效地公开了一个私有字段,如下所示: public class Container { private List<int> _myList; public List<int> MyList { get { return _myList;} } public Container() : base () { _myList = new List<int&

我有一个类,它定义了一个只读属性,有效地公开了一个私有字段,如下所示:

public class Container
{
    private List<int> _myList;

    public List<int> MyList
    {
        get { return _myList;}
    }

    public Container() : base ()
    {
        _myList = new List<int>();
    }

    // some method that need to access _myList
    public SomeMethod(int x)
    {
         _myList.Add(x);
    }
}
public class Container
{
    private readonly  List<int> _myList;

    public List<int> MyList
    {
        get { return _myList;}
    }

    public Container() : base ()
    {
        _myList = new List<int>();
    }

    public void BreakReadOnly()
    {
        _myList = new List<int>();
    }
}
    public ReadOnlyCollection<int> MyList
    {
        get { return _myList.AsReadOnly(); }
    }
    public ReadOnlyCollection<int> MyList
    {
        get { return _myList.AsReadOnly(); }
    }
这种方式会破坏整个只读概念

是否有任何合理的解决方法可以使我拥有真正的只读引用属性


另外,我不能只返回列表的副本,因为这样用户会认为他做了所有必要的更改,但唉。。。它们将消失。

请查看静态方法,您可以使用该方法返回数组周围的包装,以防止对其进行修改。

不要返回对列表的直接引用。相反,返回一个环绕它的ReadOnlyCollection,或者如果列表返回类型设置为stone,则返回列表的副本。他们可以在不影响原件的情况下对副本执行任何操作。

引用是实际对象。也就是说,不能用另一个对象替换引用。因此,如果你有一个类像这样打破它:

public class Container
{
    private List<int> _myList;

    public List<int> MyList
    {
        get { return _myList;}
    }

    public Container() : base ()
    {
        _myList = new List<int>();
    }

    // some method that need to access _myList
    public SomeMethod(int x)
    {
         _myList.Add(x);
    }
}
public class Container
{
    private readonly  List<int> _myList;

    public List<int> MyList
    {
        get { return _myList;}
    }

    public Container() : base ()
    {
        _myList = new List<int>();
    }

    public void BreakReadOnly()
    {
        _myList = new List<int>();
    }
}
    public ReadOnlyCollection<int> MyList
    {
        get { return _myList.AsReadOnly(); }
    }
    public ReadOnlyCollection<int> MyList
    {
        get { return _myList.AsReadOnly(); }
    }
希望这有帮助


更新:删除了IEnumerable的使用

您需要_myList,它是一种只读的引用类型。好吧,它是只读的,只读的概念没有失败

CLR为引用类型实现一个只读属性,其方式是引用(如果需要,指向对象的指针)是只读的,而引用指向的对象可以修改

要解决这个问题,您需要将对象本身的成员字段设置为只读,因为不能通过将只读标志附加到对象的引用来将整个对象设置为只读

您可以在不可变的泛型集合类上实现,该集合类的行为与列表对象相同,但具有只读成员。

您可以返回如下所示的:

public class Container
{
    private List<int> _myList;

    public List<int> MyList
    {
        get { return _myList;}
    }

    public Container() : base ()
    {
        _myList = new List<int>();
    }

    // some method that need to access _myList
    public SomeMethod(int x)
    {
         _myList.Add(x);
    }
}
public class Container
{
    private readonly  List<int> _myList;

    public List<int> MyList
    {
        get { return _myList;}
    }

    public Container() : base ()
    {
        _myList = new List<int>();
    }

    public void BreakReadOnly()
    {
        _myList = new List<int>();
    }
}
    public ReadOnlyCollection<int> MyList
    {
        get { return _myList.AsReadOnly(); }
    }
    public ReadOnlyCollection<int> MyList
    {
        get { return _myList.AsReadOnly(); }
    }
公共只读集合MyList
{
获取{return}myList.AsReadOnly();}
}
或者返回一个新列表,以便调用者可以在不更改原始列表的情况下更改列表

    public List<int> MyList
    {
        get { return new List<int>(_myList)}
    }
公共列表MyList
{
获取{返回新列表(_myList)}
}

您忘记在代码中写入“我的列表是只读的”。)字段本身不是只读的,它是从其他类方法访问的,只有属性是只读的。但是,即使将字段声明为只读也没有帮助:(我尝试时,它构建得非常完美。如果您有一个只读字段,则该字段可以被分配一次(静态分配或在构造函数中分配).类方法访问私有字段没有问题,我只是想防止外界干扰它,所以在字段上使用readonly只会妨碍我(我在问题中添加了SomeMethod)更新了文本。但是SWeko,你希望它是只读的吗?因为在中,任何人都不能触摸内部,即使是拥有它的对象/类?或者仅仅是外部?对不起,我的意思是_myList.AsReadOnly();在现实世界中,我无法更改返回类型(IList)因此,现在我在Add和Remove上出现了一个运行时错误,但与我之前遇到的无声失败相比,这已经足够了。显然,可枚举示例无法正确执行此任务,因为您始终可以将它们强制转换为列表并更改内容。请更正此问题。