gpt4 book ai didi

c# - 有没有办法将对公共(public)方法的访问限制为 C# 中的特定类?

转载 作者:太空狗 更新时间:2023-10-29 19:53:23 25 4
gpt4 key购买 nike

我在 C# 中有一个带有公共(public)方法的类 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();
}
}

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

var prod = Repository.Get<Product>(id);
prod.SetInactive();

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

var prod = Repository.Get<Product>(id);
prod.SetInactive();
prod.Category.ProcessInactiveProduct();

我只想允许 Category.ProcessInactiveProduct 访问 Product 类。其他类不应调用 Category.ProcessInactiveProduct。

最佳答案

将这两个类放在一个单独的程序集中,并使该方法成为内部方法。

关于c# - 有没有办法将对公共(public)方法的访问限制为 C# 中的特定类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2629981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com