C# 我可以吗;“热插拔”;在运行时,TransfromBlock中的一个Func是否在另一个Func中?

C# 我可以吗;“热插拔”;在运行时,TransfromBlock中的一个Func是否在另一个Func中?,c#,task-parallel-library,func,tpl-dataflow,C#,Task Parallel Library,Func,Tpl Dataflow,我对TransformBlock进行了以下设置: private void SetupTestModule() { Func<int, int> func1 = new Func<int, int>(input => { return (input + 1); }); Func<int, int> func2 = new Func<int, int>(input =&g

我对TransformBlock进行了以下设置:

private void SetupTestModule()
{
    Func<int, int> func1 = new Func<int, int>(input =>
        {
            return (input + 1);
        });

    Func<int, int> func2 = new Func<int, int>(input =>
        {
            return (input + 2);
        });

    TransformBlock<int, int> transform = new TransformBlock<int, int>(func1);
}
编辑(包括谓词交换):

公共类TransformBlockHotSwap
{
私有转换块转换块;
私有ActionBlock ActionBlock;
公共区块热交换()
{
Func defaultFunction=新Func(项=>项);
Func func2=新Func(项目=>项目*项目);
谓词defaultPredicate=新谓词(item=>true);
谓词pred2=新谓词(项=>
{
如果(项目%2==0)
{
返回true;
}
其他的
{
返回false;
}
});
SwappableFunction SwappableFunction=新的SwappableFunction(defaultFunction);
SwappablePredicate SwappablePredicate=新的SwappablePredicate(defaultPredicate);
transformBlock=新的transformBlock(项=>SwapableFunction.Execute(项));
actionBlock=新的actionBlock(项=>Console.WriteLine(项));
LinkTo(actionBlock,item=>swappablePredicate.Execute(item));

对于(int index=1;index我不希望您能够这样做,但您可以轻松编写一个函数,该函数委托:

public class SwappableFunction<TInput, TOutput>
{
    private volatile Func<TInput, TOutput> func;

    public SwappableFunction(Func<TInput, TOutput> func)
    {
        this.func = func;
    }

    public void Swap(Func<TInput, TOutput> newFunc)
    {
        func = newFunc;
    }

    public TOutput Execute(TInput input)
    {
        return func(input);
    }
}
公共类可交换函数
{
私有volatile Func Func;
公共可交换函数(Func Func)
{
this.func=func;
}
公共无效掉期(Func newFunc)
{
func=newFunc;
}
公共TOutput执行(TInput输入)
{
返回函数(输入);
}
}
然后:

var swappable=新的SwappableFunction(输入=>input+1);
var块=新的转换块(swappable.Execute);
//后来。。。
交换(输入=>input+2);

重要-我不能100%确定这里使用的是
volatile
-我通常不喜欢使用
volatile
,因为它的语义很混乱。可能使用
互锁。compareeexchange
会更好-但是代码会明显更长,我想让大家了解主要观点首先:)

我不希望您能够这样做,但您可以轻松编写一个函数,其中委托:

public class SwappableFunction<TInput, TOutput>
{
    private volatile Func<TInput, TOutput> func;

    public SwappableFunction(Func<TInput, TOutput> func)
    {
        this.func = func;
    }

    public void Swap(Func<TInput, TOutput> newFunc)
    {
        func = newFunc;
    }

    public TOutput Execute(TInput input)
    {
        return func(input);
    }
}
公共类可交换函数
{
私有volatile Func Func;
公共可交换函数(Func Func)
{
this.func=func;
}
公共无效掉期(Func newFunc)
{
func=newFunc;
}
公共TOutput执行(TInput输入)
{
返回函数(输入);
}
}
然后:

var swappable=新的SwappableFunction(输入=>input+1);
var块=新的转换块(swappable.Execute);
//后来。。。
交换(输入=>input+2);

重要-我不能100%确定这里使用的是
volatile
-我通常不喜欢使用
volatile
,因为它的语义很混乱。可能使用
互锁。compareeexchange
会更好-但是代码会明显更长,我想让大家了解主要观点首先:)

同样的情况也适用于
谓词吗?我使用谓词作为数据块消息过滤的过滤器。我猜您的意思是说
var block=new TransformBlock(输入=>swappable.Execute(输入))
在后一段代码中?@Freddy编译器应该很高兴地将
swappable.Execute
转换为
Func
。将其包装为lambda实际上是在应用一个不必要的标识函数。@Freddy:您可能想创建一个
SwappablePredicate
类,尽管谓词实际上是一个
有趣的类c
无论如何。shambulator是对的-方法组转换应该很好。@JonSkeet,我想我不完全理解
volatile
的用法,我在不同的网站上阅读了几篇参考文献,通常得出的结论是,它最初是被推荐的,但后来就不需要使用了。在这种情况下,解决方案非常有用ly每次都以完全相同的方式工作,即使不使用
volatile
。为什么我们需要
volatile
?我在上面添加了我的代码。同样的情况也适用于
谓词
吗?我使用谓词作为数据块消息过滤的过滤器。我猜你的意思是说
var block=new TransformBlock(输入=>swappable.Execute(输入))
在后一段代码中?@Freddy编译器应该很高兴地将
swappable.Execute
转换为
Func
。将其包装为lambda实际上是在应用一个不必要的标识函数。@Freddy:您可能想创建一个
SwappablePredicate
类,尽管谓词实际上是一个
有趣的类c
无论如何。shambulator是对的-方法组转换应该很好。@JonSkeet,我想我不完全理解
volatile
的用法,我在不同的网站上阅读了几篇参考文献,通常得出的结论是,它最初是被推荐的,但后来就不需要使用了。在这种情况下,解决方案非常有用ly每次都以完全相同的方式工作,即使不使用
volatile
。那么为什么我们需要
volatile
?我在上面添加了我的代码。
public class SwappableFunction<TInput, TOutput>
{
    private volatile Func<TInput, TOutput> func;

    public SwappableFunction(Func<TInput, TOutput> func)
    {
        this.func = func;
    }

    public void Swap(Func<TInput, TOutput> newFunc)
    {
        func = newFunc;
    }

    public TOutput Execute(TInput input)
    {
        return func(input);
    }
}
var swappable = new SwappableFunction<int, int>(input => input + 1);
var block = new TransformBlock<int, int>(swappable.Execute);
// Later...

swappable.Swap(input => input + 2);