賞金將在 6 天后到期。此問題的答案有資格獲得 50聲望賞金。 Masoud正在從有信譽的來源尋找答案。
我有以下服務等級:
public class MyService : IService
{
public MyService(Func<string,bool> question)
{
....
}
...
}
當我在我的 WinForms 應用程式中使用此服務時,我想將以下代碼作為MyService建構式引數傳遞
(string question) =>
{
var questionForm = new SimpleQuestionForm(question);
if(questionForm.ShowDialog() == DialogResult.OK)
return true;
else
return false;
}
如何告訴 StructureMap 我的question委托是什么?
uj5u.com熱心網友回復:
我在我的 WindowsForm 專案中定義了以下注冊表類:
public class WinUIRegistry:Registry
{
public WinUIRegistry()
{
bool SimpleQuestionDelegate(string question)
{
var questionForm = new SimpleQuestionForm();
questionForm.SetData(question);
return questionForm.ShowDialog() == DialogResult.Yes;
}
For<IService>()
.Use<MyService>()
.Ctor<Func<string, bool>>().Is(SimpleQuestionDelegate);
}
}
然后通過在專案開頭的以下代碼將注冊表添加到ObjectFactoryProgram.cs的容器中:
ObjectFactory.Container.Configure(x=>x.IncludeRegistry<WinUIRegistry>());
重新戀愛:
var service = ObjectFactory.Container.GetInstance<IService>();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/419632.html
標籤:
