简体   繁体   中英

Add a parameter to an existing Func

It's a bit tricky question and I don't know if it's possible.

Let's say that I have a class Foo1 that's in a DLL and I can't modify it. The class looks like this:

public class Foo1<T1> : IFoo<T1>
{
    public Foo1(Func<IFoo, T1, T1> postRead);
}

I want to inherit Foo1 and be able to inject a T2 object in the original postRead and access it in the base postRead .

Something like this:

public class Foo2<T1, T2> : Foo<T1>
{
    public Foo2(Func<IFoo, T1, T2, T1> newPostRead)
        :base(Func<IFoo, T1, T1> secondPostRead)
    {
    }
}

Is it possible?

EDIT: I'm using insight DB . It's an ORM. There's feature that does post processing. The code looks like this:

public class PostProcessRecordReader<T> : RecordReader<T>
{
    public PostProcessRecordReader(Func<IDataReader, T, T> postRead);
}

//How to use the processor 
public DummyFunction(ObjectIWantToUse objectIwantToUse)
{
    PostProcessRecordReader<Foo> _postProcessorForCubeWithAllEntitiesForBuilding = new PostProcessRecordReader<Foo> (reader, obj) =>
        {
            //Need to have access objectIwantToUse !!!


            //maybe I want to modify obj or add some stuff to it
        }
    );
    //Do some stuff by using _postProcessorForCubeWithAllEntitiesForBuilding
}

We can do some processing on the object once returned from the DB, by accessing directly the reader.

Foo1<T1> takes a function that has two T1 parameters and returns an IFoo<T1> . I presums that Foo1 calls that function at some point.

You are wanting to pass it a function that takes two T1 parameters and a T2 parameter. The problem is - Foo1 knows nothing about T2 - so how is it supposed to know what to pass in?

Now Foo2 knows about T2 but has to pass that information on to T1 somehow. It could map the original function to one with less parameters, but it would have to have some sort of resolver that knows what T2 should be:

public class Foo2<T1, T2> : Foo1<T1>
{
    public Foo2(Func<IFoo<T1>, T1, T2, T1> newPostRead, Func<T2> resolver)
        :base((IFoo<T1> t1a, T1 t1b) => newPostRead(t1a, t1b, resolver() ))
    {
    }
}

It will at least compile , but it's not clear from your description if this is a viable solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM