A
absolute_hust
Unregistered / Unconfirmed
GUEST, unregistred user!
class Set
{
private int _test = 10;
public Set()
{
}
public int Test
{
get
{
return _test;
}
}
}
class App
{
static void Main()
{
Set myTest = new Set();
Console.WriteLine("{0}",myTest.Test);
Type t = typeof(Set);
try
{
t.InvokeMember("_test", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Instance, null, myTest, new object[] { 5 });
Console.WriteLine("{0}", myTest.Test);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
}
}
执行之后set类的私有变量被改成5了,请问设计set类时如何阻止这一操作呢?
{
private int _test = 10;
public Set()
{
}
public int Test
{
get
{
return _test;
}
}
}
class App
{
static void Main()
{
Set myTest = new Set();
Console.WriteLine("{0}",myTest.Test);
Type t = typeof(Set);
try
{
t.InvokeMember("_test", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Instance, null, myTest, new object[] { 5 });
Console.WriteLine("{0}", myTest.Test);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
}
}
执行之后set类的私有变量被改成5了,请问设计set类时如何阻止这一操作呢?