希望有人能對此發表一些看法。我有一個可選參數的接口。我們使用Unity。如果我嘗試更改方法實現中的可選參數,它將直接運行,但Unity對象使用接口的默認值 - 而不是實現的方法的默認值。C#接口,Unity和可選參數
設置:
public interface ITestOptional {
string HappyMethod(string input, bool amHappy = false);
}
public class TestingOptional : ITestOptional {
public string HappyMethod(string input, bool amHappy = true) {
if (amHappy) return input + " is Happy!";
return input + " is Not Happy!";
}
}
添加到Unity:
container.RegisterType<ITestOptional, TestingOptional>();
及測試的
//direct call
var testDirect = new TestingOptional();
string happyDirect = testDirect.HappyMethod("Cow", true); //expecting happy - get happy
string sadDirect = testDirect.HappyMethod("Cow", false); //expecting not happy - get not happy
string defaultDirect = testDirect.HappyMethod("Cow"); //expecting happy (default) get happy
//unity
var testUnity = ServiceLocator.Current.GetInstance<ITestOptional>();
string happyUnity = testUnity.HappyMethod("Cow", true); //expecting happy - get happy
string sadUnity = testUnity.HappyMethod("Cow", false); //expecting not happy - get not happy
string defaultUnity = testUnity.HappyMethod("Cow"); //expecting happy (default) but get NOT happy.
爲什麼統一性對象使用虛假的可選參數的任何想法時,實現使用真正?
我不想聽起來很關鍵,但看起來*真的很迷惑,有這種差異。 – n8wrl
我完全贊同混淆因素。我計劃讓默認匹配,然後好奇心越來越好,爲什麼它會這樣做! – imukai