C#反射

C#反射之方法

反射是一种允许用户获得类信息的C#功能,Type对象映射它代表的底层对象;

在.Net 中, 一旦获得了Type对象,就可以使用GetMethods()方法获取此类型支持的方法列表;该方法的两种形式:

MethodInfo [] GetMethods()

MethodInfo [] GetMethods(BindingFlags bindingflas) :它的参数带有一些限制 BindingFlags 是一个枚举

枚举成员 [DeclaredOnly,Instance ,Public] 枚举成员的功能使用

ParameterInfo[] GetParameters() 方法返回一个方法的参数列表

方法

获取类中所有的方法

class TestClass
    {
        public int Test1( string test1,int test2) {
            Console.WriteLine("test call {0} {1}",test1,test2); 
            return 0; }
       
        private void Test2() { }
       
    }

    class Program
    {
        static void Main(string[] args)
        {
            //通过反射 获取类中的方法
            TestClass testClass = new TestClass();
            //获取到类型 获取到方法
            //BindingFlags.NonPublic   获取非公有方法 | BindingFlags.Instance 搜索当前类的方法  | BindingFlags.DeclaredOnly 只获取我们声明的
            //通过添加参数来指定获取到的方法

            MethodInfo[] methodInfo = testClass.GetType().GetMethods(BindingFlags.DeclaredOnly); 
            //对方法进行输出
            for (int i = 0; i < methodInfo.Length; i++)
            {
                Console.WriteLine(" 方法名 : {0} ",methodInfo[i].Name);
                Console.WriteLine(" 参数 : ",methodInfo[i].GetParameters());
                for (int j = 0; j < methodInfo[i].GetParameters().Length; j++)
                {
                    //输出参数名称 类型
                    Console.WriteLine(" 名称 : {0} 类型 : {1} ", methodInfo[i].GetParameters()[j].Name, methodInfo[i].GetParameters()[j].ParameterType);
                }

                if (methodInfo[i].Name=="Test1")
                {
                    methodInfo[i].Invoke(testClass, new object[] { "hello", 2 });
                }

                Console.WriteLine(" 返回值 : {0} 返回类型 : {1} ", methodInfo[i].ReturnParameter.Name,methodInfo[i].ReflectedType);
            }

            Console.ReadLine();
        }
    }

获取到指定的方法

 MethodInfo methodInfo= testClass.GetType().GetMethod("Test1");
 methodInfo.Invoke(testClass, new object[] { "hello", 2 });

字段

class TestClass
    {
        public int filed1;
        private string filed2;

        public int Field1
        {
            get
            {
                return filed1;
            }
        }

        private int Field2
        {
            get;
            set;
        }
    }
        
class Program
    {
        static void Main(string[] args)
        {
            TestClass testClass = new TestClass();
            FieldInfo fieldInfo = testClass.GetType().GetField("filed1");
            testClass.filed1 = 100;
            fieldInfo.SetValue(testClass, 1000);//设置值
            Console.WriteLine(" fieldName {0} fieldType {1} firldValue {2} ",fieldInfo.Name,fieldInfo.FieldType,fieldInfo.GetValue(testClass));

            Console.ReadLine();

        }
        }
  
    

创建对象

C#创建对象实例共有三种方法
    (1)通过System.Reflection.Assmbly里方法获得实例,主要根据类的Fullname和构造函数的参数创建实例,包括了私有和公有的,很强大

public object CreateInstance(string typeName);//使用区分大小写的方式找到指定的类型名称,FullName,
public object CreateInstance(string typeName, bool ignoreCase);//使用指定的类型名称,FullName,是否区分大小写由参数决定

//使用多种参数,binder为Null,不知道什么东西,args为构造函数的参数,culture和activation可以为null,不知道什么东西
public virtual object CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes);

//创建实例的例子:
var obj = Assembly.LoadFrom(@“D:\应用软件\VS2017\C#\WindowsFormsApp16\ClassLibrary1\bin\Debug\ClassLibrary1.dll”).CreateInstance(参数)
(2)采用System.Activator里静态方法CreateInstance获得对应的实例

public static object CreateInstance(Type type);//根据公有的无参的构造函数创建对象实例
public static object CreateInstance(Type type, bool nonPublic);//如果公共或非公共默认构造函数都可以匹配,则为 true;如果只有公共默认构造函数可以匹配,则为 false。
public static object CreateInstance(Type type, object[] args, object[] activationAttributes);//args为构造函数的参数
public static object CreateInstance(Type type, params object[] args);
public static object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture);
public static object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes);

public static ObjectHandle CreateInstance(string assemblyName, string typeName);
public static ObjectHandle CreateInstance(string assemblyName, string typeName, object[] activationAttributes);

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值