2024年1月25日发(作者:)
using System;
using tion;
using ces;
using g;
using ;
using ;
using entModel;
namespace s
{
///
/// 反射操作辅助类,如获取或设置字段、属性的值等反射信息。
///
public sealed class ReflectionUtil
{
private ReflectionUtil()
{
}
#region 属性字段设置
public static BindingFlags bf = edOnly | |
lic | ce | ;
public static obxxxxject InvokeMethod(obxxxxject obj string methodName obxxxxject[] args)
{
obxxxxject objReturn = null;
Type type = e();
objReturn = Member(methodName bf | Method null obj
args);
return objReturn;
}
public static void SetField(obxxxxject obj string name obxxxxject value)
{
FieldInfo fi = e().GetField(name bf);
ue(obj value);
}
public static obxxxxject GetField(obxxxxject obj string name)
{
FieldInfo fi = e().GetField(name bf);
return ue(obj);
}
public static FieldInfo[] GetFields(obxxxxject obj)
{
FieldInfo[] fieldInfos = e().GetFields(bf);
return fieldInfos;
}
public static void SetProperty(obxxxxject obj string name obxxxxject value)
{
PropertyInfo fieldInfo = e().GetProperty(name bf);
value = Type(value tyType);
ue(obj value null);
}
public static obxxxxject GetProperty(obxxxxject obj string name)
{
PropertyInfo fieldInfo = e().GetProperty(name bf);
return ue(obj null);
}
public static PropertyInfo[] GetProperties(obxxxxject obj)
{
PropertyInfo[] propertyInfos = e().GetProperties(bf);
return propertyInfos;
}
#endregion
#region 获取Descxxxxription
///
/// Get The Member Descxxxxription using Descxxxxription Attribute.
///
///
/// Get The Enum Field Descxxxxription using Descxxxxription Attribute.
///
/// The value.
///
public static string GetDescxxxxription(Enum value)
{
return GetDescxxxxription(value null);
}
///
/// Get The Enum Field Descxxxxription using Descxxxxription Attribute and
/// obxxxxjects to format the Descxxxxription.
///
/// Enum For Which descxxxxription is required.
/// An obxxxxject array containing zero or more obxxxxjects to
format.
///
descxxxxription
public static string GetDescxxxxription(Enum value params obxxxxject[] args)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
string text1;
FieldInfo fi = e().GetField(ng());
DescxxxxriptionAttribute[] attributes =
(DescxxxxriptionAttribute[])tomAttributes(typeof(DescxxxxriptionAttribute)
false);
text1 = ( > 0) ? attributes[0].Descxxxxription : ng();
if ((args != null) && ( > 0))
{
return (null text1 args);
}
return text1;
}
///
/// Get The Type Descxxxxription using Descxxxxription Attribute.
///
/// Specified Member for which Info is Required
///
descxxxxription
public static string GetDescxxxxription(MemberInfo member)
{
return GetDescxxxxription(member null);
}
///
/// Get The Type Descxxxxription using Descxxxxription Attribute and
/// obxxxxjects to format the Descxxxxription.
///
/// Specified Member for which Info is Required
/// An obxxxxject array containing zero or more obxxxxjects to
format.
///
/// not found or return type descxxxxription
public static string GetDescxxxxription(MemberInfo member params obxxxxject[] args)
{
string text1;
if (member == null)
{
throw new ArgumentNullException("member");
}
if (ned(typeof(DescxxxxriptionAttribute) false))
{
DescxxxxriptionAttribute[] attributes =
(DescxxxxriptionAttribute[])tomAttributes(typeof(DescxxxxriptionAttribute) false);
text1 = attributes[0].Descxxxxription;
}
else
{
return ;
}
if ((args != null) && ( > 0))
{
return (null text1 args);
}
return text1;
}
#endregion
#region 获取Attribute信息
///
/// Gets the specified obxxxxject attributes
///
///
/// Gets the specified obxxxxject attributes for assembly as specified by type
///
/// The attribute Type for which the custom attributes are to be
returned.
/// the assembly in which the specified attribute is defined
///
public static obxxxxject GetAttribute(Type attributeType Assembly assembly)
{
if (attributeType == null)
{
throw new ArgumentNullException("attributeType");
}
if (assembly == null)
{
throw new ArgumentNullException("assembly");
}
if (ned(attributeType false))
{
obxxxxject[] attributes = tomAttributes(attributeType false);
return attributes[0];
}
return null;
}
///
/// Gets the specified obxxxxject attributes for type as specified by type
///
/// The attribute Type for which the custom attributes are to be
returned.
/// the type on which the specified attribute is defined
///
public static obxxxxject GetAttribute(Type attributeType MemberInfo type)
{
return GetAttribute(attributeType type false);
}
///
/// Gets the specified obxxxxject attributes for type as specified by type with option to serach
parent
///
/// The attribute Type for which the custom attributes are to be
returned.
/// the type on which the specified attribute is defined
/// if set to
///
/// Attribute as obxxxxject or null if not found.
///
public static obxxxxject GetAttribute(Type attributeType MemberInfo type bool searchParent)
{
if (attributeType == null)
{
return null;
}
if (type == null)
{
return null;
}
if (!(lassOf(typeof(Attribute))))
{
return null;
}
if (ned(attributeType searchParent))
{
obxxxxject[] attributes = tomAttributes(attributeType searchParent);
if ( > 0)
{
return attributes[0];
}
}
return null;
}
///
/// Gets the collection of all specified obxxxxject attributes for type as specified by type
///
/// The attribute Type for which the custom attributes are to be
returned.
/// the type on which the specified attribute is defined
///
public static obxxxxject[] GetAttributes(Type attributeType MemberInfo type)
{
return GetAttributes(attributeType type false);
}
///
/// Gets the collection of all specified obxxxxject attributes for type as specified by type with
option to serach parent
///
/// The attribute Type for which the custom attributes are to be
returned.
/// the type on which the specified attribute is defined
/// The attribute Type for which the custom attribute is to be
returned.
///
/// Attribute as obxxxxject or null if not found.
///
public static obxxxxject[] GetAttributes(Type attributeType MemberInfo type bool
searchParent)
{
if (type == null)
{
return null;
}
if (attributeType == null)
{
return null;
}
if (!(lassOf(typeof(Attribute))))
{
return null;
}
if (ned(attributeType false))
{
return tomAttributes(attributeType searchParent);
}
return null;
}
#endregion
#region 资源获取
///
/// 根据资源名称获取图片资源流
///
///
///
public static Stream GetImageResource(string ResourceName)
{
Assembly asm = cutingAssembly();
return ifestResourceStream(ResourceName);
}
///
/// 获取程序集资源的位图资源
///
/// 程序集中的某一对象类型
/// 资源的根名称。例如,名为“ces”的资源文件的根名称为“MyResource”。
/// 资源项名称
public static Bitmap LoadBitmap(Type assemblyType string resourceHolder string imageName)
{
Assembly thisAssembly = embly(assemblyType);
ResourceManager rm = new ResourceManager(resourceHolder thisAssembly);
return (Bitmap)xxxject(imageName);
}
///
/// 获取程序集资源的文本资源
///
/// 程序集中的某一对象类型
/// 资源项名称
/// 资源的根名称。例如,名为“ces”的资源文件的根名称为“MyResource”。
public static string GetStringRes(Type assemblyType string resName string resourceHolder)
{
Assembly thisAssembly = embly(assemblyType);
ResourceManager rm = new ResourceManager(resourceHolder thisAssembly);
return ing(resName);
}
///
/// 获取程序集嵌入资源的文本形式
///
/// 程序集中的某一对象类型
/// 字符集编码
/// 嵌入资源相对路径
///
public static string GetManifestString(Type assemblyType string charset string ResName)
{
Assembly asm = embly(assemblyType);
Stream st = ifestResourceStream((ace
"." e("/" ".")));
if (st == null) { return ""; }
int iLen = (int);
byte[] bytes = new byte[iLen];
(bytes 0 iLen);
return (bytes != null) ? oding(charset).GetString(bytes) : "";
}
#endregion
#region 创建对应实例
///
/// 创建对应实例
///
/// 类型
///
public static obxxxxject CreateInstance(string type)
{
Type tmp = null;
Assembly[] assemblies = emblies();
for (int i = 0; i < ; i++)
{
tmp = assemblies[i].GetType(type);
if (tmp != null)
{
return assemblies[i].CreateInstance(type);
}
}
return null;
//return cutingAssembly().CreateInstance(type);
}
///
/// 创建对应实例
///
/// 类型
///
public static obxxxxject CreateInstance(Type type)
{
return CreateInstance(me);
}
#endregion
}
}
发布评论