zoukankan      html  css  js  c++  java
  • 补充一个技术文章吧,否则真的对不起博客园

            /// <summary> 较安全的返回指定属性的值 </summary>
            
    /// <remarks>
            
    /// 例如,如果你想获得Company的CompanyCode的DefaultCode属性,可以使用
            
    /// <code>
            
    /// object code = _company.SafeGetValue("CompanyCode.DefaultCode");
            
    /// </code>
            
    /// 但注意:
            
    ///        传入的参数(属性名)不允许书写错误,在此方法中不对此进行检查;
            
    /// </remarks>

            public object SafeGetValue(string propertyName)
            
    {
                
    string[] props = propertyName.Split('.');
                
    string propName;
                PropertyInfo propInfo;
                
    object obj = this;
                Type objType;

                
    for (int i = 0; i < props.Length; i++)
                
    {
                    objType 
    = obj.GetType();

                    propName 
    = props[i];
                    
    if (propName.Trim().Length == 0)
                        
    throw new ArgumentException("propertyName");

                    
    try
                    
    {
                        propInfo 
    = objType.GetProperty(propName);
                        
    if (propInfo == null)
                            
    throw new ArgumentException("propertyName");

                        obj 
    = propInfo.GetValue(obj, null);
                    }

                    
    catch (AmbiguousMatchException)
                    
    {
                        PropertyDescriptorCollection propDescs 
    = TypeDescriptor.GetProperties(objType);
                        PropertyDescriptor propDesc 
    = propDescs.Find(propName, false);
                        
    if (propDesc == null)
                            
    throw new ArgumentException("propertyName");
                        
    else
                        
    {
                            obj 
    = propDesc.GetValue(obj);
                        }

                    }


                    
    if (obj == null)
                        
    return null;
                }

                
    return obj;
            }
  • 相关阅读:
    20165311《网络对抗技术》Exp1 PC平台逆向破解
    20165311 《网络对抗技术》 Kali安装
    2018-2019-1 20165307 20165327 20165332 实验五 通讯协议设计
    2018-2019-1 20165307 20165327 20165332 实验四 外设驱动程序设计
    2018-2019-1 20165307 20165327 20165332 实验三 并发程序
    2018-2019-1 20165307 20165327 20165332 实验二 固件程序设计
    2018-2019-1 20165307 20165327 20165332 实验一 开发环境的熟悉
    2018-2019-1 20165307 《信息安全系统设计基础》第4周学习总结
    2018-2019-1 20165307 《信息安全系统设计基础》第3周学习总结
    20165307 《信息安全系统设计基础》 第一周
  • 原文地址:https://www.cnblogs.com/tansm/p/83582.html
Copyright © 2011-2022 走看看