0
我有天藍色的工作角色,我有類型列表,我在運行時創建它。 我需要使用IoC(結構圖)來初始化構造函數參數。 現在我有這個類:如果在運行時創建實例,如何解析構造函數參數?
public class BuildCompletedFormatter1
{
private readonly IBuildService _buildService;
private readonly IProjectService _projectService;
public BuildCompletedFormatter(IContainer container) : base(container)
{
_projectService = container.GetInstance<IProjectService>();
_buildService = container.GetInstance<IBuildService>();
}
}
,我現在創建:
var type = instanse.GetType();
object instantiatedType = Activator.CreateInstance(type, container);
return instantiatedType;
但我需要用零個或多個paramas初始化構造函數。 我格式化也不需要知道IContaiiner
我想在構造函數中的PARAMS:
public class BuildCompletedFormatter2
{
private readonly IBuildService _buildService;
private readonly IProjectService _projectService;
public BuildCompletedFormatter(IProjectService projectService, IBuildService buildService)
{
_projectService = projectService;
_buildService = buildService;
}
}
那到底是什麼問題?顯然你已經檢查了MSDN [CreateInstance](https://msdn.microsoft.com/en-us/library/wcxyzt4d%28v=vs.110%29.aspx)(這篇文章中的示例代碼也沒有意義,因爲沒有'CreateInstance'方法將對象作爲參數)...請澄清。 –
我沒有通過IContainer容器,我需要通過服務,而當我創建實例IoC automaticaly時需要傳遞參數 – Mediator
您已經有'instanse'。爲什麼你需要從它的類型生成一個新的對象? –