1
動態創建對象,我有以下代碼:基於類型
public async Task DispatchAsync(NoticeChannelType type, string message)
{
switch (type)
{
case NoticeChannelType.Email:
{
var email = JsonConvert.DeserializeObject<NoticeEmail>(message);
await _channelResolver.ResolveAsync(email);
}
break;
case NoticeChannelType.Pushover:
{
var pushover = JsonConvert.DeserializeObject<NoticePushover>(message);
await _channelResolver.ResolveAsync(pushover);
}
break;
default:
break;
}
}
我想以某種方式刪除此開關,創建並把對象的具體類型。
的channelResolver.ResolveAsync
後實現如下:
public async Task ResolveAsync<T>(T channel) where T : INoticeChannel
{
if (channel == null)
throw new ArgumentNullException(nameof(channel),
$"Channel: '{typeof(T).Name}' cannot be null.");
var handler = _context.Resolve<INoticeExecutor<T>>();
await handler.SendAsync(channel);
}
我試圖將它重構到這樣的事情:
public async Task DispatchAsync(NoticeChannelType type, string message)
{
var channel = _context.ResolveKeyed<INoticeChannel>(type);
Type myType = Type.GetType(channel.GetType().FullName);
await _channelResolver.ResolveAsync((INoticeChannel)JsonConvert.DeserializeObject(message, myType));
}
但之後在channelResolver.ResolveAsync
型T
是INoticeChannel
,而不是具體類型如此_context.Resolve<INoticeExecutor<T>>();
無法解決它。
這是可能刪除此開關,並使此代碼更優雅,更容易維護?
非常感謝,我知道我們不能擁有所有東西(優雅,易於維護等),但我希望在這種情況下我能處理它。 –