有没有办法将对公共方法的访问限制为仅限于C#中的特定类?

Ano*_*non 7 c# oop

我在C#中有一个带有公共方法的A类.我想允许只有B类访问此方法.这可能吗?

更新:

这就是我想做的事情:

public class Category
{
    public int NumberOfInactiveProducts {get;}
    public IList<Product> Products {get;set;}

    public void ProcessInactiveProduct()
    {
        // do things...

        NumberOfInactiveProducts++;
    }
}

public class Product
{
    public bool Inactive {get;}
    public Category Category {get;set;}

    public void SetInactive()
    {
        this.Inactive= true;
        Category.ProcessInactiveProduct();
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望其他程序员这样做:

var prod = Repository.Get<Product>(id);
prod.SetInactive();
Run Code Online (Sandbox Code Playgroud)

我想确保他们不会手动调用ProcessInactiveProduct:

var prod = Repository.Get<Product>(id);
prod.SetInactive();
prod.Category.ProcessInactiveProduct();
Run Code Online (Sandbox Code Playgroud)

我想允许Category.ProcessInactiveProduct访问仅类Product.其他类不应该能够调用Category.ProcessInactiveProduct.

Hen*_*rik 18

将两个类放在单独的程序集中,并将该方法设置为内部.