开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 5476|回复: 4
收起左侧

[C#作业] 第二十二天作业

[复制链接]
发表于 2019-4-29 09:14:03 | 显示全部楼层 |阅读模式   广东省揭阳市

今日主要学习内容

22.1  反射 - Type类的属性成员
22.2  反射 - FieldInfo MethodInfo Memberinfo PropertyInfo
22.3  反射 - ConstructorInfo 动态构建对象
22.4  反射 - 动态给属性字段赋值 动态调用方法
22.5  反射案列 - 通过反射实现多系统数据库的配置

今日作业:

  1. 使用反射机制动态构造下面类的对象并对所有属性字段赋值与读取,动态调用类中所有方法
public class Person {
        //字段 静态字段 有实例字段
        public string test;
        public static string text;
        public string Name { get; set; }

        public string Address { get; set; }

        public int Age { get; set; }

        public Person() {
        }

        public Person(string name, int age) {
            this.Name = name;
            this.Age = age;
        }

        public Person(string name, string address) {
            this.Name = name;
            this.Address = address;
        }

        public Person(string name, int age, string address):this(name,age) {
            this.Address = address;
        }
        public void SayHi() {
            Console.WriteLine($"大家好,我的名字叫{this.Name}");
        }
        public void SayHi(string test)
        {
            Console.WriteLine($"大家好,我的名字叫{this.Name}");
        }
        public void SayHi(string test,int age)
        {
            Console.WriteLine($"大家好,我的名字叫{this.Name}");
        }
    }

发表于 2019-6-28 22:08:47 | 显示全部楼层   湖南省长沙市

使用反射机制动态构造下面类的对象并对所有属性字段赋值与读取,动态调用类中所有方法.

namespace 第二十二课反射 {
    class Program {
        static void Main (string[] args) {
            Type t = typeof(Person);
            ConstructorInfo p1 = t.GetConstructor(new Type[0]);
            Person person1 = p1.Invoke(new object[0]) as Person;

            FieldInfo f1 = t.GetField("test");
            FieldInfo f2 = t.GetField("text");
            PropertyInfo p3 = t.GetProperty("Name");
            PropertyInfo p4 = t.GetProperty("Address");
            PropertyInfo p5 = t.GetProperty("Age");
            f1.SetValue(person1, "测试字段");
            f2.SetValue(person1, "测试静态字段");
            p3.SetValue(person1, "测试1");
            p4.SetValue(person1, "测试2");
            p5.SetValue(person1, 99);
            Console.WriteLine(f1.GetValue(person1));
            Console.WriteLine(f2.GetValue(person1));
            Console.WriteLine(p3.GetValue(person1));
            Console.WriteLine(p4.GetValue(person1));
            Console.WriteLine(p5.GetValue(person1));
            MethodInfo m1 = t.GetMethod("SayHi", new Type[0]);
            MethodInfo m2 = t.GetMethod("SayHi", new Type[] { typeof(string) });
            MethodInfo m3 = t.GetMethod("SayHi", new Type[] { typeof(string), typeof(int) });
            m1.Invoke(person1, new Object[0]);
            m2.Invoke(person1, new Object[1]);            
            m3.Invoke(person1, new Object[2]);
            Console.Read();
        }
    }
}

评分

参与人数 1好评 +1 精币 +10 收起 理由
老郭 + 1 + 10 很赞同,谢谢!

查看全部评分

回复 支持 反对

使用道具 举报

结帖率:100% (1/1)
发表于 2019-6-9 21:43:17 | 显示全部楼层   江苏省南京市
本帖最后由 曜石头 于 2019-6-9 22:07 编辑

static void Main(string[] args)
        {
            Type t = typeof(Person);
            ConstructorInfo p1 = t.GetConstructor(new Type[0]);
            Person person1 = p1.Invoke(new object[0]) as Person;

            FieldInfo f1 = t.GetField("test");
            FieldInfo f2 = t.GetField("text");
            PropertyInfo p3 = t.GetProperty("Name");
            PropertyInfo p4 = t.GetProperty("Address");
            PropertyInfo p5 = t.GetProperty("Age");
            f1.SetValue(person1, "测试字段");
            f2.SetValue(person1, "测试静态字段");
            p3.SetValue(person1, "小仙女");
            p4.SetValue(person1, "火星");
            p5.SetValue(person1, 99);
            Console.WriteLine(f1.GetValue(person1));
            Console.WriteLine(f2.GetValue(person1));
            Console.WriteLine(p3.GetValue(person1));
            Console.WriteLine(p4.GetValue(person1));
            Console.WriteLine(p5.GetValue(person1));
            MethodInfo m1 = t.GetMethod("SayHi", new Type[0]);
            MethodInfo m2 = t.GetMethod("SayHi", new Type[] { typeof(string) });
            MethodInfo m3 = t.GetMethod("SayHi", new Type[] { typeof(string), typeof(int) });
            m1.Invoke(person1, new Object[0]);
            m2.Invoke(person1, new Object[1]);            
            m3.Invoke(person1, new Object[2]);
            Console.Read();
        }
对于属性 字段 定义有疑问
字段=属性? 还是 属性 既有字段也有方法?还是 字段 既有属性也有方法?public string Name { get; set; }这个叫有实例的字段?实现get和set访问器就叫有实例?
IDAL dal = FactoryDbHelper();
dal.Delete("");
这里为什么dal可以直接点Delete

点评

属性的本质就是方法,对于字段来说你无法控制数据的正确性而属性是可以的   广东省揭阳市  发表于 2019-6-14 09:15

评分

参与人数 1好评 +1 精币 +5 收起 理由
老郭 + 1 + 5 很赞同,谢谢!

查看全部评分

回复 支持 反对

使用道具 举报

发表于 2019-4-30 00:04:22 | 显示全部楼层   江苏省扬州市
本帖最后由 qingshanlushui 于 2019-4-30 00:08 编辑

发现语法糖:this(name,age)。。。

           static void Main(string[] args)
        {
            Type t = typeof(Person);
            //无参数的Person
            ConstructorInfo p1 = t.GetConstructor(new Type[0]);
            Person person1 = p1.Invoke(new object[0]) as Person;

            FieldInfo f1 = t.GetField("test");
            f1.SetValue(person1, "settest");
            Console.WriteLine(f1.GetValue(person1));

            FieldInfo f2 = t.GetField("text");
            f2.SetValue(person1, "settext");
            Console.WriteLine(f2.GetValue(person1));

            PropertyInfo f3 = t.GetProperty("Name");
            f3.SetValue(person1, "setName");
            Console.WriteLine(f3.GetValue(person1));

            PropertyInfo f4 = t.GetProperty("Address");
            f4.SetValue(person1, "setAddress");
            Console.WriteLine(f4.GetValue(person1));

            PropertyInfo f5 = t.GetProperty("Age");
            f5.SetValue(person1, 11);
            Console.WriteLine(f5.GetValue(person1));

            MethodInfo m1 = t.GetMethod("SayHi", new Type[0]);
            m1.Invoke(person1, new Object[0]);

            MethodInfo m2 = t.GetMethod("SayHi", new Type[] { typeof(string)});
            m2.Invoke(person1, new Object[1]);

            MethodInfo m3 = t.GetMethod("SayHi", new Type[] { typeof(string), typeof(int) });
            m3.Invoke(person1, new Object[2]);

            //有参数的Person以及SayHi
            ConstructorInfo p2 = t.GetConstructor(new Type[] { typeof(string), typeof(int) });
            Person person2 = p2.Invoke(new object[] { "name2", 12 }) as Person;
            MethodInfo m4 = t.GetMethod("SayHi", new Type[0]);
            m1.Invoke(person2, new Object[0]);

            ConstructorInfo p3 = t.GetConstructor(new Type[] { typeof(string), typeof(string) });
            Person person3 = p3.Invoke(new object[] { "name3", "address3" }) as Person;
            m2.Invoke(person3, new Object[1]);

            ConstructorInfo p4 = t.GetConstructor(new Type[] { typeof(string), typeof(int), typeof(string) });
            Person person4 = p4.Invoke(new object[] { "name4", 17, "address4" }) as Person;
            m3.Invoke(person4, new Object[2]);

            Console.Read();
        }

控制台结果:
settest
settext
setName
setAddress
11
大家好,我的名字叫setName
大家好,我的名字叫setName
大家好,我的名字叫setName
大家好,我的名字叫name2
大家好,我的名字叫name3
大家好,我的名字叫name4


回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表