我有一个简单的问题。这段代码看起来真的不对。如果适用,我想利用泛型和代表以及很可能的泛型代表。我正在使用一些代码生成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;
}
我认为他想的显示方法的代码,使他不必指定它的每一个他想要的ISomeEntity – Randolpho 2009-08-07 15:42:45