How to Access a decendant object's internal method in C#
Posted
by Giovanni Galbo
on Stack Overflow
See other posts from Stack Overflow
or by Giovanni Galbo
Published on 2010-04-08T21:57:14Z
Indexed on
2010/04/08
22:13 UTC
Read the original article
Hit count: 482
I'm trying to access a method that is marked as internal in the parent class (in its own assembly) in an object that inherits from the same parent.
Let me explain what I'm trying to do...
I want to create Service classes that return IEnumberable with an underlying List to non-Service classes (e.g. the UI) and optionally return an IEnumerable with an underlying IQueryable to other services.
I wrote some sample code to demonstrate what I'm trying to accomplish, shown below. The example is not real life, so please remember that when commenting.
All services would inherit from something like this (only relevant code shown):
public class ServiceBase<T>
{
protected readonly ObjectContext _context;
protected string _setName = String.Empty;
public ServiceBase(ObjectContext context)
{
_context = context;
}
public IEnumerable<T> GetAll()
{
return GetAll(false);
}
//These are not the correct access modifiers.. I want something
//that is accessible to children classes AND between descendant classes
internal protected IEnumerable<T> GetAll(bool returnQueryable)
{
var query = _context.CreateQuery<T>(GetSetName());
if(returnQueryable)
{
return query;
}
else
{
return query.ToList();
}
}
private string GetSetName()
{
//Some code...
return _setName;
}
}
Inherited services would look like this:
public class EmployeeService : ServiceBase<Employees>
{
public EmployeeService(ObjectContext context)
: base(context)
{
}
}
public class DepartmentService : ServiceBase<Departments>
{
private readonly EmployeeService _employeeService;
public DepartmentService(ObjectContext context, EmployeeService employeeService) : base(context)
{
_employeeService = employeeService;
}
public IList<Departments> DoSomethingWithEmployees(string lastName)
{
//won't work because method with this signature is not visible to this class
var emps = _employeeService.GetAll(true);
//more code...
}
}
Because the parent class lives is reusable, it would live in a different assembly than the child services. With GetAll(bool returnQueryable) being marked internal, the children would not be able to see each other's GetAll(bool) method, just the public GetAll() method.
I know that I can add a new internal GetAll method to each service (or perhaps an intermediary parent class within the same assembly) so that each child service within the assembly can see each other's method; but it seems unnecessary since the functionality is already available in the parent class.
For example:
internal IEnumerable<Employees> GetAll(bool returnIQueryable)
{
return base.GetAll(returnIQueryable);
}
Essentially what I want is for services to be able to access other service methods as IQueryable so that they can further refine the uncommitted results, while everyone else gets plain old lists.
Any ideas?
© Stack Overflow or respective owner