C# NET反射操作辅助类如获取或设置字段属性的值等反射信息.docx
《C# NET反射操作辅助类如获取或设置字段属性的值等反射信息.docx》由会员分享,可在线阅读,更多相关《C# NET反射操作辅助类如获取或设置字段属性的值等反射信息.docx(15页珍藏版)》请在冰豆网上搜索。
C#NET反射操作辅助类如获取或设置字段属性的值等反射信息
usingSystem;
usingSystem.Reflection;
usingSystem.Resources;
usingSystem.Drawing;
usingSystem.IO;
usingSystem.Text;
usingSystem.ComponentModel;
namespaceWHC.OrderWater.Commons
{
///
///反射操作辅助类,如获取或设置字段、属性的值等反射信息。
///
publicsealedclassReflectionUtil
{
privateReflectionUtil()
{
}
#region属性字段设置
publicstaticBindingFlagsbf=BindingFlags.DeclaredOnly|BindingFlags.Public|
BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.Static;
publicstaticobxxxxjectInvokeMethod(obxxxxjectobjstringmethodNameobxxxxject[]args)
{
obxxxxjectobjReturn=null;
Typetype=obj.GetType();
objReturn=type.InvokeMember(methodNamebf|BindingFlags.InvokeMethodnullobjargs);
returnobjReturn;
}
publicstaticvoidSetField(obxxxxjectobjstringnameobxxxxjectvalue)
{
FieldInfofi=obj.GetType().GetField(namebf);
fi.SetValue(objvalue);
}
publicstaticobxxxxjectGetField(obxxxxjectobjstringname)
{
FieldInfofi=obj.GetType().GetField(namebf);
returnfi.GetValue(obj);
}
publicstaticFieldInfo[]GetFields(obxxxxjectobj)
{
FieldInfo[]fieldInfos=obj.GetType().GetFields(bf);
returnfieldInfos;
}
publicstaticvoidSetProperty(obxxxxjectobjstringnameobxxxxjectvalue)
{
PropertyInfofieldInfo=obj.GetType().GetProperty(namebf);
value=Convert.ChangeType(valuefieldInfo.PropertyType);
fieldInfo.SetValue(objvaluenull);
}
publicstaticobxxxxjectGetProperty(obxxxxjectobjstringname)
{
PropertyInfofieldInfo=obj.GetType().GetProperty(namebf);
returnfieldInfo.GetValue(objnull);
}
publicstaticPropertyInfo[]GetProperties(obxxxxjectobj)
{
PropertyInfo[]propertyInfos=obj.GetType().GetProperties(bf);
returnpropertyInfos;
}
#endregion
#region获取Descxxxxription
///
///GetTheMemberDescxxxxriptionusingDescxxxxriptionAttribute.
///
///
///GetTheEnumFieldDescxxxxriptionusingDescxxxxriptionAttribute.
///
///Thevalue.
///returndescxxxxriptionorvalue.ToString()
publicstaticstringGetDescxxxxription(Enumvalue)
{
returnGetDescxxxxription(valuenull);
}
///
///GetTheEnumFieldDescxxxxriptionusingDescxxxxriptionAttributeand
///obxxxxjectstoformattheDescxxxxription.
///
///EnumForWhichdescxxxxriptionisrequired.
///Anobxxxxjectarraycontainingzeroormoreobxxxxjectstoformat.
///returnnullifDescxxxxriptionAttributeisnotfoundorreturntypedescxxxxription
publicstaticstringGetDescxxxxription(Enumvalueparamsobxxxxject[]args)
{
if(value==null)
{
thrownewArgumentNullException("value");
}
stringtext1;
FieldInfofi=value.GetType().GetField(value.ToString());
DescxxxxriptionAttribute[]attributes=
(DescxxxxriptionAttribute[])fi.GetCustomAttributes(typeof(DescxxxxriptionAttribute)false);
text1=(attributes.Length>0)?
attributes[0].Descxxxxription:
value.ToString();
if((args!
=null)&&(args.Length>0))
{
returnstring.Format(nulltext1args);
}
returntext1;
}
///
///GetTheTypeDescxxxxriptionusingDescxxxxriptionAttribute.
///
///SpecifiedMemberforwhichInfoisRequired
///returnnullifDescxxxxriptionAttributeisnotfoundorreturntypedescxxxxription
publicstaticstringGetDescxxxxription(MemberInfomember)
{
returnGetDescxxxxription(membernull);
}
///
///GetTheTypeDescxxxxriptionusingDescxxxxriptionAttributeand
///obxxxxjectstoformattheDescxxxxription.
///
///SpecifiedMemberforwhichInfoisRequired
///Anobxxxxjectarraycontainingzeroormoreobxxxxjectstoformat.
///returnifDescxxxxriptionAttributeis
///notfoundorreturntypedescxxxxription
publicstaticstringGetDescxxxxription(MemberInfomemberparamsobxxxxject[]args)
{
stringtext1;
if(member==null)
{
thrownewArgumentNullException("member");
}
if(member.IsDefined(typeof(DescxxxxriptionAttribute)false))
{
DescxxxxriptionAttribute[]attributes=
(DescxxxxriptionAttribute[])member.GetCustomAttributes(typeof(DescxxxxriptionAttribute)false);
text1=attributes[0].Descxxxxription;
}
else
{
returnString.Empty;
}
if((args!
=null)&&(args.Length>0))
{
returnString.Format(nulltext1args);
}
returntext1;
}
#endregion
#region获取Attribute信息
///
///Getsthespecifiedobxxxxjectattributes
///
///
///Getsthespecifiedobxxxxjectattributesforassemblyasspecifiedbytype
///
///TheattributeTypeforwhichthecustomattributesaretobereturned.
///theassemblyinwhichthespecifiedattributeisdefined
///Attributeasobxxxxjectornullifnotfound.
publicstaticobxxxxjectGetAttribute(TypeattributeTypeAssemblyassembly)
{
if(attributeType==null)
{
thrownewArgumentNullException("attributeType");
}
if(assembly==null)
{
thrownewArgumentNullException("assembly");
}
if(assembly.IsDefined(attributeTypefalse))
{
obxxxxject[]attributes=assembly.GetCustomAttributes(attributeTypefalse);
returnattributes[0];
}
returnnull;
}
///
///Getsthespecifiedobxxxxjectattributesfortypeasspecifiedbytype
///
///TheattributeTypeforwhichthecustomattributesaretobereturned.
///thetypeonwhichthespecifiedattributeisdefined
///Attributeasobxxxxjectornullifnotfound.
publicstaticobxxxxjectGetAttribute(TypeattributeTypeMemberInfotype)
{
returnGetAttribute(attributeTypetypefalse);
}
///
///Getsthespecifiedobxxxxjectattributesfortypeasspecifiedbytypewithoptiontoserachparent
///
///TheattributeTypeforwhichthecustomattributesaretobereturned.
///thetypeonwhichthespecifiedattributeisdefined
///ifsetto[searchparent].
///
///Attributeasobxxxxjectornullifnotfound.
///
publicstaticobxxxxjectGetAttribute(TypeattributeTypeMemberInfotypeboolsearchParent)
{
if(attributeType==null)
{
returnnull;
}
if(type==null)
{
returnnull;
}
if(!
(attributeType.IsSubclassOf(typeof(Attribute))))
{
returnnull;
}
if(type.IsDefined(attributeTypesearchParent))
{
obxxxxject[]attributes=type.GetCustomAttributes(attributeTypesearchParent);
if(attributes.Length>0)
{
returnattributes[0];
}
}
returnnull;
}
///
///Getsthecollectionofallspecifiedobxxxxjectattributesfortypeasspecifiedbytype
///
///TheattributeTypeforwhichthecustomattributesaretobereturned.
///thetypeonwhichthespecifiedattributeisdefined
///Attributeasobxxxxjectornullifnotfound.
publicstaticobxxxxject[]GetAttributes(TypeattributeTypeMemberInfotype)
{
returnGetAttributes(attributeTypetypefalse);
}
///
///Getsthecollectionofallspecifiedobxxxxjectattributesfortypeasspecifiedbytypewithoptiontoserachparent
///
///TheattributeTypeforwhichthecustomattributesaretobereturned.
///thetypeonwhichthespecifiedattributeisdefined
///TheattributeTypeforwhichthecustomattributeistobereturned.
///
///Attributeasobxxxxjectornullifnotfound.
///
publicstaticobxxxxject[]GetAttributes(TypeattributeTypeMemberInfotypeboolsearchParent)
{
if(type==null)
{
returnnull;
}
if(attributeType==null)
{
returnnull;
}
if(!
(attributeType.IsSubclassOf(typeof(Attribute))))
{
returnnull;
}
if(type.IsDefined(attributeTypefalse))
{
returntype.GetCustomAttributes(attributeTypesearchParent);
}
returnnull;
}
#endregion
#region资源获取
///
///根据资源名称获取图片资源流
///
///
///
publicstaticStreamGetImageResource(stringResourceName)
{
Assemblyasm=Assembly.GetExecutingAssembly();
returnasm.GetManifestResourceStream(ResourceName);
}
///
///获取程序集资源的位图资源
///
///程序集中的某一对象类型
///资源的根名称。
例如,名为“MyResource.en-US.resources”的资源文件的根名称为“MyResource”。
///资源项名称
publicstaticBitmapLoadBitmap(TypeassemblyTypestringresourceHolderstringimageName)
{
AssemblythisAssembly=Assembly.GetAssembly(assemblyType);
ResourceManagerrm=newResourceManager(resourceHolderthisAssembly);
return(Bitmap)rm.Getobxxxxject(imageName);
}
///
///获取程序集资源的文本资源
///
///程序集中的某一对象类型
///资源项名称
///资源的根名称。
例如,名为“MyResource.en-US.resources”的资源文件的根名称为“MyResource”。
publicstaticstringGetStringRes(TypeassemblyTypestringresNamestringresourceHolder)
{
AssemblythisAssembly=Assembly.GetAssembly(assembly