惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
Y
Y Combinator Blog
博客园 - 【当耐特】
V
Visual Studio Blog
GbyAI
GbyAI
V
V2EX
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
A
About on SuperTechFans
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
量子位
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件

博客园 - 索马

线程内键盘钩子实现Winforn快捷键(组合键) Win32 API对文本框发送消息(多个文本Edit,动态 控件 ID) 一个项目同时支持VS2005和VS2008打开 (原创)代码生成器让你抓狂,结束你选择,就是如此简单! RadControls集成jquery,随便几行代码吊死你!不信来看! - 索马 - 博客园 RadGrid客户端绑定,分页,过滤,效率真不错 RadInputManager验证效果,很酷哦 RadControl中的radalert以后调用js函数啊 RadComboBox中的RadTreeView父节点不选择,子节点可以选择 - 索马 - 博客园 仿腾讯留言效果(拖动分页) RadComboBox中的RadGrid分页 漂亮的Alert 消息框! Java调用.net的web service - 索马 - 博客园 WCF传输大数据量DataSet sql获取表,字段,长度,类型,描述,等详细信息 .net根据模板生成Word文件 生成C#执行指定存储过程的参数 CheckBox单选 SQL SERVER 动态游标
反射常用功能-持续更新
索马 · 2011-02-12 · via 博客园 - 索马

反射判断 某个对象 是否 是泛型集合的类型

 public bool isCollection(object o) 
    { 
        
return typeof(ICollection).IsAssignableFrom(o.GetType()) 
            
|| typeof(ICollection<>).IsAssignableFrom(o.GetType()); 
    } 

 获取类实例

        public static object Load(string assembly, string className)
        {
            
return Assembly.Load(assembly).CreateInstance(className);
        }

获取指定属性

代码

        public static PropertyInfo GetPropertyByName(Type type, string propertyName)
        {
            
return type.GetProperty(propertyName, BindingFlags.Public
                
| BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy);
        }

 反射创建泛型集合

代码

        public static IList CreateList(Type listType)
        {
            
//Type type = listType;
            
//反射创建普通对象
            object o = Activator.CreateInstance(listType);
            
//反射创建泛型集合
            Type generic = typeof(List<>);
            Type[] typeArgs 
= { listType };
            generic 
= generic.MakeGenericType(typeArgs);
            
return Activator.CreateInstance(generic) as IList;
        }

反射调用方法

代码

                    Type dataType = Type.GetType(ClassName);
                    MethodInfo parseMethod 
= dataType.GetMethod("Parse", BindingFlags.Public | BindingFlags.IgnoreCase
                        
| BindingFlags.Static | BindingFlags.FlattenHierarchy, nullnew Type[] { typeof(string) }, null);

 反射获取某个属性集合(如获取DataTable的columns属性,并且遍历它)

                DataTable dtSource = (DataTable)data;
                Type type 
= dtSource.GetType();
                PropertyInfo pi 
= type.GetProperty("Columns");
                IEnumerable listObject 
= (IEnumerable)pi.GetValue(dtSource, null); 

 反射设置属性

        public static void SetPropertyValue(this PropertyInfo property, object target, object value)
        {
            
if (null != property && property.CanWrite)
            {
                
if (value.GetType() == typeof(string))
                {
                    
// 对换行符进行转换
                    value = value.ToString().Replace("\\r""\r").
                        Replace(
"\\n""\n").Replace("<br>""\n");
                }
                value 
= GetObjectInstance(property.PropertyType, value.ToString());
                property.SetValue(target, value, 
null);
            }
        }