Net Core教程

c# 反射调用

本文主要是介绍c# 反射调用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

c# 反射调用

反射加载DLL

                Assembly assembly = Assembly.Load("Ant.DB.SQLServer");//加载方式一:dll文件名(当前目录)
                Assembly assembly1 = Assembly.LoadFile(@"E:\MyReflection\MyReflection\Ant.DB.SQLServer\bin\Debug\Ant.DB.Interface.dll");//加载方式二:完整路径(文件具体路径)
                Assembly assembly2 = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//方法三:完全限定名(当前目录)
                Assembly assembly3 = Assembly.LoadFrom(@"E:\MyReflection\MyReflection\Ant.DB.SQLServer\bin\Debug\Ant.DB.Interface.dll");//(文件具体路径)
                Console.WriteLine(assembly);
                Console.WriteLine(assembly1);
                Console.WriteLine(assembly2);
                Console.WriteLine(assembly3);


   foreach (var type in assembly.GetTypes())//找到所有类型
                {
                    Console.WriteLine(type.Name);
                    foreach (var item in type.GetMethods())
                    {
                        Console.WriteLine("method.name= "+ item);
                    }
                }

反射创建对象

 Assembly assembly = Assembly.Load("Ant.DB.SQLServer");//加载方式一:dll文件名(当前目录)
                Type SQLServer_type = assembly.GetType("Ant.DB.SQLServer.SQLServerHelper");
               
                object obj = Activator.CreateInstance(SQLServer_type);
                SQLServerHelper ServerHelper= obj as SQLServerHelper;
                Students students = ServerHelper.Find(1);
                Console.WriteLine(students);



                Console.WriteLine("----------------------------------------Common--------------------------------");

使用反射创建对象(带参数的构造函数)

   Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");
                Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");
                ConstructorInfo[] constructorInfos = type.GetConstructors();
                foreach (var constructor in constructorInfos)
                {
                    Console.WriteLine(constructor);//显示类型
                }

                Console.WriteLine("************************创建对象************************");
               object oCotr1 = Activator.CreateInstance(type);//无参数构造函数
               object oCotr2 = Activator.CreateInstance(type,new object[] { "111"});
               object oCotr3 = Activator.CreateInstance(type, new object[] { 123});
               object oCotr4 = Activator.CreateInstance(type, new object[] { 123,"123" });
                #region Common
                Console.WriteLine("----------------------------------------Common--------------------------------");

使用反射创建对象(私有构造函数)

Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//加载DLL文件
Type type = assembly.GetType("Ant.DB.SQLServer.PrivateCtor");//获取到类型
object oPrivate = Activator.CreateInstance(type, true);

使用反射创建泛型类

  Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件
                Type type = assembly.GetType("Ant.DB.SQLServer.GenericClass`3");//获取到类型名称 
                Type makeType = type.MakeGenericType(new Type[] { typeof(int), typeof(string), typeof(double) });
                object oGeneric = Activator.CreateInstance(makeType);

通过反射调用方法

方法无参数

Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件
                Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");//获取到类型名称 
                object oReflection = Activator.CreateInstance(type);
                foreach (var method in type.GetMethods())
                {
                    Console.WriteLine(method.Name);
                    foreach (var parameter in method.GetParameters())
                    {
                        Console.WriteLine(parameter.Name + " " + parameter.ParameterType);
                    }

                    MethodInfo methodInfo = type.GetMethod("Test1");
                    methodInfo.Invoke(oReflection, null);//调用方法
                }

方法携带参数

 Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件
                Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");//获取到类型名称 
                object oReflection = Activator.CreateInstance(type);
                foreach (var method in type.GetMethods())
                {
                    Console.WriteLine(method.Name);
                    foreach (var parameter in method.GetParameters())
                    {
                        Console.WriteLine(parameter.Name + " " + parameter.ParameterType);
                    }

               
                 MethodInfo methodInfo = type.GetMethod("Test2");
                 methodInfo.Invoke(oReflection, new object[] {123456 });//调用方法,携带参数
                }

有参数重载方法

   Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件
                Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");//获取到类型名称 
                object oReflection = Activator.CreateInstance(type);
                foreach (var method in type.GetMethods())
                {
                    Console.WriteLine(method.Name);
                    foreach (var parameter in method.GetParameters())
                    {
                        Console.WriteLine(parameter.Name + " " + parameter.ParameterType);
                    }


                   MethodInfo methodInfo = type.GetMethod("Test3", new Type[] { typeof(int), typeof(string) });
                    methodInfo.Invoke(oReflection, new object[] { 133, "123" });//调用方法
                }

无参数重载方法

 Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件
                Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");//获取到类型名称 
                object oReflection = Activator.CreateInstance(type);
                foreach (var method in type.GetMethods())
                {
                    Console.WriteLine(method.Name);
                    foreach (var parameter in method.GetParameters())
                    {
                        Console.WriteLine(parameter.Name + " " + parameter.ParameterType);
                    }


                    MethodInfo methodInfo = type.GetMethod("Test3", new Type[] { });
                      methodInfo.Invoke(oReflection, null);//调用方法
                }

静态方法调用

MethodInfo methodInfo = type.GetMethod("Test5");
methodInfo.Invoke(oReflection, new object[] { "123" });//调用方法
MethodInfo methodInfo = type.GetMethod("Test5");
methodInfo.Invoke(null, new object[] { "123" });//调用方法

Invoke第一个参数为对象的实例对象或null都可。

反射调用私有方法

                    Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件
                    Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");//获取到类型名称 
                    object oReflection = Activator.CreateInstance(type);
                    MethodInfo methodInfo = type.GetMethod("Test4", BindingFlags.Instance | BindingFlags.NonPublic);
                    methodInfo.Invoke(oReflection, new object[] { "123" });

反射调用普通类中的泛型方法

MethodInfo methodGeneric = methodInfo.MakeGenericMethod(new Type[] { typeof(int), typeof(string), typeof(DateTime) });//确定方法的参数类型和个数
methodGeneric.Invoke(oReflection, new object[] { 1, "Ant编程", DateTime.Now });

泛型类里的泛型方法调用

MethodInfo methodInfoNew = methodInfo.MakeGenericMethod(new Type[] { typeof(int), typeof(string), typeof(DateTime) });
methodInfoNew.Invoke(oReflection, new object[] { 1, "Ant编程", DateTime.Now });

反射操作字段和属性等成员

  Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件
                Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");//获取到类型名称 
                object oReflection = Activator.CreateInstance(type);
                Student studen = new Student()
                {
                    Id = 1,
                    StudentAddress = "xx",
                    StudentName = "giao"
                };

                foreach (var prop in type.GetProperties())
                {
                    Console.WriteLine($"{prop.PropertyType}+{prop.Name}={prop.GetValue(studen)}");
                    Console.WriteLine("-------------------------------------------------");
                    if (prop.Name.Equals("Id"))
                    {
                        prop.SetValue(studen, 2);
                    }
                    if (prop.Name.Equals("StudentName"))
                    {
                        prop.SetValue(studen, "giao");
                    }
                    if (prop.Name.Equals("StudentAddress"))
                    {
                        prop.SetValue(studen, "1111");
                    }
                    Console.WriteLine($"{prop.PropertyType}+{prop.Name}={prop.GetValue(studen)}");
                   
                }

还有个比较简单的方式

PropertyInfo[] propertyInfos = type.GetProperties();//查找所有的属性
PropertyInfo propertyInfo = type.GetProperty("Id");
这篇关于c# 反射调用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!