using System.Reflection;
namespace TestReflection
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("TestReflection");
Console.WriteLine("****************************************");
Console.WriteLine("****************************************");
//实例化
TestReflectionClass testCase = new TestReflectionClass() { publicUnStatic = 5 };
TestReflectionClass.publicStatic = 10;
//获得指定类型所在程序集
Assembly testClassAssembly = Assembly.GetAssembly(typeof(TestReflection.TestReflectionClass));
//获取测试类型
Type testClass = testClassAssembly.GetType("TestReflection.TestReflectionClass");
//获取所有public字段
FieldInfo[] publicFields = testClass.GetFields();
foreach (FieldInfo field in publicFields) { Console.WriteLine(field.Name); }
Console.WriteLine("****************************************");
//获取所有private非static字段
FieldInfo[] privateFields = testClass.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (FieldInfo field in privateFields) { Console.WriteLine(field.Name); }
Console.WriteLine("****************************************");
//获取所有private static字段
FieldInfo[] privateStaticFields = testClass.GetFields(BindingFlags.NonPublic | BindingFlags.Static);
foreach (FieldInfo field in privateStaticFields) { Console.WriteLine(field.Name); }
Console.WriteLine("****************************************");
//获取public非static字段
FieldInfo publicUnstaticField = testClass.GetField("publicUnStatic");
Console.WriteLine("publicUnstaticField value:" + publicUnstaticField.GetValue(testCase));
Console.WriteLine("****************************************");
//获取public static字段
FieldInfo publicStaticField = testClass.GetField("publicStatic");
Console.WriteLine("publicStaticField ByInstance value:" + publicStaticField.GetValue(testCase));
Console.WriteLine("publicStaticField ByClass value:" + publicStaticField.GetValue(null));
Console.WriteLine("****************************************");
//获取private非static字段
FieldInfo privateUnstaticField = testClass.GetField("privateUnStatic", BindingFlags.Instance | BindingFlags.NonPublic);
Console.WriteLine("privateUnstaticField value:" + privateUnstaticField.GetValue(testCase));
Console.WriteLine("****************************************");
//获取private static字段
FieldInfo privateStaticField = testClass.GetField("privateStatic", BindingFlags.Static | BindingFlags.NonPublic);
Console.WriteLine("privateStaticField ByInstance value:" + privateStaticField.GetValue(testCase));
Console.WriteLine("privateStaticField ByClass value:" + privateStaticField.GetValue(null));
privateStaticField.SetValue(null, 100);
Console.WriteLine("privateStaticField after Set:" + privateStaticField.GetValue(null));
Console.WriteLine("****************************************");
Console.WriteLine("****************************************");
//获取所有属性
PropertyInfo[] properties = testClass.GetProperties();
foreach (PropertyInfo property in properties) { Console.WriteLine(property.Name); }
Console.WriteLine("property value:" + testClass.GetProperty("PublicProperty").GetValue(testCase));
testClass.GetProperty("PublicProperty").SetValue(testCase, 100);
Console.WriteLine("property value:" + testClass.GetProperty("PublicProperty").GetValue(testCase));
Console.WriteLine("****************************************");
Console.WriteLine("****************************************");
//获取所有public方法
MethodInfo[] publicMethods = testClass.GetMethods();
foreach (MethodInfo method in publicMethods) { Console.WriteLine(method.Name); }
Console.WriteLine("****************************************");
//获取所有private方法
MethodInfo[] privateMethods = testClass.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (MethodInfo method in privateMethods) { Console.WriteLine(method.Name); }
Console.WriteLine("****************************************");
//反射调用公共普通方法
MethodInfo testFunc1 = testClass.GetMethod("TestFunc");
testFunc1.Invoke(testCase, null);
Console.WriteLine("****************************************");
//反射调用公共带参方法
MethodInfo testFunc2 = testClass.GetMethod("TestFuncWithParam");
Console.WriteLine(testFunc2.Invoke(testCase, new object[] { 6, 7 }));
Console.WriteLine("****************************************");
//反射调用私有普通方法
MethodInfo testFuncp1 = testClass.GetMethod("PTestFunc", BindingFlags.NonPublic | BindingFlags.Instance);
testFuncp1.Invoke(testCase, null);
Console.WriteLine("****************************************");
//反射调用私有带参方法
MethodInfo testFuncp2 = testClass.GetMethod("PTestFuncWithParam", BindingFlags.NonPublic | BindingFlags.Instance);
Console.WriteLine(testFuncp2.Invoke(testCase, new object[] { 6, 7 }));
Console.WriteLine("****************************************");
Console.WriteLine();
Console.ReadKey();
}
}
public class TestReflectionClass
{
/// <summary>
/// 私有字段
/// </summary>
private int privateUnStatic;
/// <summary>
/// 私有静态字段
/// </summary>
private static int privateStatic;
/// <summary>
/// 公共字段
/// </summary>
public int publicUnStatic;
/// <summary>
/// 公共静态字段
/// </summary>
public static int publicStatic;
/// <summary>
/// 公共属性
/// </summary>
public int PublicProperty
{
set
{
privateUnStatic = value;
Console.WriteLine("PublicProperty Set 已执行");
}
get
{
Console.WriteLine("PublicProperty Get 已执行");
return privateUnStatic;
}
}
/// <summary>
/// 公共无参方法
/// </summary>
public void TestFunc()
{
Console.WriteLine("公共方法");
}
/// <summary>
/// 公共有参方法
/// </summary>
public int TestFuncWithParam(int a, int b)
{
int sum = a + b;
return sum;
}
/// <summary>
/// 私有无参方法
/// </summary>
private void PTestFunc()
{
Console.WriteLine("私有方法");
}
/// <summary>
/// 私有有参方法
/// </summary>
private int PTestFuncWithParam(int a, int b)
{
int sum = a + b;
return sum;
}
}
}
标签:反射,Console,C#,testClass,testCase,int,BindingFlags,WriteLine,使用 From: https://www.cnblogs.com/REGO101100/p/18143338