2016-06-28 19 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

相關問題