阻止 class 访问它自己的字段之一

Prevent a class from access to one of its own field

在 C# 中,有没有办法阻止 class 访问它自己的字段之一?

例如:

class MyClass()
{
    private object field;
    public object Property
    {
        get { return field; }
        set { field = value; }
    }

    public void myMethod()
    {
        field = null; // forbidden
        Property = null; // allowed
    }
}

原因是总是使用setter所以调用了一些函数。

没有。如果您想在访问器中的某个点设置其值,则无法阻止 class 访问其自己的字段。

这样您就不需要支持字段了。 但我不认为你正在寻找这样的。

Class MyClass()
{
    public object Property { get; set; }

    public void myMethod()
    {
        Property = null; // allowed
    }
}