c#之System.Reflection.MethodBase

关于此类的官方链接:
http://msdn.microsoft.com/zh-cn/library/29c267yf.aspx

测试代码如下:

实施

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace testconstructinternal
{
    public class Class1
    {
        public int name=3;
        public void Print(int vname)
        {
            name = vname;
            Console.WriteLine(Convert.ToString(name));

            //获取当前正在执行方法的名称(通过反射),必须要写在正在执行方法的代码块中,而不是其它的代码块
            Console.WriteLine( MethodBase.GetCurrentMethod().Name);//获取当前正在执行方法的名称
            //getparameters获取当在执行方法的参数的相关信息
            ParameterInfo[] pi=MethodBase.GetCurrentMethod().GetParameters();
            //i表示parameterinfo数组中可能会存储多个方法参数,每个方法参数会有多个不同的属性,故用数组及length循环
            for (int i = 0; i < pi. Length; i++)
            {
                Console.WriteLine( pi.GetType().Name);//方法参数的类型
                Console.WriteLine( pi.GetValue(i).ToString());//方法参数的名称

            }
                Console.ReadKey();
            //AppDomain.CurrentDomain.BaseDirectory.
        }
    }
}


调用代码
using System;
using System.Reflection;//反射
using testconstructinternal;
public class FieldInfoClass
{
    public int myField1 = 0;
    protected string myField2 = null;//保护成员,class and subclass can access
    public  static void Gx()
    {
        ;
    }
    public static void Main() //static main method
    {
        //学习system.reflection.methodbase类的用法
        //methodbase 用于获取方法及构造函数的信息
        Class1 c1 = new Class1();
        c1.Print(34);
       
    }
}

小结:反射很复杂,要努力学习,以适应工作

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9240380/viewspace-718489/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9240380/viewspace-718489/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值