0
我需要一种方法来使用泛型调用在表单上设置属性。我有一个静态类,创建一种单身形式,一个主体形式(MDI)打电话,班负责打电话,知道要么显示,打开等。通用动作呼叫分配问题
一切都很好,直到现在我意识到我需要在窗体上设置属性。我可以这样做,但我希望在表单加载之前发生作业。
我想出了一个办法做到这一点,至少我认为我已经有一个很酷的想法,但是......让我们来看看代码:
public interface IFormBase
{
Action<IFormBase> SetParameters { get; set; }
}
public class FormBase : Form, IFormBase
{
public Action<IFormBase> SetParameters { get; set; }
protected override void OnLoad(EventArgs e)
{
if (SetParameters != null)
{
SetParameters.Invoke(this);
}
base.OnLoad(e);
}
}
...后来在FormManager静态类。 ..
public TResult GetSingleTonForm<TResult, T>(object state, Action<T> setParameters)
where TResult : FormBase
where T : IFormBase
{
Type t = typeof(T);
FormBase f = null;
if (f == null)
{
f = (FormBase)Activator.CreateInstance(t);
}
if (setParameters != null && f is IFormBase)
{
f.SetParameters = setParameters;
}
return (TResult)f;
}
...
问题:
无法隐式转换类型
System.Action<T>
到System.Action<blabla.IFormBase>
我理解错误,我寻求帮助,以拟定可能不同的解决方案! 谢谢!
它的工作原理!谢谢! –