我试图更一般地写这个:
public static CormantRadDock RecreateDock(RadDockSetting settings)
{
CormantRadDock dock = new CormantRadDock();
settings.SetSettings(dock);
Logger.DebugFormat("Recreated dock {0}", dock.ID);
return dock;
}
我有这个:
public static T Recreate<T>() where T : new()
{
T _control = new T();
//settings.SetSettings(dock);
Logger.DebugFormat("Recreated control {0}", (_control as Control).ID);
return _control;
}
通用解决方案:
public interface ISetting<T>
{
void SetSettings(T obj);
}
public void SetSettings(CormantRadDock dock)
{
// do stuff with dock
}
谢谢!
答案 0 :(得分:2)
这是你想要做的吗?
// move the generic type parameter up here so the interface is generic
// and not the method definition
public interface ISetting<T>
{
void SetSettings(T obj);
}
然后你可以在一个类中成功实现它:
public class SomeClass: ISetting<YourSettingType>
{
public void SetSettings(YourSettingType obj) { ... }
}
答案 1 :(得分:0)
或者
public abstract class ISetting<T>
{
public abstract void SetSettings<T>(T obj);
}
public void SetSettings<T>(T dock)
{
// do stuff with dock
}
或
public abstract class ISetting<T>
{
public abstract void SetSettings(T obj);
}
public void SetSettings(T dock)
{
// do stuff with dock
}
答案 2 :(得分:0)
这个怎么样:
public interface ICanBeRecreated<T>
{
T Recreate();
}
public class CormantDock : ICanBeRecreated<CormantDock>
{
private RadDockSetting _settings;
private void ApplySettings(RadDockSetting settings)
{
// apply settings
}
public CormantDock Recreate()
{
var dock = new CormantDock;
dock.ApplySettings(_settings);
}
}