public class Top { public Top() { var profileSettings = new Settings(); BinaryFormatter bf = new BinaryFormatter(); FileStream fs = new FileStream(@PROFILE, FileMode.Open); profileSettings = (Settings) bf.Deserialize (fs); fs.Close ();
Label_Name.Text = profileSettings.Name; } }
エラーが起きるのは最後の行です。 public Top()の部分です。 System.FieldAccessException: Transparent method System.Runtime.Serialization.Formatters.Binary.ObjectReader:SetObjectValue (object,string,System.Reflection.MemberInfo,System.Runtime.Serialization.SerializationInfo,object,System.Type,int[]) cannot get or set private/internal field Test.Settings:_Name. と表示されます。よろしくお願いします。
Settingsのクラスの変数が private string _Name; private int _Month,_Day; private Test.Sex _Sex; ではなく、 public string _Name; public int _Month,_Day; public Test.Sex _Sex; でした。
分類:[.NET]
http://dobon.net/vb/dotnet/programing/storeappsettings.html
↑のサイトを参考にプログラムを組んでみたのですが、読み込み作業でエラーが出ます。以下にコードを載せますので、間違いを指摘していただけると助かります。
public class Test
{
const string PROFILE = "C:\Documents\settings.config";
public enum Sex{Man, Woman};
public static string name;
public static int month,day;
public static Sex sex;
public Test()
{
name = "";
Button_1.Click += OnEnter;
Button_Man.Click += OnMan;
Button_Woman.Click += OnWoman;
Button_Save.Click += OnSave;
}
public void OnEnter(object sender, EventArgs e)
{
if (TextBox_Name.Text != "")
{
name = TextBox_Name.Text;
month = TextBox_M.Month;
day = TextBox_D.Day;
}
}
public void OnMan(object sender, EventArgs e)
{
sex = Sex.Man;
}
public void OnWoman(object sender, EventArgs e)
{
sex = Sex.Woman;
}
private void Enter_Click(object sender, EventArgs e)
{
Settings profileSettings = new Settings();
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream(@PROFILE, FileMode.Create);
bf.Serialize (fs, profileSettings);
fs.Close ();
}
}
[Serializable()]
public class Settings
{
private string _Name;
private int _Month,_Day;
private Test.Sex _Sex;
public string Name
{
get {return _Name;}
set {_Name = value;}
}
public int Month
{
get {return _Month;}
set {_Month = value;}
}
public int Day
{
get {return _Day;}
set {_Day = value;}
}
public Test.Sex Sex
{
get {return _Sex;}
set {_Sex = value;}
}
public Settings()
{
_Name = Test.name;
_Month = Test.month;
_Day = Test.day;
_Sex = Test.sex;
}
}
public class Top
{
public Top()
{
var profileSettings = new Settings();
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream(@PROFILE, FileMode.Open);
profileSettings = (Settings) bf.Deserialize (fs);
fs.Close ();
Label_Name.Text = profileSettings.Name;
}
}
エラーが起きるのは最後の行です。
public Top()の部分です。
System.FieldAccessException: Transparent method System.Runtime.Serialization.Formatters.Binary.ObjectReader:SetObjectValue (object,string,System.Reflection.MemberInfo,System.Runtime.Serialization.SerializationInfo,object,System.Type,int[]) cannot get or set private/internal field Test.Settings:_Name.
と表示されます。よろしくお願いします。