C#:Serializable() 属性是否阻止将类实例传递给另一个表单?
我有以下课程,并正在尝试为我的应用程序构建一个设置模块。但是当我尝试在 settingForm 方法中访问 _configurator 时出现异常:“对象引用未设置为对象的实例”。为什么?
[Serializable()]
public class Config
{
public Config() { }
public string ComPort
{
get
{
return comPort;
}
set
{
comPort = value;
}
}
private string comPort;
}
public partial class kineticMoldDockUserControl : UserControl
{
private settingsForm setForm = null;
private Config _cf = null;
public kineticMoldDockUserControl()
{
InitializeComponent();
_cf = new Config();
_cf.ComPort = "COM12";
}
private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!Application.OpenForms.OfType<settingsForm>().Any())
{
setForm = new settingsForm();
setForm.Show();
setForm.cf = _cf;
}
}
}
public partial class settingsForm : Form
{
Config _configutor = null;
public Config cf { get { return _configutor; } set { _configutor = value; } }
public settingsForm()
{
InitializeComponent();
try
{
MessageBox.Show(_configutor.ComPort.GetType().ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}