是否可以选择这样做中间类:
INterfaceClass - > IndirectClass< - FormClass
____________________ | ^ | _______________
AnotherClass
我想在AnotherClass中使用FormClass(textBox)中的对象,其中IndirectClass将负责向我提供对该文本框的访问。
答案 0 :(得分:0)
在构造间接类的实例时,只需传递对表单类的引用。
public class IndirectClass
{
private FormClass _form;
public IndirectClass(FormClass form)
{
_form = form;
}
public FormClass
{
get { return _form; }
}
}
现在您可以像这样使用它:
var f = new FormClass();
var c = new IndirectClass(f);
c.f.[whatever]; // Note that [whatever] needs to be visible outside of f
您还可以将变量存储到FormClass
内的IndirectClass
成员中,以消除一个间接层。