Unity 框架 - 将整数和字符串传递到已解析的对象中

发布于 2024-08-30 09:03:50 字数 334 浏览 6 评论 0原文

有没有办法使用 Unity 框架将整数作为参数传递到构造函数或解析的对象中?

伪代码..

IService svc = Container.Resolve<ConcreteService>()

在这种情况下,具体服务将是这样的...

public class ConcreteService
{
    public ConcreteService(int val)
    {
    }
}

而且我需要在 xml 配置中执行此操作,而不是在代码中执行此操作。

提前致谢。

is there a way using the Unity framework to pass an integer as an argument into the constructor or a resolved object?

Pseudo code..

IService svc = Container.Resolve<ConcreteService>()

in this case Concrete service will be something like this...

public class ConcreteService
{
    public ConcreteService(int val)
    {
    }
}

Also I need to do this in xml configuration as opposed to doing it in code.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

素罗衫 2024-09-06 09:03:50

希望我理解你的意思

   public class ConcreteService {

        public int Val { get; set; }

        public ConcreteService(int val) {
            Val = val;
        }
    }

现在让我们配置 Unity。

        var container = new UnityContainer();

        container.RegisterType<ConcreteService>();
        container.Configure<InjectedMembers>().ConfigureInjectionFor<ConcreteService>(new InjectionConstructor(1));

        container.RegisterType<ConcreteService>("for42");
        container.Configure<InjectedMembers>().ConfigureInjectionFor<ConcreteService>("for42",
                                                                                      new InjectionConstructor(42));
        container.RegisterType<ConcreteService>("for31");
        container.Configure<InjectedMembers>().ConfigureInjectionFor<ConcreteService>("for31",
                                                                                      new InjectionConstructor(31));

        Debug.WriteLine(container.Resolve<ConcreteService>().Val); //1
        Debug.WriteLine(container.Resolve<ConcreteService>("for42").Val); //42
        Debug.WriteLine(container.Resolve<ConcreteService>("for31").Val); //31

“for42”的等效配置是

Unity 1.4

<type type="ConcreteService"  name="for42">
          <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
                                     Microsoft.Practices.Unity.Configuration">
            <constructor>
              <param name="val" parameterType="int">
                <value value="42"/>
              </param>
            </constructor>           
          </typeConfig>
        </type>

Unity 2.0

非常相似,但没有冗余的 typeConfig 节点

<type type="ConcreteService"  name="for42">
        <constructor>
              <param name="val" parameterType="int">
                <value value="42"/>
              </param>
            </constructor>           
        </type>

Hope I understood you right

   public class ConcreteService {

        public int Val { get; set; }

        public ConcreteService(int val) {
            Val = val;
        }
    }

Now let's configure unity.

        var container = new UnityContainer();

        container.RegisterType<ConcreteService>();
        container.Configure<InjectedMembers>().ConfigureInjectionFor<ConcreteService>(new InjectionConstructor(1));

        container.RegisterType<ConcreteService>("for42");
        container.Configure<InjectedMembers>().ConfigureInjectionFor<ConcreteService>("for42",
                                                                                      new InjectionConstructor(42));
        container.RegisterType<ConcreteService>("for31");
        container.Configure<InjectedMembers>().ConfigureInjectionFor<ConcreteService>("for31",
                                                                                      new InjectionConstructor(31));

        Debug.WriteLine(container.Resolve<ConcreteService>().Val); //1
        Debug.WriteLine(container.Resolve<ConcreteService>("for42").Val); //42
        Debug.WriteLine(container.Resolve<ConcreteService>("for31").Val); //31

Equvivalent configuration for "for42" is

Unity 1.4

<type type="ConcreteService"  name="for42">
          <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
                                     Microsoft.Practices.Unity.Configuration">
            <constructor>
              <param name="val" parameterType="int">
                <value value="42"/>
              </param>
            </constructor>           
          </typeConfig>
        </type>

Unity 2.0

It's much the same but without redundant typeConfig node

<type type="ConcreteService"  name="for42">
        <constructor>
              <param name="val" parameterType="int">
                <value value="42"/>
              </param>
            </constructor>           
        </type>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文