有没有办法来限制访问一个公共方法,只一个特定的类在C#中? [英] Is there a way to restrict access to a public method to only a specific class in C#?
本文介绍了有没有办法来限制访问一个公共方法,只一个特定的类在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C#中的公共方法A类。我想允许访问此方法只B类这是可能的。
I have a class A with a public method in C#. I want to allow access to this method to only class B. Is this possible?
更新:
这是我想要做的:
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();
}
}
我想其他程序员做的:
I'd like other programmers to do:
var prod = Repository.Get<Product>(id);
prod.SetInactive();
我想,以确保他们不叫ProcessInactiveProduct手动:
I'd like to make sure they don't call ProcessInactiveProduct manually:
var prod = Repository.Get<Product>(id);
prod.SetInactive();
prod.Category.ProcessInactiveProduct();
我想允许只类产品Category.ProcessInactiveProduct的访问。其他类不应该能够调用Category.ProcessInactiveProduct。
I want to allow access of Category.ProcessInactiveProduct to only class Product. Other classes shouldn't be able to call Category.ProcessInactiveProduct.
推荐答案
将两个类在一个单独的程序,使内部的方法。
Place both classes in a separate assembly and make the method internal.
这篇关于有没有办法来限制访问一个公共方法,只一个特定的类在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文