用this关键字为类添加扩展方法

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

namespace test
{
    class Class1
    {
        public string code = "2";
        private void Show()
        {
            Console.Write(this.GetAddress());
        }
    }

    public static class Class2
    {
        public static string GetAddress(this Class1 c1)
        {
            switch (c1.code)
            {
                case "1":
                    return "北京";
                case "2":
                    return "上海";
                default:
                    return "杭州";
            }
        }
    }
}

也可以为一个接口扩展方法,实现该接口的类将也会拥有该方法

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

namespace test
{
    interface Inter
    {
        public string Code { get; set; }
    }
    public class Class1 : Inter
    {
        private void Show()
        {
            Console.Write(this.GetAddress());
        }

        private void Haha()
        {
            Console.Write("哈哈".GetHaha());
        }

        public string Code
        {
            get;
            set;
        }
    }

    public static class Class2
    {

        public static string GetAddress(this Inter c1)
        {
            switch (c1.Code)
            {
                case "1":
                    return "北京";
                case "2":
                    return "上海";
                default:
                    return "杭州";
            }
        }

        public static string GetHaha(this string str)
        {
            return str + "哈哈";
        }
    }
}

反射:

.NET反射的定义:审查元数据并收集关于它的类型信息的能力。
http://www.cnblogs.com/focusonnet/archive/2009/04/17/1438013.html这一篇文章讲的不错

 程序集:

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

namespace ClassLibrary1
{
    public interface IWel
    {
        String Print();
    }

    public class Arithmetic:IWel
    {
        int _num1;
        public int Num1 { get { return _num1; } set { _num1 = value; } }
        int _num2;
        public int Num2 { get { return _num2; } set { _num2 = value; } }

        public Arithmetic() { }

        public Arithmetic(int num1, int num2)
        {
            _num1 = num1;
            _num2 = num2;
        }

        public String Add(int num1, int num2)
        {
            Console.WriteLine("{0}+{1}={2}", num1, num2, num1 + num2);
            return "Add(int num1,int num2)方法是一个公有的带参数的方法";
        }

        private string Add()
        {
            return "Add()方法是一个私有的不传参数的方法";
        }

        private void Subtration(int num1, int num2)
        {
            Console.WriteLine("{0}-{1}+Subtration(int num1,int num2)" +
                "方法是一个私有的带参数的方法", num1, num2, num1 - num2);
        }

        public static void Multiplication(int num1, int num2)
        {
            Console.WriteLine("{0}*{1}={2} Multipcation(int num1,int num2)" +
                "是一个公有的带参数的静态方法", num1, num2, num1 * num2);
        }

        private static void Multiplication()
        {
            Console.WriteLine("Multiplication()是一个公有的不带参数的方法";
        }

        #region Iwel 成员
        public string Print()
        {
            return "欢迎你的使用接口";
        }
        #endregion
    }
}

控制台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary1;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        delegate void TestDelegage(int num1, int num2);
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.Load("ClassLibrary1");
            Console.WriteLine("得到这个里面所有的类");
            foreach (Type var in assembly.GetTypes())
            {
                Console.WriteLine(var.Name + "是ClassLibrary1命名空间下的类");
            }

            Console.WriteLine("得到这个里面所有的模块集");
            Module[] modules = assembly.GetModules();
            foreach (Module va in modules)
            {
                Console.WriteLine(va.Name);
            }

            Type type = typeof(ClassLibrary1.Arithmetic);
            Console.WriteLine("{0}是不是public类型{1}", type, type.IsPublic);

            PropertyInfo[] memberinfo = type.GetProperties();//得到类的属性
            foreach (PropertyInfo var in memberinfo)
            {
                Console.WriteLine(type + "类的属性有" + var.Name);
            }

            Type[] t=type.GetInterfaces();
            foreach(Type var in t)
            {
                Console.WriteLine(var.Name+"是接口");
            }

            MethodInfo[] method = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (MethodInfo var in method)
            {
                Console.WriteLine("私有方法:方法名={0} 方法信息={1}", var.Name, var);
            }

            MethodInfo[] methodpublic = type.GetMethods(BindingFlags.Instance | BindingFlags.Public);
            foreach (MethodInfo var in methodpublic)
            {
                Console.WriteLine("公有方法:方法名={0} 方法信息{1}", var.Name, var);
            }

            MethodInfo[] methodstatic = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
            foreach (MethodInfo var in methodstatic)
            {
                Console.WriteLine("静态公有方法名={0} 方法信息={1}", var.Name, var);
            }

            MethodInfo[] methodprivatestatic=type.GetMethods(BindingFlags.NonPublic|BindingFlags.Static)
            foreach(MethodInfo var in methodprivatestatic)
            {
                Console.WriteLine("静态私有方法={0} 方法信息{1}",var.Name,var);
            }

            ConstructorInfo[] con=type.GetConstructors();
            foreach(ConstructorInfo var in con)
            {
                Console.WriteLine(var);
            }
            
            object obj=Activator.CreateInstance(type,null);
            MethodInfo men1=type.GetMethod("Add");
            Console.WriteLine("调用{0}方法",men1);
            object[] nums1={5,4};
            Console.WriteLine(men1.Invoke(obj,nums1));

            MethodInfo men2=type.GetMethod("Add",BindingFlags.Instance|BindingFlags.NonPublic);
            Console.WriteLine(men2.Invoke(obj,nums1));

            MethodInfo men3=type.GetMethod("Multiplication",BindingFlags.Public|BindingFlags.Static);
            object[] nums2={5,6};
            men3.Invoke(null,nums2);

            MethodInfo men4=type.GetMethod("Multiplication",BindingFlags.Static|BindingFlags.NonPublic);
            men4.Invoke(null,null);

            TestDelegage dele=(TestDelegage)Delegate.CreateDelegate(typeof(TestDelegage),obj,"Subtration");
            dele(9,3);
        }
    }
}

特性:明天再看吧

posted on 2012-07-24 19:55  tnspop  阅读(425)  评论(0编辑  收藏  举报