.net反射技术封装类(1)

文章摘要:如果有人问你,如何调用一个类的private的方法或访问一个类的私有成员,如果你不知道反射的话,你会告诉他不可以。但是有了反射,这一切都成为可能。我有时候会想,既然使用private来限制访问,为什么又要用反射去破坏这种限制呢?也许可以通过侧面来解释这个问题,如果你要维护类的封装性,那请不要用反射,反射会破坏类的封装性。 但反正某些情况下,又会变得相当地有用,比 һϢϵͳ








һϢϵͳܱǸύлʵкϵͳĹؼʻIPܵ˷ƣβЧϵͳѼ¼IPύݡע⣬ҪύκΥҹ涨ݣصϢΪgooglesyndication
uterType.Module;

return typeModule.GetType(hiddenTypeName);

}

public static MethodInfo GetMethod(Type type, string funcName, Type[] paramTypes)

{

if (paramTypes != null)

return type.GetMethod(funcName, flags, null, paramTypes, null);

else

return type.GetMethod(funcName, flags);

}

public static MethodInfo GetMethod(Type type, string funcName)

{

return GetMethod(type, funcName, null);

}

public static object CallMethod(object obj, string funcName, params object[] parameters)

{

Type type = obj.GetType();

MethodInfo method = GetMethod(type, funcName, GetTypesFromObjects(parameters));

return method.Invoke(obj, parameters);

}

public static object CallStaticMethod(Type type, string funcName, params object[] parameters)

{

MethodInfo method = GetMethod(type, funcName, GetTypesFromObjects(parameters));

return method.Invoke(null, parameters);

}

public static object GetProperty(object obj, string propertyName)

{

return GetProperty(obj, propertyName, null);

}

public static object GetProperty(object obj, string propertyName, params object[] index)

{

Type type = obj.GetType();

return type.GetProperty(propertyName, flags).GetValue(obj, index);

}

public static object GetStaticProperty(Type type, string propertyName)

{

return GetStaticProperty(type, propertyName, null);

}

public static object GetStaticProperty(Type type, string propertyName, params object[] index)

{

return type.GetProperty(propertyName, flags).GetValue(null, index);

}

public static void SetProperty(object obj, string propertyName, object value)

{

SetProperty(obj, propertyName, value, null);

}

public static void SetProperty(object obj, string propertyName, object value, params object[] index)

{

Type type = obj.GetType();

type.GetProperty(propertyName, flags).SetValue(obj, value һϢϵͳ









һϢϵͳܱǸύлʵкϵͳĹؼʻIPܵ˷ƣβЧϵͳѼ¼IPύݡע⣬ҪύκΥҹ涨ݣصϢΪgooglesyndication
һϢϵͳ








һϢϵͳܱǸύлʵкϵͳĹؼʻIPܵ˷ƣβЧϵͳѼ¼IPύݡע⣬ҪύκΥҹ涨ݣصϢΪgooglesyndication
  • something about Listeners
  • 在windows任务栏系统托盘区显示硬盘工作状态
  • delphi的面向对象编程入门教程
  • Adobe 再曝新漏洞 专补工具帮忙打补丁
  • js将英文转成数字实现加密
  • 善用Unix系统中LVM硬盘管理