如何组合 Spring.Net 配置中的路径以将其用作属性或构造函数参数?

发布于 2024-12-27 10:28:25 字数 1063 浏览 2 评论 0原文

是否有一种简单的“内置方式”来组合路径以将它们用作单个参数?

我使用自己的名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2025-01-03 10:28:25

您尝试过使用表达式吗?例如:

<object id="MyObject" type="q8892913.MyClass, q8892913">
  <property name="Path" expression="T(System.IO.Path).Combine('c:\dev', 'Temp')" />
</object>

您也可以轻松使用其他对象的属性:

<object id="MyObject" type="q8892913.MyClass, q8892913">
  <property name="Path" expression="T(System.IO.Path).Combine(@(MyConfig).Path, 'Temp')" />
</object>

<object id="MyConfig" type="q8892913.MyClass, q8892913">
  <property name="Path" value="c:\dev" />
</object>

Have you tried using an expression? For example:

<object id="MyObject" type="q8892913.MyClass, q8892913">
  <property name="Path" expression="T(System.IO.Path).Combine('c:\dev', 'Temp')" />
</object>

You can easily use properties of other objects too:

<object id="MyObject" type="q8892913.MyClass, q8892913">
  <property name="Path" expression="T(System.IO.Path).Combine(@(MyConfig).Path, 'Temp')" />
</object>

<object id="MyConfig" type="q8892913.MyClass, q8892913">
  <property name="Path" value="c:\dev" />
</object>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文