简体   繁体   English

向现有功能添加参数

[英]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. 假设我有一个DLL中的类Foo1 ,但无法对其进行修改。 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 . 我想继承Foo1并能够在原始postRead注入一个T2对象,并在基本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. 这是一个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> . Foo1<T1>采用具有两个T1参数的函数,并返回IFoo<T1> I presums that Foo1 calls that function at some point. 我假设Foo1在某个时候调用了该函数。

You are wanting to pass it a function that takes two T1 parameters and a T2 parameter. 您想要传递给它一个带有两个T1参数一个T2参数的函数。 The problem is - Foo1 knows nothing about T2 - so how is it supposed to know what to pass in? 问题是Foo1T2 一无所知 -那么它应该如何知道传入的内容呢?

Now Foo2 knows about T2 but has to pass that information on to T1 somehow. 现在, Foo2知道了T2但必须以某种方式将该信息传递给T1。 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: 可以将原始函数映射到带有较少参数的函数,但是它必须具有某种知道T2应该是什么的解析器:

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. 它至少会编译 ,但是从您的描述中尚不清楚这是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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