Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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;
}
Posted

1 solution

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:

C#
//invoke the constructor
object yourInstance = yourType.InvokeMember(null,
    System.Reflection.BindingFlags.CreateInstance,
    null, null,
    //your parameter list here
    new object[] { param1, param2, ... });
 
Share this answer
 
Comments
TheAteist 11-Mar-11 8:55am    
Thank you for your answer, but I need this using StructureMap.dll
Olivier Levrey 11-Mar-11 9:01am    
OK sorry. I don't know anything about StructureMap.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900