如何使得某个类无法被反射技术而访问到私有成员
public 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类时如何阻止这一操作呢?