Is there a way to restrict access to a public method to only a specific class in C#?
Posted
by Anon
on Stack Overflow
See other posts from Stack Overflow
or by Anon
Published on 2010-04-13T13:43:50Z
Indexed on
2010/04/13
15:13 UTC
Read the original article
Hit count: 312
c#
|object-oriented-design
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?
UPDATE:
This is what i'd like to do:
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();
I'd like to make sure they don't call ProcessInactiveProduct manually:
var prod = Repository.Get<Product>(id);
prod.SetInactive();
prod.Category.ProcessInactiveProduct();
I want to allow access of Category.ProcessInactiveProduct to only class Product. Other classes shouldn't be able to call Category.ProcessInactiveProduct.
© Stack Overflow or respective owner