「依賴倒置原則」:高層模塌不應依賴低層模塊,雙方應依賴抽象(接口)。
var implType = Assembly.GetExecutingAssembly()
.GetTypes()
.First(t => typeof(IMyInterface).IsAssignableFrom(t) && !t.IsInterface);
var instance = (IMyInterface)Activator.CreateInstance(implType);
若接口僅有一個實現,不需要接口;若多實現且調用方不想依賴具體類,應由調用方定義接口(如回調接口),實現方實現它(好萊塢原則)。