1

To explain my problem here a short example:

public interface IData
{
    Guid Id { get; }
    DateTime Time { get; }
}

public interface IProvider
{
    IEnumerable<IData> GetObjects(Expression<Func<IData,bool>> expr);
}

public class MyData : IData { ... }

public class MyProvider : IProvider
{
    IEnumerable<IData> GetObjects(Expression<Func<IData,bool>> expr)
    {
        // !!! This part is my problem: 
        // I need to convert the Expression, defined on the Interface
        // to the actual implemented class.

        return GetObjectsSpecific(expr); // Does not work!!!
    }

    IEnumerable<MyData> GetObjectsSpecific(Expression<Func<MyData,bool>> expr)
    {
        // Already fully implemented und functional
    }
}

public class Program
{
    public void Test()
    {
        IProvider prov = new MyProvider();
        var objects = prov.GetObjects(data => data.Time < DateTime.Now);
    }
}

So the question is: Can the expression, defined on the common interface, be transformed(?) so that it uses the actual class, which implements the common interface?

Any ideas?

Faryu
  • 11
  • 2
  • The answer is going to depend heavily on what you are doing with that `Expression`. – Jay Jun 30 '15 at 15:06
  • @Faryu, See http://stackoverflow.com/questions/14007101/how-can-i-convert-a-lambda-expression-between-different-but-compatible-models#14007103 – haim770 Jun 30 '15 at 15:06
  • The reason this doesn't work, is that C# doesn't support return-type covariance http://stackoverflow.com/questions/5709034/does-c-sharp-support-return-type-covariance – Dave Bish Jun 30 '15 at 15:12
  • Is there any practical reason that `GetObjectsSpecific` needs a `MyData` predicate instead of an `IData` one? I can't imagine why it should. – Jay Jun 30 '15 at 15:12
  • I'm actually using the specific methods to read in a SQLite Database. For that purpose I need to access properties, which are not defined via IData, but are only in MyData. (Flags like "processed" or "obsolet") I guess the best solution at the moment would be to parse the Expression tree, declared on the interface and than apply / translate the Result to my specific class. Since the properties are the same (inheritance) that should not be too dificult. – Faryu Jun 30 '15 at 15:45
  • @haim770 I like the generic approach to convert the underlying Type. I will try that one first! – Faryu Jun 30 '15 at 15:53

0 Answers0