First excuse the poorly written summary as I am not able to describe more precisely what I am looking for.
I have the following class:
public abstract class Table<T>
{
public ReadOnlyCollection<T> list;
protected abstract Func<T, object[], bool> Func { get; }
protected T Find(params object[] objects) => list.First(element => Func(element, objects));
}
Which contains and abstract Func<T, object[], bool>
that each derived class has to implement. So for example, the following classes:
public class Person
{
public string FirstName;
}
public class Person2
{
public string FirstName;
public string LastName;
}
public class Employee
{
public int ID;
}
Will have their corresponding Table<T>
implementation like this:
public class TablePerson : Table<Person>
{
protected override Func<Person, object[], bool> Func => (p, obj)
=> p.FirstName == (string)obj[0];
public Person GetPerson(string firstName) => Find(firstName);
}
public class TablePerson2 : Table<Person2>
{
protected override Func<Person2, object[], bool> Func => (p, obj)
=> p.FirstName == (string)obj[0] && p.LastName == (string)obj[1];
public Person2 GetPerson2(string firstName, string lastName) => Find(firstName, lastName);
}
public class TableEmployee : Table<Employee>
{
protected override Func<Employee, object[], bool> Func => (p, obj)
=> p.ID == (int)obj[0];
public Employee GetEmployee(int id) => Find(id);
}
I am trying to change the object[]
parameter of Func<T, object[], bool>
to something that is type aware if possible. I am also not sure if this is the way it should be programmed.
Any help is appreciated.