thekip の提案に触発された中間の「ファクトリ」を使用して問題を解決しましたが、コンストラクターで述語と関数を必要とするオブジェクト (上記の例の MyClass) はそのままにしておきます。このソリューションは、春にデリゲートを処理するという「問題」を MyClass の実装の外側に保持します (thekip の提案とは反対ですが、回答に感謝します)。
このような状況を構成する私の方法は次のとおりです (Spring.Net でデリゲートを設定し、オブジェクト ベースのファクトリ パターンを使用します)。
MyDelegator クラスは、使用する predicate/func のサンプル実装です (ここでは object ですが、predicate/func パラメーターに適合するものであれば何でもかまいません)。
public class MyDelegator
{
// for the predicate Predicate<TInput> condition, implemented with object
public bool Condition(object input)
{
...
}
//for the Func<TInput, TOutput> result, implemented with object
public object Evaluate(object input)
{
...
}
}
...次に、述語および/またはfuncを持つオブジェクトのコンテナが必要です(別のクラスにもある可能性があります)...
public class MySpringConfigurationDelegateObjectContainer
{
private readonly Predicate<object> condition;
private readonly Func<object, object> evaluate;
public MySpringConfigurationDelegateObjectContainer(MyDelegator strategy)
{
condition = strategy.Condition;
evaluate = strategy.Evaluate;
}
public Predicate<object> GetConditionMethod()
{
return condition;
}
public Func<object, object> GetEvaluateMethod()
{
return evaluate;
}
}
...そして、これはSpring.Net xmlでこのように構成できます
<!-- a simple container class, just has the object with the method to call -->
<object id="Container" type="MySpringConfigurationDelegateObjectContainer">
<constructor-arg name="myDelegateObject">
<!-- put the object with the delegate/func/predicate here -->
<object type="MyDelegator" />
</constructor-arg>
</object>
これで、predicate/func を持つオブジェクトの config のどこでも使用できます (predicate/func を必要とするクラスを変更する必要はありません)。
<object id="NeedAPredicateAndFuncInConstructor" type="...">
...
<constructor-arg name="condition">
<object factory-method="GetConditionMethod" factory-object="Container" />
</constructor-arg>
<constructor-arg name="result">
<object factory-method="GetEvaluateMethod" factory-object="Container" />
</constructor-arg>
...
</object>
それでおしまい。このソリューションを改善するための提案はありますか? 多分、Spring.net はすでにこれに対する一般的な解決策を持っています...