一个很好用的C#反射操作类 [好东西不敢私有]
今天在做项目的时候需要反射来创建类,并调用其方式,这个类很不错,一直在用,贴出来供分享.
/// <summary> /// 获取用户数据库分页数据 /// </summary> /// <param name="TableName"></param> /// <param name="PageSize"></param> /// <param name="PageIndex"></param> /// <param name="SqlWhere"></param> /// <returns></returns> public DataSet GetList( string TableName, int PageSize, int PageIndex, string SqlWhere) { try { Assembly a = ReflectionUtil.LoadAssembly(Globals.UserDATA_Assembly + ".BLL" ); //创建一个程序集 Type t = ReflectionUtil.GetType(a, Globals.UserDATA_Assembly + ".BLL." + TableName + "BLL" ); object o = ReflectionUtil.CreateInstance(t, null ); Type[] types = new Type[3]; types[0] = Type.GetType( "System.Int32" ); types[1] = Type.GetType( "System.Int32" ); types[2] = Type.GetType( "System.String" ); MethodInfo mi = t.GetMethod( "GetList" , types); Object[] obj = new Object[3]; obj[0] = PageSize; obj[1] = PageIndex; obj[2] = SqlWhere; DataSet ds = (DataSet)mi.Invoke(o, obj); return ds; } catch { return null ; } } |
类在这里
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.ComponentModel; namespace XINLG.Labs.Utils { /// <summary> /// 反射操作类 /// </summary> public class ReflectionUtil { #region 加载程序集 /// <summary> /// 加载程序集 /// </summary> /// <param name="assemblyName">程序集名称,不要加上程序集的后缀,如.dll</param> public static Assembly LoadAssembly( string assemblyName) { try { return Assembly.Load(assemblyName); } catch ( Exception ex ) { string errMsg = ex.Message; return null ; } } #endregion #region 获取程序集中的类型 /// <summary> /// 获取本地程序集中的类型 /// </summary> /// <param name="typeName">类型名称,范例格式:"命名空间.类名",类型名称必须在本地程序集中</param> public static Type GetType( string typeName ) { try { return Type.GetType( typeName ); } catch ( Exception ex ) { string errMsg = ex.Message; return null ; } } /// <summary> /// 获取指定程序集中的类型 /// </summary> /// <param name="assembly">指定的程序集</param> /// <param name="typeName">类型名称,范例格式:"命名空间.类名",类型名称必须在assembly程序集中</param> /// <returns></returns> public static Type GetType( Assembly assembly , string typeName ) { try { return assembly.GetType( typeName ); } catch ( Exception ex ) { string errMsg = ex.Message; return null ; } } #endregion #region 动态创建对象实例 /// <summary> /// 创建类型的实例 /// </summary> /// <param name="type">类型</param> /// <param name="parameters">传递给构造函数的参数</param> public static object CreateInstance( Type type, params object [] parameters ) { return Activator.CreateInstance( type, parameters ); } /// <summary> /// 创建类的实例 /// </summary> /// <param name="className">类名,格式:"命名空间.类名"</param> /// <param name="parameters">传递给构造函数的参数</param> public static object CreateInstance( string className, params object [] parameters ) { try { //获取类型 Type type = GetType( className ); //类型为空则返回 if ( type == null ) { return null ; } return CreateInstance( type, parameters ); } catch ( Exception ex ) { string errMsg = ex.Message; return null ; } } #endregion #region 获取类的命名空间 /// <summary> /// 获取类的命名空间 /// </summary> /// <typeparam name="T">类名或接口名</typeparam> public static string GetNamespace<T>() { return typeof ( T ).Namespace; } #endregion #region 设置成员的值 #region 设置属性值 /// <summary> /// 将值装载到目标对象的指定属性中 /// </summary> /// <param name="target">要装载数据的目标对象</param> /// <param name="propertyName">目标对象的属性名</param> /// <param name="value">要装载的值</param> public static void SetPropertyValue( object target, string propertyName, object value ) { PropertyInfo propertyInfo = target.GetType().GetProperty( propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy ); SetValue( target, propertyInfo, value ); } #endregion #region 设置成员的值 /// <summary> /// 设置成员的值 /// </summary> /// <param name="target">要装载数据的目标对象</param> /// <param name="memberInfo">目标对象的成员</param> /// <param name="value">要装载的值</param> private static void SetValue( object target, MemberInfo memberInfo, object value ) { if ( value != null ) { //获取成员类型 Type pType; if ( memberInfo.MemberType == MemberTypes.Property ) pType = ( (PropertyInfo)memberInfo ).PropertyType; else pType = ( (FieldInfo)memberInfo ).FieldType; //获取值的类型 Type vType = GetPropertyType( value.GetType() ); //强制将值转换为属性类型 value = CoerceValue( pType, vType, value ); } if ( memberInfo.MemberType == MemberTypes.Property ) ( (PropertyInfo)memberInfo ).SetValue( target, value, null ); else ( (FieldInfo)memberInfo ).SetValue( target, value ); } #endregion #region 强制将值转换为指定类型 /// <summary> /// 强制将值转换为指定类型 /// </summary> /// <param name="propertyType">目标类型</param> /// <param name="valueType">值的类型</param> /// <param name="value">值</param> private static object CoerceValue( Type propertyType, Type valueType, object value ) { //如果值的类型与目标类型相同则直接返回,否则进行转换 if ( propertyType.Equals( valueType ) ) { return value; } else { if ( propertyType.IsGenericType ) { if ( propertyType.GetGenericTypeDefinition() == typeof ( Nullable<> ) ) { if ( value == null ) return null ; else if ( valueType.Equals( typeof ( string ) ) && ( string )value == string .Empty ) return null ; } propertyType = GetPropertyType( propertyType ); } if ( propertyType.IsEnum && valueType.Equals( typeof ( string ) ) ) return Enum.Parse( propertyType, value.ToString() ); if ( propertyType.IsPrimitive && valueType.Equals( typeof ( string ) ) && string .IsNullOrEmpty( ( string )value ) ) value = 0; try { return Convert.ChangeType( value, GetPropertyType( propertyType ) ); } catch ( Exception ex ) { TypeConverter cnv = TypeDescriptor.GetConverter( GetPropertyType( propertyType ) ); if ( cnv != null && cnv.CanConvertFrom( value.GetType() ) ) return cnv.ConvertFrom( value ); else throw ex; } } } #endregion #region 获取类型,如果类型为Nullable<>,则返回Nullable<>的基础类型 /// <summary> /// 获取类型,如果类型为Nullable(of T),则返回Nullable(of T)的基础类型 /// </summary> /// <param name="propertyType">需要转换的类型</param> private static Type GetPropertyType( Type propertyType ) { Type type = propertyType; if ( type.IsGenericType && ( type.GetGenericTypeDefinition() == typeof ( Nullable<> ) ) ) return Nullable.GetUnderlyingType( type ); return type; } #endregion #endregion } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库