C#反射

C#基础

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Base11
{
    class Program
    {
        private static BindingFlags values;

        static void Main(string[] args)
        {
            //反射  实际上就是在反编译
            //java    class   c#   type

            #region   typeof
            //Type type = typeof(Students);
            //Console.WriteLine(type);
            //Console.ReadKey();
            #endregion


            #region  gettype()
            //Students ss = new Students();
            //Type type = ss.GetType();
            //Console.WriteLine(type);
            //Console.ReadKey();
            #endregion


            #region Type.GetType("Base11.Students");
            //Type type = Type.GetType("Base11.Students");
            //Console.WriteLine(type);
            //Console.ReadKey();
            #endregion


            #region  type 类的属性
            //Type type = typeof(Students);
            Type类的属性:
            1.Name数据类型名
            //Console.WriteLine(type.Name);
            2.FullName 数据类型的完全限定名(包括命名空间名)
            //Console.WriteLine(type.FullName);
            3.Namespace 定义数据类型的命名空间名
            //Console.WriteLine(type.Namespace);
            4.IsAbstract 指示该类型是否是抽象类型
            //Console.WriteLine(type.IsAbstract);
            5.IsArray 指示该类型是否是数组
            //Console.WriteLine(type.IsArray);
            6.IsClass 指示该类型是否是类
            //Console.WriteLine(type.IsClass);
            7.IsEnum 指示该类型是否是枚举
            //Console.WriteLine(type.IsEnum);
            8.IsInterface 指示该类型是否是接口
            //Console.WriteLine(type.IsInterface);
            9.IsPublic 指示该类型是否是公有的
            //Console.WriteLine(type.IsPublic);
            10.IsSealed 指示该类型是否是密封类
            //Console.WriteLine(type.IsSealed);
            11.IsValueType 指示该类型是否是值类型
            //Console.WriteLine(type.IsValueType);

            //Console.ReadKey();
            #endregion


            #region   构造方法
            //object obj = null;
            //Type type = typeof(Students);
            //ConstructorInfo[] cis = type.GetConstructors();
            //for (int i = 0; i < cis.Length; i++)
            //{
            //    obj = cis[i].Invoke(null);
            //    Console.WriteLine(obj);
            //}

            //Type type = typeof(Students);
            参数的数据类型
            //Type[] types = new Type[2];
            //types[0] = typeof(int);
            //types[1] = typeof(string);
            //ConstructorInfo ci = type.GetConstructor(types);
            //object obj = ci.Invoke(new object[] { 1, "zs" });
            //Console.WriteLine(obj);
            //Console.ReadKey();
            #endregion


            #region  获取当前类对象的字段 public

            FieldInfo pid = type.GetField("pid");
            Console.WriteLine(pid.IsPublic);
            //   Students 对象的对象名 pid=值
            //给当前字段进行赋值
            pid.SetValue(obj, 100);
            //取值
            Console.WriteLine(pid.GetValue(obj));
            Console.ReadKey();

            //FieldInfo[] fs = type.GetFields();
            //for (int i = 0; i < fs.Length; i++)
            //{
            //    Console.WriteLine(fs[i].Name);
            //}
            //Console.ReadKey();
            #endregion


            #region 获取属性
            //PropertyInfo pname = type.GetProperty("Pname");
            //pname.SetValue(obj, "zs");
            //Console.WriteLine(pname.GetValue(obj));
            //Console.ReadKey();

            //PropertyInfo[] ps = type.GetProperties();
            //for (int i = 0; i < ps.Length; i++)
            //{
            //    if (ps[i].PropertyType.Name.Contains("Int"))
            //    {
            //        ps[i].SetValue(obj, 1);
            //    }
            //    else if (ps[i].PropertyType.Name.Equals("String"))
            //    {
            //        ps[i].SetValue(obj, "zs");
            //    }
            //    Console.WriteLine(ps[i].GetValue(obj));
            //}
            //Console.ReadKey();
            #endregion


            #region  方法
            //MethodInfo mi = type.GetMethod("show");
            //mi.Invoke(obj, null);
            //MethodInfo mi = type.GetMethod("show1");
            //if (!mi.ReturnType.Name.Equals("Void"))
            //{
            //    string str = mi.Invoke(obj, null).ToString();
            //    Console.WriteLine(str);
            //}

            //MethodInfo mi = type.GetMethod("show3");
            //ParameterInfo[] pi = mi.GetParameters();
            //if (pi.Length > 0)
            //{ 
            //    Type[] types = new Type[pi.Length];
            //    object[] values = new object[pi.Length];
            //    for (int i = 0; i < pi.Length; i++)
            //    {
            //        if (pi[i].ParameterType.Name.Equals("String"))
            //        {
            //            types[i] = typeof(String);
            //            values[i] = "zsaaa";
            //        }
            //        else
            //        {
            //            types[i] = typeof(Int32);
            //            values[i] = 1;
            //        }
            //    }
            //    mi = type.GetMethod("show3", types);
            //    if (!mi.ReturnType.Name.Equals("Void"))
            //    {
            //        object objs = mi.Invoke(obj, values);
            //        Console.WriteLine(objs.ToString());
            //    }
            //}

            //MethodInfo[] ms = type.GetMethods();
            //for (int i = 0; i < ms.Length; i++)
            //{
            //    if (!ms[i].Name.ToUpper().Contains("GET"))
            //    {
            //        if (!ms[i].Name.ToUpper().Contains("SET"))
            //        {
            //            string methodName = ms[i].Name;
            //            MethodInfo mi = type.GetMethod(methodName);
            //            if (mi.GetParameters().Length > 0)
            //            {
            //                ParameterInfo[] pi = mi.GetParameters();
            //                Type[] types = new Type[pi.Length];
            //                object[] values = new object[pi.Length];
            //                for (int j = 0; j < pi.Length; j++)
            //                {
            //                    if (pi[j].ParameterType.Name.Equals("String"))
            //                    {
            //                        types[j] = typeof(String);
            //                        values[j] = "zsaaa";
            //                    }
            //                    else
            //                    {
            //                        types[j] = typeof(Int32);
            //                        values[j] = 1;
            //                    }
            //                }
            //                mi = type.GetMethod(methodName, types);
            //                if (!mi.ReturnType.Name.Equals("Void"))
            //                {
            //                    object objs = mi.Invoke(obj, values);
            //                    Console.WriteLine(objs.ToString());
            //                }
            //                else
            //                {
            //                    mi.Invoke(obj, values);
            //                }
            //            }
            //            else
            //            {
            //                mi.Invoke(obj, null);
            //            }
            //        }
            //    }
            //}

            //Console.ReadKey();

            #endregion

            #region Assembly
            //通过DLL文件名称返回Assembly对象                            
            //Type type = ass.GetType("Base11.Students");
            //Console.WriteLine(type.FullName);

            //Assembly.LoadFrom(@"c:\ReflectionDemo2.dll");
            //Assembly ass = Assembly.Load("Base11");
            Assembly ass = Assembly.LoadFrom(@"C:\Users\19519\Desktop\Base11.dll");
            Type[] types = ass.GetTypes();
            for (int i = 0; i < types.Length; i++)
            {
                Console.WriteLine(types[i].FullName);
            }
            Console.ReadKey();
            #endregion


        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Base11
{
  public  class Students
    {

        private int pid;

        private string pname;
        //public Students() { }
        //public Students(int pid, string pname)
        //{
        //    this.pid = pid;
        //    this.pname = pname;
        //}

        public int Pid
        {
            get
            {
                return pid;
            }

            set
            {
                pid = value;
            }
        }

        public string Pname
        {
            get
            {
                return pname;
            }

            set
            {
                pname = value;
            }
        }

        //public override string ToString()
        //{
        //    return pid+"----------------"+pname; 
        //}


        public void show()
        {
            Console.WriteLine("a");
        }
        public string show1()
        {
            return "a";
        }
        public void show2(string str,int i)
        {
            Console.WriteLine(str+"------------"+i);
        }


        public string show3(string str)
        {
            if (str.Length > 3)
            {
                return str;
            }
            return "a";
        }



    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值