【发布时间】:2010-11-17 17:45:53
【问题描述】:
我有一个简短的问题。这段代码似乎真的有问题。我想利用泛型和委托,如果适用的话,很可能是泛型委托。我正在使用一些代码生成的 api,并且生成的对象非常相似。我看到它们都实现了一个接口,所以我尝试用几种方法创建一个类来处理不同的场景。这是一些示例代码。只是在很多层面上都感觉不对。请告诉我如何使这段代码更好。如果您愿意,请提供一些重构建议。并且一定要把它撕成碎片。我想更好地编码并学会正确地做事。
private delegate IsomeEntity DisplayDelegate(IsomeEntity display);
public IsomeEntity Display<T>()
{
DisplayDelegate _del = null;
IsomeEntity display = factory.CreateObject(typeof(T).Name);
if (display.GetType() == typeof(ADisplayEntity))
_del = ADisplayEntity;
if (display.GetType() == typeof(BDisplayEntity))
_del = BDisplayEntity;
if (display.GetType() == typeof(CDisplayEntity))
_del = CDisplayEntity;
return _del(display);
}
public ADisplayEntity ADisplayEntity(IsomeEntity display)
{
ADisplayEntity ade = display as ADisplayEntity;
try
{
ADisplay o = new ADisplay();
ADisplayEntity response = o.ADisplay(ade);
return response;
}
catch (Exception ex)
{
Exception newEx;
if (someExceptionHandler.HandleException(ex, this, out newEx))
throw newEx;
}
return null;
}
public BDisplayEntity BDisplayEntity(IsomeEntity display)
{
BDisplayEntity dde = display as BDisplayEntity;
try
{
BDisplay o = new BDisplay();
BDisplayEntity response = o.BDisplay(bde);
return response;
}
catch (Exception ex)
{
Exception newEx;
if (someExceptionHandler.HandleException(ex, this, out newEx))
throw newEx;
}
return null;
}
【问题讨论】:
标签: c# generics refactoring delegates