SetValue方法的20個代碼示例,這些例子默認根據受歡迎程度排序。
... I/F public bool SetPropertyValue(object component, PropertyInfo property, ...
當前位置:首頁>>代碼示例>>C#>>正文
本文整理匯總了C#中System.Reflection.PropertyInfo.SetValue方法的典型用法代碼示例。
如果您正苦於以下問題:C#PropertyInfo.SetValue方法的具體用法?C#PropertyInfo.SetValue怎麽用?C#PropertyInfo.SetValue使用的例子?那麽恭喜您,這裏精選的方法代碼示例或許可以為您提供幫助。
您也可以進一步了解該方法所在類System.Reflection.PropertyInfo的用法示例。
在下文中一共展示了PropertyInfo.SetValue方法的20個代碼示例,這些例子默認根據受歡迎程度排序。
您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於我們的係統推薦出更棒的C#代碼示例。
示例1:SetPropertyValue
點讚9
//I/F
publicboolSetPropertyValue(objectcomponent,PropertyInfoproperty,stringpropertyValue)
{
varpropertyType=property.PropertyType;
if(propertyType==typeof(string))
{
property.SetValue(component,propertyValue,null);
returntrue;
}
if(propertyType.IsEnum)
{
varconvertedValue=Enum.Parse(propertyType,propertyValue,true);
property.SetValue(component,convertedValue,null);
returntrue;
}
if(typeof(IConvertible).IsAssignableFrom(propertyType))
{
varconvertedValue=Convert.ChangeType(propertyValue,propertyType,null);
property.SetValue(component,convertedValue,null);
returntrue;
}
returnfalse;
}開發者ID:willcraftia,項目名稱:TestBlocks,代碼行數:27,代碼來源:DefaultPropertyHandler.cs
示例2:DecodeXmlTextReaderValue
點讚7
internalstaticboolDecodeXmlTextReaderValue(objectinstance,PropertyInfopropertyInfo,XmlReaderxmlTextReader)
{
try
{
//findrelatedpropertybynameifnotprovidedasparameter
if(propertyInfo==null)
propertyInfo=instance.GetType().GetProperty(xmlTextReader.Name,BindingFlags.IgnoreCase|BindingFlags.Public|BindingFlags.Instance);
if(propertyInfo==null)
returnfalse;
//unescapedcharacters<>&
if(propertyInfo.PropertyType.Equals(typeof(string)))
propertyInfo.SetValue(instance,Decode(xmlTextReader.ReadInnerXml().Trim()),null);
elseif(propertyInfo.PropertyType.Equals(typeof(DateTime)))
propertyInfo.SetValue(instance,ParseRfc822DateTime(xmlTextReader.ReadInnerXml().Trim()),null);
else
propertyInfo.SetValue(instance,TypeDescriptor.GetConverter(propertyInfo.PropertyType).ConvertFromString(xmlTextReader.ReadInnerXml().Trim()),null);
}
catch(Exceptione)
{
Debug.WriteLine(propertyInfo.Name+","+propertyInfo.PropertyType.Name+"/"+instance.ToString()+""+e.Message);
returnfalse;
}
returntrue;
}開發者ID:miracle091,項目名稱:transmission-remote-dotnet,代碼行數:25,代碼來源:XmlSerializationUtil.cs
示例3:AssingDefaultValue
點讚5
publicvoidAssingDefaultValue(PropertyInfoprop)
{
varattrib=prop.GetAttribute();
if(attrib.CreateNewAsDefaultValue)
prop.SetValue(this,Activator.CreateInstance(prop.PropertyType),null);
elseif(attrib.DefaultValue!=null)
prop.SetValue(this,attrib.DefaultValue,null);
}開發者ID:kkalinowski,項目名稱:lib12,代碼行數:8,代碼來源:SerializableViewModel.cs
示例4:SetPropertyValue
點讚2
voidSetPropertyValue(NameValueCollectionconfig,PropertyInfopropertyInfo){
if(!(string.IsNullOrEmpty(config[propertyInfo.Name.MakeFirstCharLower()]))){
propertyInfo.SetValue(this,XpandReflectionHelper.ChangeType(config[propertyInfo.Name.MakeFirstCharLower()],propertyInfo.PropertyType),null);
}else{
vardefaultValueAttribute=propertyInfo.GetCustomAttributes(typeof(DefaultValueAttribute),true).OfType().FirstOrDefault();
if(defaultValueAttribute!=null)
propertyInfo.SetValue(this,defaultValueAttribute.Value,null);
}
}開發者ID:aries544,項目名稱:eXpand,代碼行數:9,代碼來源:FilterProviderBase.cs
示例5:CopyPropertyValueByInfo
點讚1
///
///Copiesvaluefromonepropertytoanother
///
///
///Sourceobject
///
///
///Sourceproperty
///
///
///Destinationobject
///
///
///Destinationproperty
///
///
///trueifsuccessful
///
publicstaticboolCopyPropertyValueByInfo(objectaFromObj,PropertyInfoaFrom,objectaToObj,PropertyInfoaTo)
{
boolres=false;
if((aFrom==null)||(aTo==null)||(aFromObj==null)||(aToObj==null)){
Debug.Warning("CopyPropertyValueByInfo","Oneisnull"+(aFrom==null)+(aTo==null)+(aFromObj==null)+(aToObj==null));
return(false);
}
if(aFrom.CanRead==false)
thrownewExceptionGettingWriteOnlyProperty();
objectval=aFrom.GetValue(aFromObj,null);
if(val==null)
return(false);
if(aTo.CanWrite==false)
thrownewExceptionAssigningReadOnlyProperty();
try{
objectno=System.Convert.ChangeType(val,aTo.PropertyType);
if(noisIComparable){
objectcval=aTo.GetValue(aToObj,null);
if((cvalasIComparable).CompareTo(no)!=0){
aTo.SetValue(aToObj,no,null);
res=true;
}
}
else{
res=true;
aTo.SetValue(aToObj,no,null);
}
no=null;
}
catch(System.InvalidCastException){
res=false;
thrownewExceptionPropertyTranslationError();
}
catch(System.ArgumentNullException){
res=false;
thrownewExceptionPropertyTranslationError();
}
catch(System.FormatException){
res=false;
thrownewExceptionPropertyTranslationError();
}
catch(System.OverflowException){
res=false;
thrownewExceptionPropertyTranslationError();
}
val=null;
return(res);
}開發者ID:QualitySolution,項目名稱:Gtk.DataBindings,代碼行數:68,代碼來源:ConnectionProvider.cs
示例6:SetOption
點讚1
privatestaticboolSetOption(
objectcommandLine,PropertyInfoproperty,
string[]optionParts,refstringerrorMessage)
{
boolsuccess;
if(property.PropertyType==typeof(bool))
{
//Lastparametersforhandlingindexers
property.SetValue(
commandLine,true,null);
success=true;
}
else
{
if((optionParts.Length<2)
||optionParts[1]==""
||optionParts[1]==":")
{
//Nosettingwasprovidedfortheswitch.
success=false;
errorMessage=string.Format(
"Youmustspecifythevalueforthe{0}option.",
property.Name);
}
elseif(
property.PropertyType==typeof(string))
{
property.SetValue(
commandLine,optionParts[1],null);
success=true;
}
elseif(property.PropertyType.IsEnum)
{
success=TryParseEnumSwitch(
commandLine,optionParts,
property,referrorMessage);
}
else
{
success=false;
errorMessage=string.Format(
"Datatype'{0}'on{1}isnotsupported.",
property.PropertyType.ToString(),
commandLine.GetType().ToString());
}
}
returnsuccess;
}開發者ID:rubycoder,項目名稱:EssentialCSharp,代碼行數:50,代碼來源:Listing17.16.UpdatingCommandLineHandler.TryParseToHandleAliases.cs
示例7:Map
點讚1
publicboolMap(objectdestinationObject,PropertyInfodestinationProperty,XmlElementelement,XmlElementallConfig,ConfigMappermapper)
{
varchildElement=element.GetElementNamed(ElementName??destinationProperty.Name);
if(childElement==null)
{
returnfalse;
}
varelementValue=childElement.InnerText;
vardestinationPropertyType=destinationProperty.PropertyType;
if(destinationPropertyType.IsEnum)
{
varvalue=Enum.Parse(destinationPropertyType,elementValue);
destinationProperty.SetValue(destinationObject,value,null);
returntrue;
}
if(destinationPropertyType.IsNullable())
{
if(elementValue=="")
{
destinationProperty.SetValue(destinationObject,null,null);
returntrue;
}
destinationPropertyType=destinationPropertyType.GetGenericArguments()[0];
}
if(destinationPropertyType.IsA())
{
varvalue=Convert.ChangeType(elementValue,destinationPropertyType);
destinationProperty.SetValue(destinationObject,value,null);
returntrue;
}
varconverter=TypeDescriptor.GetConverter(destinationPropertyType);
if(converter.CanConvertFrom(typeof(string)))
{
varvalue=converter.ConvertFromString(elementValue);
destinationProperty.SetValue(destinationObject,value,null);
returntrue;
}
returnfalse;
}開發者ID:bushadam,項目名稱:simple-config,代碼行數:49,代碼來源:ElementValueAttribute.cs
示例8:SetValue
點讚1
publicstaticvoidSetValue(thisobjectself,PropertyInfoinfo,objectvalue)
{
if(info!=null)
{
info.SetValue(self,ChangeType(info,value));
}
}開發者ID:ernestjohn,項目名稱:azure-search-util,代碼行數:7,代碼來源:PropertyExtensions.cs
示例9:SpecifyUtcKind
點讚1
privatestaticvoidSpecifyUtcKind(PropertyInfoproperty,objectvalue)
{
//Getthedatetimevalue
vardatetime=property.GetValue(value,null);
//setDateTimeKindtoUtc
if(property.PropertyType==typeof(DateTime))
{
datetime=DateTime.SpecifyKind((DateTime)datetime,DateTimeKind.Utc);
}
elseif(property.PropertyType==typeof(DateTime?))
{
varnullable=(DateTime?)datetime;
if(!nullable.HasValue)
return;
datetime=(DateTime?)DateTime.SpecifyKind(nullable.Value,DateTimeKind.Utc);
}
else
{
return;
}
//AndsettheUtcDateTimevalue
property.SetValue(value,datetime,null);
}開發者ID:nandusharma,項目名稱:tasklist,代碼行數:25,代碼來源:DbContextExtension-Timezone.cs
示例10:DoAction
點讚1
publicvoidDoAction(objectsiteObject,PropertyInfopropertyInfo,DataGeneratordataGenerator)
{
if(propertyInfo.CanWrite)
{
propertyInfo.SetValue(siteObject,Guid.NewGuid().ToString(),null);
}
}開發者ID:ManginAlexander,項目名稱:mic.comQuestionary,代碼行數:7,代碼來源:DataGenerator.cs
示例11:ReadBoolean
點讚1
publicvoidReadBoolean(PropertyInfoproperty)
{
XElementbooleanElement=this.m_Document.Element(property.Name);
if(booleanElement!=null)
{
intvalue=Convert.ToInt32(booleanElement.Value);
if(value==1)
{
property.SetValue(this.m_ObjectToWriteTo,true,null);
}
else
{
property.SetValue(this.m_ObjectToWriteTo,false,null);
}
}
}開發者ID:WilliamCopland,項目名稱:YPILIS,代碼行數:16,代碼來源:XmlPropertyWriter.cs
示例12:SetValue
點讚1
///
///Setsthenewvalueforaspecificpropertyonanobject.
///
///Targetobjecttosettheproperty.Inmostcasesthiswillbeadomainobject.
///Propertyinfoforthepropertytoset.
///Valuefortheproperty.
///
///Unabletoconvertthenewvaluetothespecifiedproperty.
///nullreferenceisnotacceptasavalidargument.
publicvoidSetValue(objectinstance,PropertyInfopi,objectnewvalue,CultureInfoculture=null)
{
//elsetrytocastthenewvaluetothecorrecttype
objecto=Convert.ChangeType(newvalue,pi.PropertyType,culture??CultureInfo.CurrentCulture);
if(pi.CanWrite)pi.SetValue(instance,o,null);
}開發者ID:erwinbovendeur,項目名稱:ADF,代碼行數:16,代碼來源:DefaultPropertyParser.cs
示例13:SetPropertyToUniqueValue
點讚1
publicstaticvoidSetPropertyToUniqueValue(thisobjecttarget,PropertyInfoproperty,objectensureDifferentFrom)
{
objectotherValue=null;
if(ensureDifferentFrom!=null)
otherValue=property.GetValue(ensureDifferentFrom);
property.SetValue(target,CreateUniqueValueOf(property.PropertyType,otherValue));
}開發者ID:rickeygalloway,項目名稱:Test,代碼行數:7,代碼來源:ObjectExtensions.cs
示例14:BindNamed
點讚1
voidBindNamed(objectcommando,PropertyInfoproperty,Listparameters,NamedArgumentAttributeattribute,BindingContextcontext)
{
varname=attribute.Name;
varshortHand=attribute.ShortHand;
varparameter=parameters.Where(p=>pisNamedCommandLineParameter)
.Cast()
.SingleOrDefault(p=>p.Name==name||p.Name==shortHand);
varvalue=parameter!=null
?Mutate(parameter,property)
:attribute.Default!=null
?Mutate(attribute.Default,property)
:null;
if(value==null)
{
context.Report.PropertiesNotBound.Add(property);
if(!attribute.Required)return;
throwEx("Couldnotfindparametermatchingrequiredparameternamed{0}",name);
}
property.SetValue(commando,value,null);
context.Report.PropertiesBound.Add(property);
}開發者ID:rebus-org,項目名稱:GoCommando,代碼行數:27,代碼來源:Binder.cs
示例15:SetValueFor
點讚1
publicvoidSetValueFor(Objectinstance,stringkey,PropertyInfoproperty)
{
if(Scope.Inputs[key]==null)return;
objectresults=ConversionUtil.ConvertTo(property.PropertyType,Scope.Inputs[key]);
if(results!=null)property.SetValue(instance,results,null);
}開發者ID:nkmajeti,項目名稱:rhino-tools,代碼行數:7,代碼來源:IInjectedMemberStrategy.cs
示例16:GetObjectFromWriteOnlyProperty
點讚1
privateobjectGetObjectFromWriteOnlyProperty(objectparentObject,PropertyInfodestinationProperty)
{
vardestinationObject=destinationProperty.PropertyType.CreateFromObjectOrInterface();
destinationProperty.SetValue(parentObject,destinationObject,null);
returndestinationObject;
}開發者ID:bushadam,項目名稱:simple-config,代碼行數:7,代碼來源:ComplexTypeBindingStrategy.cs
示例17:SetPropertyValues
點讚1
internalstaticboolSetPropertyValues(thisobjectinstance,PropertyInfoproperty,objectvalue)
{
varcontainer=(IEnumerable)property.GetValue(instance);
varitemType=property.PropertyType.FindItemType();
if(property.PropertyType.IsArray)
{
returnproperty.SetPropertyArrayValue(instance,container,value);
}
if((container!=null)&&(container.GetType().GetImplementationOfAny(typeof(ICollection<>),typeof(ICollection))!=null))
{
container.GetType().GetMethod("Add",BindingFlags.Instance|BindingFlags.Public).Invoke(container,new[]{value});
returntrue;
}
if(!property.CanWrite)
{
returnfalse;
}
varlist=(IList)typeof(List<>).MakeGenericType(itemType).GetConstructor(newType[0]).Invoke(null);
property.SetValue(instance,list);
container.ForEach(item=>list.Add(item));
list.Add(value);
returntrue;
}開發者ID:alien-mcl,項目名稱:URSA,代碼行數:26,代碼來源:MemberInfoExtensions.cs
示例18:SetPassword
點讚1
privatevoidSetPassword(objectentity,EditorItemeditorItem,PropertyInfoproperty)
{
if(entity.GetType().GetInterfaces().Count(t=>t==typeof(IPassword))==0)
property.SetValue(entity,editorItem.Value,null);
else
typeof(IPassword).GetMethod("SetPassword").Invoke(entity,newobject[]{editorItem.Value});
}開發者ID:liny4cn,項目名稱:ComBoost,代碼行數:7,代碼來源:PasswordEditor.cs
示例19:FillProperty
點讚1
privatevoidFillProperty(objectviewModel,PropertyInfoproperty)
{
objectresult=null;
varpropertyType=property.PropertyType;
varbaseType=propertyType.GetGenericTypeDefinition();
varinnerType=propertyType.GetGenericArguments()[0];
vardata=GetQueryableDataFor(innerType);
varfilters=FilterLocator.GetFiltersByConvention(innerType,property);
foreach(varfilterinfilters)
{
data=newFilterExecutor().Execute(filter,data);
}
if(baseType==typeof(IPagedList<>))
{
//ToDoUmhowdowepageafilledproperty?probablyabadidea
result=Utilities.ToPagedList(innerType,data,newPagingCriteria());
}
elseif(baseType==typeof(IEnumerable<>)||baseType==typeof(IList<>))
{
result=Utilities.ToList(innerType,data);
}
elseif(baseType==typeof(IQueryable<>))
{
result=data;
}
property.SetValue(viewModel,result,null);
}開發者ID:ryanohs,項目名稱:DynamicServices,代碼行數:31,代碼來源:QueryModelInspector.cs
示例20:BrushDesignerWindow
點讚1
publicBrushDesignerWindow(objectobj,PropertyInfoprop)
{
InitializeComponent();
(GradientGrid.BackgroundasDrawingBrush).RelativeTransform=GGridTrans=newRotateTransform();
GGridTrans.CenterX=0.5;
GGridTrans.CenterY=0.5;
isd=newImageSourceDesigner();
b=prop.GetValue(obj,null)asBrush;
setv=(x)=>prop.SetValue(obj,x,null);
if(b.IsFrozen)
{
b=b.Clone();
setv(b);
}
if(bisSolidColorBrush)
{
cmode=Mode.Solid;
}
elseif(bisLinearGradientBrush)
{
cmode=Mode.LinearGradient;
}
elseif(bisImageBrush)
{
cmode=Mode.Image;
}
}開發者ID:byteit101,項目名稱:ZomB-Dashboard-System,代碼行數:28,代碼來源:BrushDesignerWindow.xaml.cs
注:本文中的System.Reflection.PropertyInfo.SetValue方法示例整理自Github/MSDocs等源碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。
相關方法
PropertyInfo.GetCustomAttributes
PropertyInfo.GetIndexParameters
PropertyInfo.SetValue
PropertyInfo.IsDefined
PropertyInfo.GetSetMethod
PropertyInfo.GetGetMethod
PropertyInfo.GetValue
PropertyInfo.GetAttribute
PropertyInfo.GetCustomAttribute
PropertyInfo.GetAccessors
PropertyInfo.Is
PropertyInfo.FirstOrDefault
PropertyInfo.GetAttributes
PropertyInfo.HasAttribute
PropertyInfo.NiceName
PropertyInfo.Select
PropertyInfo.GetExtendedMemberInfoString
PropertyInfo.GetType
PropertyInfo.Where
PropertyInfo.ToString
PropertyInfo.Attr
PropertyInfo.Attrs
PropertyInfo.Setter
PropertyInfo.GetArgumentName
PropertyInfo.HasAttr
PropertyInfo.Single
PropertyInfo.Clone
PropertyInfo.Getter
PropertyInfo.GetOneToManyAttribute
PropertyInfo.GetCustomAttributesData
PropertyInfo.FirstAttribute
PropertyInfo.GetKeyProperty
PropertyInfo.IsAutoProperty
PropertyInfo.ThrowIfNull
PropertyInfo.Any
PropertyInfo.GetNamefromAttributeOrPropertyName
PropertyInfo.GetManyToManyAttribute
PropertyInfo.IsNonEmpty
PropertyInfo.AttributeExists
PropertyInfo.IsStatic
PropertyInfo.IsIgnored
PropertyInfo.GetDataMember
PropertyInfo.Name
PropertyInfo.Last
PropertyInfo.GetAnnotation
PropertyInfo.ForAttribute
PropertyInfo.GetFirstCustomAttribute
PropertyInfo.HasAnnotation
PropertyInfo.IsValidEdmScalarProperty
PropertyInfo.IsSameAs
PropertyInfo.IsIndexer
PropertyInfo.GetDocumentation
PropertyInfo.GetDescription
PropertyInfo.?.GetCustomAttributes
PropertyInfo.GetHashCode
PropertyInfo.IsNavigable
PropertyInfo.GetManyToOneAttribute
PropertyInfo.IsValidEdmNavigationProperty
PropertyInfo.IsSerializable
PropertyInfo.NameString
PropertyInfo.GetCodeFirstAttribute
PropertyInfo.IsEquivalent
PropertyInfo.First
PropertyInfo.ToPropertyDecl
PropertyInfo.ToElement
PropertyInfo.DelegateForGetPropertyValue
PropertyInfo.DeclaringTypeNameString
PropertyInfo.Count
PropertyInfo.ConvertToCompatibleValueFast
PropertyInfo.CanWriteExtended