MyAutoScaleFactor可能会导致运行时异常,因为它是一个按引用编组的类的字段

时间:2011-09-12 09:24:34

标签: c#

return new Size(bounds.Right - bounds.Left + (int)(form.MyAutoScaleFactor.Width * 4), bounds.Bottom - bounds.Top);

但我收到了

的警告
Warning 2   Accessing a member on 'NonFullscreen.MessageBoxForm.MyAutoScaleFactor' may cause a runtime exception because it is a field of a marshal-by-reference class  C:\Projekti\Skladiscenje\Skladiscenje\NonFullscreen\MessageBoxForm.cs   244 60  NonFullscreen

如何防止运行时异常?

1 个答案:

答案 0 :(得分:4)

根据the docs for that warning,这样的事情可能有所帮助(首先提取到局部变量):

var scaleFactor = form.MyAutoScaleFactor;

return new Size(bounds.Right - bounds.Left + (int)(scaleFactor.Width * 4),
                bounds.Bottom - bounds.Top);