Castle Windsor:如何将组件连接到工厂属性而不是方法

时间:2021-09-14 00:06:49

I have the following component

我有以下组件

public class MyTimer : IMyTimer {
  public MyTimer(TimeSpan timespan){...}
}

Where timespan should be provided by the property ISettings.MyTimerFrequency.

时间跨度应由ISettings.MyTimerFrequency属性提供。

How do I wire this up in windsor container xml? I thought I could do something like this:

如何在windsor容器xml中连接它?我以为我可以这样做:

    <component id="settings"
                service="MySample.ISettings, MySample"
                type="MySample.Settings, MySample"
                factoryId="settings_dao" factoryCreate="GetSettingsForInstance">
        <parameters><instance_id>1</instance_id></parameters>
    </component>

    <component id="my_timer_frequency"
                type="System.TimeSpan"
                factoryId="settings" factoryCreate="MyTimerFrequency" />

    <component id="my_timer" 
                service="MySample.IMyTimer, MySample"
                type="MySample.MyTimer, MySample">
        <parameters><timespan>${my_timer_frequency}</timespan></parameters>         

but I am getting an error because MyTimerFrequency is a property when the factory facility expects a method.

但是我收到一个错误,因为当工厂设施需要一个方法时,MyTimerFrequency是一个属性。

Is there a simple resolution here? Am I approaching the whole thing the wrong way?

这里有简单的解决方案吗?我是以错误的方式接近整件事吗?

EDIT: There is definitely a solution, see my answer below.

编辑:肯定有一个解决方案,请参阅下面的答案。

2 个解决方案

#1


4  

The solution actually came to me in a dream. Keep in mind that properties are not a CLR construct but rather C# syntactic sugar. If you don't believe me just try compiling

解决方案实际上是在梦中找到我的。请记住,属性不是CLR构造,而是C#语法糖。如果你不相信我只是尝试编译

public class MyClass {
  public object Item {
    get;
  }
  public object get_Item() {return null;}
}

results in a Error: Type 'TestApp.MyClass' already reserves a member called 'get_Item' with the same parameter types

导致错误:类型'TestApp.MyClass'已经保留了一个名为'get_Item'的成员,其成员具有相同的参数类型

Since the Xml configuration is pursed at runtime after compilation, we can simply bind to a factoryCreate property by binding to the method that it compiles to so the above example becomes:

由于Xml配置在编译后在运行时进行,我们可以通过绑定到它编译的方法简单地绑定到factoryCreate属性,因此上面的示例变为:

<component id="my_timer_frequency"
                        type="System.TimeSpan"
                        factoryId="settings" factoryCreate="get_MyTimerFrequency" />

And voila!

Someone vote this up since I can't mark it as an answer.

有人投了这票,因为我无法将其标记为答案。

#2


0  

Wouldn't the simplest solution be to add a method which wraps the property?

最简单的解决方案不是添加一个包装属性的方法吗?

#1


4  

The solution actually came to me in a dream. Keep in mind that properties are not a CLR construct but rather C# syntactic sugar. If you don't believe me just try compiling

解决方案实际上是在梦中找到我的。请记住,属性不是CLR构造,而是C#语法糖。如果你不相信我只是尝试编译

public class MyClass {
  public object Item {
    get;
  }
  public object get_Item() {return null;}
}

results in a Error: Type 'TestApp.MyClass' already reserves a member called 'get_Item' with the same parameter types

导致错误:类型'TestApp.MyClass'已经保留了一个名为'get_Item'的成员,其成员具有相同的参数类型

Since the Xml configuration is pursed at runtime after compilation, we can simply bind to a factoryCreate property by binding to the method that it compiles to so the above example becomes:

由于Xml配置在编译后在运行时进行,我们可以通过绑定到它编译的方法简单地绑定到factoryCreate属性,因此上面的示例变为:

<component id="my_timer_frequency"
                        type="System.TimeSpan"
                        factoryId="settings" factoryCreate="get_MyTimerFrequency" />

And voila!

Someone vote this up since I can't mark it as an answer.

有人投了这票,因为我无法将其标记为答案。

#2


0  

Wouldn't the simplest solution be to add a method which wraps the property?

最简单的解决方案不是添加一个包装属性的方法吗?