Take the following example (created purely to demonstrate the point). What I am trying to do is isolate one part of the system from another and only want to expose a specific subset of the functionality externally from the assembly while internally working against the full objects methods.
This code compiles, but I get an invalid cast exception at runtime. It feels like this should work but unfortunately it does not.
Can anybody suggest an elegant solution to this problem?
UPDATED: Based on comments I have changed this example to better demonstrate the issue, I also in the sample now show the solution that worked for me...
using System.Collections.Generic;
namespace Test
{
public class PeopleManager
{
List<Person> people = new List<Person>();
public PeopleManager()
{
}
public void CreatePeople()
{
people.Add(new Person("Joe", "111 aaa st"));
people.Add(new Person("Bob", "111 bbb st"));
people.Add(new Person("Fred", "111 ccc st"));
people.Add(new Person("Mark", "111 ddd st"));
}
public IList<IName> GetNames()
{
/* ERROR
* Cannot convert type 'System.Collections.Generic.List<Test.Person>'
* to 'System.Collections.Generic.List<Test.IName>' c:\notes\ConsoleApplication1\Program.cs
*/
return (List<IName>) people; // <-- Error at this line
// Answered my own question, do the following
return people.ConvertAll(item => (IName)item);
}
}
public interface IName
{
string Name { get; set; }
}
internal class Person : IName
{
public Person(string name, string address)
{
this.Name = name;
this.Address = address;
}
public string Name
{
get;
set;
}
public string Address
{
get;
set;
}
}
}