• 文档03_反射机制




    反射初探

    实例的class

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleAppModel03
    {
        class TestClass
        {
            public TestClass() { }
            public TestClass(string str) { this.name = str; }
            public string name { get; set; }
            public int age { get; set; }
            public bool sex { get; set; }
            public void Erite()
            {
                if(this.name!=null)
                    System.Console.WriteLine(this.name);
                else
                    System.Console.WriteLine("value is null");
            }
            public void Erite(string str)
            {
                if (this.name != null)
                    System.Console.WriteLine(this.name);
                else
                    System.Console.WriteLine("value is null");
                System.Console.WriteLine("value is "+str);
            }
        }
    }
    


















    运行示例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;/*引用反射的空间*/
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleAppModel03
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("列出ConsoleAppModel03.exe程序集的所有类型");
                //加载程序集
                Assembly ass = Assembly.LoadFrom("ConsoleAppModel03.exe");
                Type classobj = null;
                //获取定义的类型
                Type[] types = ass.GetTypes();
                foreach(Type t in types)
                {
                    Console.WriteLine(t.Name);
                    if (t.Name == "TestClass")
                    { classobj = t; }
                }
                Console.WriteLine("列出TestClass的所有公共方法");
                //返回当前Type的所有公共方法。
                MethodInfo[] mif = classobj.GetMethods();
                foreach(MethodInfo m in mif)
                {
                    Console.WriteLine(m.Name);
                }
    
                Console.WriteLine("实例化TestClass的Erite()方法");
                /*使用默认化结构对象*/
                object obj = Activator.CreateInstance(classobj);
                /*使用有参数的结构对象*/
                object nobj = Activator.CreateInstance(classobj,"LWW");
                /*空重载方法*/
                MethodInfo mi1 = classobj.GetMethod("Erite",new Type[]{});
                /*有参数的重载方法*/
                MethodInfo mi2 = classobj.GetMethod("Erite", new Type[] { typeof(string) });
    
                //使用指定的对象,调用它的方法
                mi1.Invoke(obj,null);
                mi1.Invoke(nobj,null);
    
                Console.WriteLine("调用有参数重载的方法");
                object[] obpars = new object[1];
                obpars[0] = "123456";
    
                mi2.Invoke(obj,obpars);
                mi2.Invoke(nobj, new object[]{"123"});
                Console.ReadLine();
            }
        }
    }
    


    ps:懂得不多,感觉上可以外部调用任何。net的程序集,完成本地加载的操作。




    关于equas和==:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleAppModel04
    {
        class Program
        {
            static void Main(string[] args)
            {
                string s1 = "test";
                string s2 = "test";
                Console.WriteLine("测试string 与 string");
                Console.WriteLine(s1==s2);//true
                Console.WriteLine(s1.Equals(s2));//true
    
                char[] c = new char[] { 't', 'e', 's', 't' };
                string s11 = new string(c);
                string s22 = new string(c);
                Console.WriteLine("测试string 与 string");
                Console.WriteLine(s11 == s22);//true
                Console.WriteLine(s11.Equals(s22));//true
    
    
                object o1 = s1;
                object o2 = s2;
                Console.WriteLine("测试object 与 object");
                Console.WriteLine(o1 == o2);//true
                Console.WriteLine(o1.Equals(o2));//true
    
                string s3=new string(new char[]{'t','e','s','t'});
                string s4=new string(new char[]{'t','e','s','t'});
                Console.WriteLine("测试new 的string");
                Console.WriteLine(s3==s4);//true
                Console.WriteLine(s3.Equals(s4));//true
    
                /*隐示的创建了o3 同object o3 = new object(); o3 = s3;*/
                object o3 = s3;
                object o4 = s4;
                Console.WriteLine("1111111111测试object被赋值new string");
                Console.WriteLine(o3==o4);//false
                Console.WriteLine(o3.Equals(o4));//true
    
                User u1 = new User("lww");
                User u2 = new User("lww");
                Console.WriteLine("测试两个new user");
                Console.WriteLine(u1==u2);//false
                Console.WriteLine(u1.Equals(u2));//false
    
                User u3 = new User("lww");
                User u4 = u3;
                Console.WriteLine("测试赋值的user");
                Console.WriteLine(u3==u4);//true
                Console.WriteLine(u3.Equals(u4));//true
    
                Console.ReadKey();
            }
        }
        public class User
        {
            public User() { }
            public User(string str) { this.name = str; }
            public string name { get; set; }
            public int age { get; set; }
        }
    }
    




    关于const和readonly
    const是运行时常数,readonly是运行时常数
    const在编译的时候需要有一个确定的值,而readonly则不需要,即const为常数,readonly可以是变量,readonly好比是一个恒定的表达式


    访问符盘点
    private public protected internal的区别
    private 完全私有 子类不能调用
    protected 保护 外界不能调用 可以被继承 所以在子类可以调用
    public 公共 无限制
    internal 同一程序集调用 不能夸程序集而public则可以




    因缘际会的相遇,自当有非同寻常的结局 QQ交流群:110826636
  • 相关阅读:
    android AudioManager类详解
    Android的Media(录音,播放音乐,播放视频等)
    加载大图片报OOM错误
    cmd更换编码类型
    通讯录数据库插入数据
    开机自启加入任务栈
    特殊的广播接收者(界面注册)
    将Image的名字转换为R.image.id
    通过XML文件生成View
    判断某张表是否存在
  • 原文地址:https://www.cnblogs.com/RainbowInTheSky/p/3044014.html
Copyright © 2020-2023  润新知