如何将参数传递给由ObjectFactory创建的实例的ctor [英] How to pass param to ctor of instance that is created by ObjectFactory

查看:82
本文介绍了如何将参数传递给由ObjectFactory创建的实例的ctor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用StructureMap创建ModuleData的实例

我有许多从ModuleData继承的类(类A,B,C ...),每个类都在coustructor中获得Config1或Config2
注册表(位于file1.cs中)中,我扫描了所有类型的ModuleData.
Get (在file2.cs中列出)中,我获得了实例.

我希望当ObjectFactory在创建ModuleData实例时创建Config1/Config2时,它将参数"传递给Config1/Config2构造函数.
如何配置structuremap来做到这一点?

P.S. 注册表& 获取方法位于不同文件中!!!
谢谢

public class Config1
{
     Config1(string param)
     {
     }
}
public class Config2
{
     Config2(string param)
     {
     }
}
//.....//
public class A : ModuleData
{
    A(Config1 c)
   {
   }
}
public class B : ModuleData
{
    A(Config2 c)
   {
   }
}
//....//
//located in file1.cs
public Registry()
{
     Scan(x =>
     {
          x.TheCallingAssembly();
          x.AddAllTypesOf<ModuleData>();
     });
     ObjectFactory.Initialize(x =>
     {
             x.For<Config1>().Use<Config1>();
             x.For<Config2>().Use<Config2>();
     });
}
//....//
//located in file2.cs
public ModuleData Get(object o)
{
    var module = o as PageModule;
    var t = Type.GetType(string.Format("{0}.{1},{2}", Settings.Namespace, module.Name, Settings.Assembly));
    return ObjectFactory.With("param").EqualTo(module.Parameters).GetInstance(t) as ModuleData;
}

解决方案

我不确定我是否很好地理解了您的问题,但是如果您想使用反射来实例化对象,则可以将参数传递给它的构造函数这样:

 // 调用构造函数
对象 yourInstance = yourType.InvokeMember(,
    System.Reflection.BindingFlags.CreateInstance,
    // 此处的参数列表
     对象 [] {param1,param2,...}); 


I using StructureMap to create instances of ModuleData

I have many classes that inherit from ModuleData(class A,B,C...) and each of them get Config1 or Config2 in coustructor
In Registry(located in file1.cs) I scan all types of ModuleData.
In Get(lacated in file2.cs) I get the instance.

I want that when ObjectFactory creates Config1/Config2 while creating instance of ModuleData it will pass "param" to Config1/Config2 constructors.
How I can configure structuremap to do this?

P.S. Registry & Get methods are located in DIFFERENT files!!!
Thank you

public class Config1
{
     Config1(string param)
     {
     }
}
public class Config2
{
     Config2(string param)
     {
     }
}
//.....//
public class A : ModuleData
{
    A(Config1 c)
   {
   }
}
public class B : ModuleData
{
    A(Config2 c)
   {
   }
}
//....//
//located in file1.cs
public Registry()
{
     Scan(x =>
     {
          x.TheCallingAssembly();
          x.AddAllTypesOf<ModuleData>();
     });
     ObjectFactory.Initialize(x =>
     {
             x.For<Config1>().Use<Config1>();
             x.For<Config2>().Use<Config2>();
     });
}
//....//
//located in file2.cs
public ModuleData Get(object o)
{
    var module = o as PageModule;
    var t = Type.GetType(string.Format("{0}.{1},{2}", Settings.Namespace, module.Name, Settings.Assembly));
    return ObjectFactory.With("param").EqualTo(module.Parameters).GetInstance(t) as ModuleData;
}

解决方案

I am not sure I understood your question very well, but if you want to instanciate an object using reflection, then you can pass parameters to its constructor this way:

//invoke the constructor
object yourInstance = yourType.InvokeMember(null,
    System.Reflection.BindingFlags.CreateInstance,
    null, null,
    //your parameter list here
    new object[] { param1, param2, ... });


这篇关于如何将参数传递给由ObjectFactory创建的实例的ctor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆