class Class1
{
[System.Runtime.InteropServices.DllImport("msvcrt.dll")]
static extern bool system(string str);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//改变私有静态变量的值
{
Console.WriteLine("Old Static Value:{0}",TestClass.Str);
System.Reflection.FieldInfo fiInfo = typeof(TestClass).GetField("_str",System.Reflection.BindingFlags.NonPublic| System.Reflection.BindingFlags.Static);
fiInfo.SetValue(null,"456");
Console.WriteLine("New Static Value:{0}",TestClass.Str);
}
Console.WriteLine();
//改变私有非静态变量的值
{
TestClass testClass = new TestClass("ISI");
Console.WriteLine("Old NonStatic Value:{0}",testClass.Name);
System.Reflection.FieldInfo fiInfo = testClass.GetType().GetField("_name",System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
fiInfo.SetValue(testClass,"SBU");
Console.WriteLine("New NonStatic Value:{0}",testClass.Name);
}
system("pause");
}
}
public class TestClass
{
public TestClass(string name)
{
_name = name;
}
private static string _str = "123";
private string _name = null;
public static string Str
{
get
{
return _str;
}
}
public string Name
{
get
{
return _name;
}
}
}