Spring.NET - Lambda 作为依赖项 - 可以使用 XML 吗?
我知道 Spring.NET 支持“Spring 表达式”——在运行时解析为代码的文本——并且可以在 XML 配置中声明它们。
但是您可以定义一个 lambda 表达式,并将其解析并用作代码中的强类型委托(例如作为构造函数参数)吗?
像这样:
<object name="Page1To2Transition"
type="XmlVsDslConfig.PageTransition, XmlVsDslConfig">
<constructor-arg type="string" value="Page1-Page2"/>
<constructor-arg ref="Page1"/>
<constructor-arg ref="Page2"/>
<property name="CanTransition" expression="#CanTransition = {|p| true }"/>
</object>
PageTransition 构造函数如下所示:
public PageTransition (
String name,
Page from,
Page to,
Func<Page, bool> canTransition )
{/*...*/}
I know Spring.NET has support for 'Spring Expressions' - text that is parsed at runtime as code - and that they can be declared in your XML config.
But can you define a lambda expression, and have it parsed and used as a strongly-typed delegate, within your code, say, as a constructor parameter?
Something like this:
<object name="Page1To2Transition"
type="XmlVsDslConfig.PageTransition, XmlVsDslConfig">
<constructor-arg type="string" value="Page1-Page2"/>
<constructor-arg ref="Page1"/>
<constructor-arg ref="Page2"/>
<property name="CanTransition" expression="#CanTransition = {|p| true }"/>
</object>
where the PageTransition constructor looks like this:
public PageTransition (
String name,
Page from,
Page to,
Func<Page, bool> canTransition )
{/*...*/}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AFAIK 直接使用 Spring.net 表达式是不可能的。也许您可以使用 Spring.net 的内置 DelegateFactoryObject 来解决通过 xml-config 将委托传递给构造函数/属性的问题。
这将创建一个 Action 类型的委托。您还可以创建任何其他类型的委托(只需在名为“DelegateType”的属性中指定类型(包括您要求的 Func 委托)。
也许您可以将它与表达式结合使用以获得您需要的结果。
AFAIK it is not possible with using Spring.net Expressions directly. Maybe you can solve your problem of passing a delegate to a constructor / property via xml-config by using the built-in DelegateFactoryObject of Spring.net.
This will create a delegate of type Action. You can also create delegates of any other type (just specify the type in the property named "DelegateType" (includes the Func delegate you asked for).
Maybe you can use it in combination with expressions to get the result you need.