zoukankan      html  css  js  c++  java
  • c# 反射访问类成员

    using System.Reflection;
    
                bool exist = false;
                //实体对象
                object myResult = null;
                int _index = path.IndexOf("Combine");
                foreach (object obj in objList)
                {
                    if ((obj as RdoObject).rosPath == path)
                    {
                        exist = true;
                        //(obj as RdoObject).Load();
                        return (obj as RdoObject); //存在就返回
                    }
                }
                Assembly myAssembly = Assembly.GetExecutingAssembly();
                string _class = "";
                if (exist == false)
                {
                    if (_index < 0)
                    {
                        _class = "OMNI.Conf.RDO.RdoObject";
                    }
                    else
                    {
    
                        _class = path.Substring(9, path.Length - 10);
                        _class = "OMNI.Conf.PLU." + _class;
    
                    }
                    Type myType = Type.GetType(_class);
                    //抽象类
                    myResult = myAssembly.CreateInstance(_class);
                    PropertyInfo property1 = myType.GetProperty("rosPath");
                    property1.SetValue(myResult, path, null);//给对应属性赋值
    
                    PropertyInfo property2 = myType.GetProperty("con");
                    property2.SetValue(myResult, _con, null);//给对应属性赋值
    
                    MethodInfo method = myType.GetMethod("Load");
                    method.Invoke(myResult, null);
    
                    objList.Add(myResult);
                    return myResult;
                }
                else
                {
                    return myResult;
                }
     
    

      

  • 相关阅读:
    AJAX基础
    DOM事件机制(事件捕获和事件冒泡和事件委托)
    http协议
    DOM实战-js todo
    python常见面试题
    jQuery快速入门
    三级菜单
    购物车
    员工信息表
    random模块
  • 原文地址:https://www.cnblogs.com/saturn/p/2657304.html
Copyright © 2011-2022 走看看