using System.Reflection;
namespace TestReflection
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("TestReflection");
Console.WriteLine("****************************************");
Console.WriteLine("****************************************");
#region dll加载与反射
//加载dll获取程序集
Assembly dllAssembly = Assembly.LoadFrom("IndustryLib.dll");
//获取程序集中所有类型
Type[] dlltypes = dllAssembly.GetTypes();
foreach (Type type in dlltypes) { Console.WriteLine(type.Namespace + "." + type.Name); }
Console.WriteLine("****************************************");
//实例化指定类型
Type classType = dllAssembly.GetType("IndustryLib.DataConvert.DataConvert");
object classObj = Activator.CreateInstance(classType);
Console.WriteLine("****************************************");
//获取类中所有方法及参数
MethodInfo[] methods = classType.GetMethods();
foreach (MethodInfo method in methods) { method.GetParameters().ToArray().ToList().ForEach(p => Console.WriteLine(method.Name + ":" + p.Name + "," + p.ParameterType.Name + ",")); }
Console.WriteLine("****************************************");
//调用方法
MethodInfo methodInfo = classType.GetMethod("StringToIntArray");
object result = methodInfo.Invoke(classObj, new object[] { "12345" });
foreach (int i in result as int[]) { Console.Write(i.ToString("x") + " "); }
Console.WriteLine();
Console.WriteLine("****************************************");
Console.WriteLine("****************************************");
#endregion
#region 类加载反射
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[] { "Hello" }));
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[] { "Hello" }));
Console.WriteLine("****************************************");
#endregion
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(string msg) { Console.WriteLine("公共有参方法已执行:" + msg); return 4; }
/// <summary>
/// 私有无参方法
/// </summary>
private void PTestFunc()
{
Console.WriteLine("私有无参方法已执行");
}
/// <summary>
/// 私有有参方法
/// </summary>
private int PTestFuncWithParam(string msg) { Console.WriteLine("私有有参方法已执行:" + msg); return 14; }
}
}
标签:反射,Console,C#,testClass,testCase,BindingFlags,WriteLine,使用,MethodInfo From: https://www.cnblogs.com/REGO101100/p/18143798