如何组合 Spring.Net 配置中的路径以将其用作属性或构造函数参数?
是否有一种简单的“内置方式”来组合路径以将它们用作单个参数?
我使用自己的名为 SpringPathCombiner 的实现来执行以下操作:
<property name="CombinedPath">
<object type="SpringExt.SpringPathCombiner, SpringExt">
<constructor-arg name="path1">
<object factory-method="GetBasePath" factory-object="MyConfig" />
</constructor-arg>
<constructor-arg name="path2" value="Temp" />
</object>
</property>
这会将组合路径设置为返回值的组合 方法 GetBasePath 与“Temp”一起使用,例如 C:\MyBasePath\Temp。
SpringPathCombiner类本身非常简单:
public class SpringPathCombiner
{
private readonly string path;
public SpringPathCombiner(string path1, string path2)
{
path = Path.Combine(path1, path2);
}
public static implicit operator string(SpringPathCombiner combiner)
{
return combiner.path;
}
public override string ToString()
{
return path;
}
}
但是我不想有代码重复,并且我认为必须有一种方法来利用Spring本身带来的东西来完成这种工作。任何人都知道如何在没有自己的实现的情况下做到这一点,例如直接使用 Path.Combine ?
Is there an easy "built-in-way" of combining paths for using them as a single parameter?
I use an own Implementation called SpringPathCombiner to do the following:
<property name="CombinedPath">
<object type="SpringExt.SpringPathCombiner, SpringExt">
<constructor-arg name="path1">
<object factory-method="GetBasePath" factory-object="MyConfig" />
</constructor-arg>
<constructor-arg name="path2" value="Temp" />
</object>
</property>
This will set the Combined path to the combination of the return value of the
method GetBasePath together with "Temp", e.g. C:\MyBasePath\Temp.
The SpringPathCombiner class itself is very simple:
public class SpringPathCombiner
{
private readonly string path;
public SpringPathCombiner(string path1, string path2)
{
path = Path.Combine(path1, path2);
}
public static implicit operator string(SpringPathCombiner combiner)
{
return combiner.path;
}
public override string ToString()
{
return path;
}
}
But I don't want to have code duplication and I think there must by a way to do this kind of job with the things brought by Spring itself. Anyone knows a way how to do this without an own implementation, e.g. using Path.Combine directly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试过使用表达式吗?例如:
您也可以轻松使用其他对象的属性:
Have you tried using an expression? For example:
You can easily use properties of other objects too: