• 反射的常见应用


    /*
     *
     * 创建人:李林峰
     *
     * 时  间:2012-7-23
     *
     * 描  述:应用程序入口
     *
     */
    using System.Reflection;//反射的名称空间
    using CSharp.Model;     //实体名称空间
    namespace CSharp.Reflection
    {
        class Program
        {
            static void Main(string[] args)
            {
                Employee employee = (Employee)Assembly.Load(Constant.ASSEMBLYNAME).CreateInstance("CSharp.Model.Employee");
                employee.ID = 1;
                employee.Name = "李林峰";
                employee.Department = "技术";
                employee.Position = "程序员";
                System.Console.WriteLine(employee.Name);
                System.Console.WriteLine(employee.Department);
                System.Console.WriteLine(employee.Position);
                System.Console.WriteLine(employee.Method());
                System.Console.WriteLine("---------------------------------");
                Employee employeeNew = (Employee)Assembly.LoadFile(@"E:公司内网HZYT.Test6 C#映射教程ClassThreeCSharp.ReflectioninDebugCSharp.Model.dll").CreateInstance("CSharp.Model.Employee");
                employee.ID = 2;
                employeeNew.Name = "李林峰";
                employeeNew.Department = "技术";
                employeeNew.Position = "程序员";
                System.Console.WriteLine(employeeNew.Name);
                System.Console.WriteLine(employeeNew.Department);
                System.Console.WriteLine(employeeNew.Position);
                System.Console.WriteLine(employee.Method());
            }
        }
    }
     
    #引用的类库函数如下
    /*
     *
     * 创建人:李林峰
     *
     * 时  间:2012-7-23
     *
     * 描  述:反射类的实例
     *
     */
    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace CSharp.Model
    {
        /// <summary>
        /// 员工类
        /// </summary>
        public class Employee
        {
            public int ID { get; set; }             //编号
            public string Name { get; set; }        //姓名
            public string Password { get; set; }    //密码
            public string Department { get; set; }  //部门
            public string Position { get; set; }    //职位
            /// <summary>
            /// 测试方法
            /// </summary>
            /// <returns></returns>
            public string Method()
            {
                return this.ID.ToString();
            }
        }
    }
     
    转自 :http://www.cnblogs.com/iamlilinfeng 多谢李林峰大佬
    反射的两种常见应用,记录一下
  • 相关阅读:
    .XamlReader.Load()解决给定编码中的字符无效与无法创建未知类型
    ie中jQuery无法解析xml文件的解决方案[转帖]
    关于BI
    关天response.setContentType()和application/json
    win7 exe程序icon消失
    关于spring的p标签(转)
    人生的阶段(转)
    Firefox高版本中alert样式问题
    IDM606注册
    Java 线程池
  • 原文地址:https://www.cnblogs.com/yangzh666/p/11471343.html
Copyright © 2020-2023  润新知