垃圾代码--反射的用法

 

namespace IWebTest
{
    interface interface1
    {
      int add();
    }
    public class Class1:interface1
    {
    
        private string _write;
        public string _writec;
        public Class1(string a)
        {
            this._write = a;
        }
        public Class1()
        {
            this._write = "noparam";
        }
        public string write
        {
            get { return _write; }
            set { _write = value; }       
        }
        public string writec
        {
            get { return _writec; }
            set { _writec = value; }         
        }
        public void ReflectTest()
        {
            this._write = "write";
            this._writec = "writec";           
        }
        public string ReflectTest(string a,string b)
        {
            this._write=a;
            this._writec=b;
            return _write + _writec;
       
        }
        public string WriteString(string a, int b)
        {
            return "welcome you " + a + "----"+b.ToString();
       
        }
        public static string WriteName(string name)
        {
            return "welcome " + name;
        }
        public string WriteNoParam()
        {
            return "no param";       
        }
        private string WritePrivate()
        {
            return "write private way";
        }


        public int add()
        {

            return 5;
        }

    }
}

 

 

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.Text;

using System.Reflection;

using IWebTest;

namespace ConsoleApplication1
{
    class Program
    {
        delegate string TestDelegate(string value, string value1);
        static void Main(string[] args)
        {
            //得到程序集
            Assembly t = Assembly.Load("IWebTest");
            //列出所有的类型,类,接口....
            foreach (Type aaa in t.GetTypes())
            {
                Console.Write(aaa.Name);
                Console.WriteLine("next");
            }
            //list modules(.dll files)
            Module[] modules = t.GetModules();
            foreach (Module bbb in modules)
            {
                Console.Write(bbb.Name);
                Console.WriteLine("next module");
            }
            //create instance across constructor
            Type a = t.GetType("IWebTest.Class1");

            Console.Write(a.Name);
            foreach (MethodInfo x in a.GetMethods())
            {
                Console.WriteLine(x.Name);
            }

            object[] obj = new object[] { "aaaaaaaa" };
            object dobj = Activator.CreateInstance(a, obj);
            //Get way of instance
            MethodInfo method = a.GetMethod("WriteName");
            object[] param = new object[] { "mary" };
            //invoke way
            object returnvalue = method.Invoke(dobj, param);
            Console.WriteLine(returnvalue.ToString());


            //MethodInfo add = a.GetMethod("add");
            //Console.WriteLine(add.Invoke(dobj,null));

            //invoke static way
            MethodInfo smt = a.GetMethod("WriteName");
            object[] objsmt = new object[] {"static way invoke" };
            Console.WriteLine((string)smt.Invoke(null, objsmt));
            //invoke way no param
            MethodInfo noparam = a.GetMethod("WriteNoParam");
            Console.WriteLine((string)noparam.Invoke(dobj, null));

            MethodInfo priway = a.GetMethod("WritePrivate", BindingFlags.Instance | BindingFlags.NonPublic);
            Console.WriteLine((string)priway.Invoke(dobj, null));

            //get property
            PropertyInfo[] proall = a.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo p in proall)
                Console.WriteLine(p.Name);

            PropertyInfo p1 = a.GetProperty("write");
            p1.SetValue(dobj,"_gggggggggggggggggge",null);
            Console.WriteLine(p1.GetValue(dobj,null));
            //Get field
            FieldInfo finfo = a.GetField("_write",BindingFlags.Instance | BindingFlags.NonPublic);
            Console.WriteLine(finfo.GetValue(dobj));


            //Get Constructor Information
            ConstructorInfo[] conInfo = a.GetConstructors();
            foreach (ConstructorInfo cInfoa in conInfo)
                Console.WriteLine(cInfoa.ToString());

            ConstructorInfo conInfoa=a.GetConstructor(new Type[]{typeof(string)});
            Console.WriteLine(conInfoa.ToString());
            Console.WriteLine(conInfoa.Name);

            TestDelegate tDele = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), dobj, "ReflectTest");
            Console.WriteLine(tDele("aaaaaaaaaa", "bbbbbbbbbbb"));


     

        }
    
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qixi0616

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值