C#反射实体类
来源:互联网 发布:血手幽灵数据共享平台 编辑:程序博客网 时间:2024/05/16 10:57
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Reflection;
- namespace EasySRCoreClass.Component.UtilComponent
- {
- public class ObjectMethod
- {
- private Type mytype;
- private Object obj;
- public ObjectMethod(Object obj)
- {
- this.obj = obj;
- mytype = obj.GetType();
- }
-
- public void SetPropertyValue(string name, Object value)
- {
- PropertyInfo[] pis = mytype.GetProperties();
- for (int i = 0; i < pis.Length; i++)
- {
- PropertyInfo pi = pis[i];
- if (pi.Name.Equals(name))
- {
- pi.SetValue(obj, value, null);
- break;
- }
- }
- }
-
- public Object GetPropertyValue(string name)
- {
- Object result = null;
- PropertyInfo[] pis = mytype.GetProperties();
- for (int i = 0; i < pis.Length; i++)
- {
- PropertyInfo pi = pis[i];
- if (pi.Name.Equals(name))
- {
- result = pi.GetValue(obj, null);
- break;
- }
- }
- return result;
- }
- public Object InvokeMethod(String method)
- {
- return InvokeMethod(method, null);
- }
-
- public Object InvokeMethod(String method, Object[] paras)
- {
- Object result = null;
- MethodInfo[] mis = mytype.GetMethods();
- for (int i = 0; i < mis.Length; i++)
- {
- MethodInfo mi = mis[i];
- if (mi.Name.Equals(method))
- {
- result = mi.Invoke(obj, paras);
- break;
- }
- }
- return result;
- }
- }
- }