2016-06-28 57 views
0

我使用3方库并且我想调用自定义构造函数的空构造函数。如何使用IoC作为空构造函数

我使用结构图。

可能吗?从3方库

的源代码:

public static T InstantiateType<T>(Type type) 
      { 
       if (type == null) 
       { 
        throw new ArgumentNullException("type", "Cannot instantiate null"); 
       } 
       ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes); 
       if (ci == null) 
       { 
        throw new ArgumentException("Cannot instantiate type which has no empty constructor", type.Name); 
       } 
       return (T) ci.Invoke(new object[0]); 
      } 

我试图

x.For<IJobFactory>().Use<StructureMapJobFactory>(); 
       x.ForConcreteType<StructureMapJobFactory>().Configure.SelectConstructor(() => new StructureMapJobFactory(container.GetInstance<IContext>())); 
+0

请问你的代码的工作?如果不是,会发生什么? –

+0

另外,你想实例化'T'还是'type'? –

+0

嗯..我不知道,我认为类型。我设置了正确的类型,但我需要通过NOT空构造函数创建 – Mediator

回答

1

您可以使用此:

public static T InstantiateType<T>() where T : new() 
{ 
    return new T(); 
} 
+0

我无法更改代码。它是三方库源代码 – Mediator

相关问题