class A
{
TypeX PropertyX;
void SomeMethod()
{
using (DisposableType y = new DisposableType())
{
PropertyX = y.GetX();
}
}
}
当 Y 被处置时,PropertyX 会怎样? 如果我不知道 Y 正在处理什么,我宁愿这样做吗?
class A : IDisposable
{
TypeX PropertyX { get; set;}
DisposableType Y { get; set; }
void SomeMethod()
{
using (Y = new DisposableType())
{
PropertyX = Y.GetX();
}
}
void Dispose()
{
Y.Dispose();
}
}
您的 MainWindow 不会被释放,但自动化实例会在执行离开 using 块后被释放。另一种写法是:
using var automation = new UIA2Automation();
MainWindow = launcher.App.GetMainWindow(automation);
写同样的东西的更详细的方式是:
var automation = new UIA2Automation();
MainWindow = launcher.App.GetMainWindow(automation);
// At this point MainWindow is already instantiated.
// We no longer need automation instance so we dispose of it.
automation.Dispose();
快速谷歌搜索将我带到 FlaUI 项目,它看起来就像你正在使用的。查看代码示例,看起来您的方法是正确的。