本文整理汇总了C#中System.ComponentModel.PropertyDescriptor.SetValue方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyDescriptor.SetValue方法的具体用法?C# PropertyDescriptor.SetValue怎么用?C# PropertyDescriptor.SetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ComponentModel.PropertyDescriptor
的用法示例。
在下文中一共展示了PropertyDescriptor.SetValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessProperty
/// <summary>
/// Processes the property.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="property">The property.</param>
private static void ProcessProperty(object value, PropertyDescriptor property)
{
if (property.Attributes.Contains(AntiXssHtmlText))
{
property.SetValue(value, Sanitizer.GetSafeHtmlFragment((string) property.GetValue(value)));
}
else if (property.Attributes.Contains(AntiXssIgnore))
{
// do nothing this contains special text
}
else
{
property.SetValue(value, HttpUtility.HtmlDecode(HttpUtility.HtmlEncode((string)property.GetValue(value))));
}
}
示例2: BindProperty
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
{
var performValidation = controllerContext.Controller.ValidateRequest &&
bindingContext.ModelMetadata.RequestValidationEnabled;
var propertyBinderAttribute = propertyDescriptor
.Attributes
.OfType<DataMemberAttribute>()
.FirstOrDefault();
var modelName = propertyBinderAttribute == null ? bindingContext.ModelName : propertyBinderAttribute.Name;
var result = performValidation
? bindingContext.ValueProvider.GetValue(modelName)
: bindingContext.GetUnvalidatedValue(modelName);
if (result == null)
{
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
else
{
propertyDescriptor.SetValue(bindingContext.Model, result.AttemptedValue);
}
}
示例3: ProcessProperty
/// <summary>
/// Processes the property.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="property">The property.</param>
private static void ProcessProperty(object value, PropertyDescriptor property)
{
if (property.Attributes.Contains(AntiXssHtmlText))
{
property.SetValue(value, AntiXss.GetSafeHtmlFragment((string) property.GetValue(value)));
}
}
示例4: BindProperty
/// <summary>
/// Binds the specified property by using the specified controller context and binding context and the specified property descriptor.
/// </summary>
/// <param name="controllerContext">The context within which the controller operates. The context information includes the controller, HTTP content, request context, and route data.</param><param name="bindingContext">The context within which the model is bound. The context includes information such as the model object, model name, model type, property filter, and value provider.</param><param name="propertyDescriptor">Describes a property to be bound. The descriptor provides information such as the component type, property type, and property value. It also provides methods to get or set the property value.</param>
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
{
var jsonAttribute = (JsonPropertyAttribute) propertyDescriptor.Attributes.Cast<Attribute>().FirstOrDefault(a => a is JsonPropertyAttribute);
if (jsonAttribute != null)
{
var propertyName = jsonAttribute.PropertyName;
var form = controllerContext.HttpContext.Request.Unvalidated().Form;
if (form.AllKeys.Contains(propertyName) && propertyDescriptor.Converter != null)
{
var propertyValue = form[propertyName];
if (propertyDescriptor.PropertyType == typeof(bool) || propertyDescriptor.PropertyType == typeof(bool?))
{
if (propertyValue != null && propertyValue == "on")
{
propertyValue = "true";
} else
{
propertyValue = "false";
}
}
propertyDescriptor.SetValue(bindingContext.Model, propertyDescriptor.Converter.ConvertFrom(propertyValue));
}
}
else
{
base.BindProperty(controllerContext,bindingContext,propertyDescriptor);
}
}
示例5: SetProperty
protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
{
ModelMetadata propertyMetadata = bindingContext.PropertyMetadata[propertyDescriptor.Name];
propertyMetadata.Model = value;
string modelStateKey = CreateSubPropertyName(bindingContext.ModelName, propertyMetadata.PropertyName);
// Try to set a value into the property unless we know it will fail (read-only
// properties and null values with non-nullable types)
if (!propertyDescriptor.IsReadOnly)
{
try
{
if (value == null)
{
propertyDescriptor.SetValue(bindingContext.Model, value);
}
else
{
Type valueType = value.GetType();
if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
IListSource ls = (IListSource)propertyDescriptor.GetValue(bindingContext.Model);
IList list = ls.GetList();
foreach (var item in (IEnumerable)value)
{
list.Add(item);
}
}
else
{
propertyDescriptor.SetValue(bindingContext.Model, value);
}
}
}
catch (Exception ex)
{
// Only add if we're not already invalid
if (bindingContext.ModelState.IsValidField(modelStateKey))
{
bindingContext.ModelState.AddModelError(modelStateKey, ex);
}
}
}
}
示例6: BindProperty
protected override void BindProperty(ControllerContext controllerContext,
ModelBindingContext bindingContext,
PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.Name == "Password") {
var password = controllerContext.HttpContext.Request.Form["Password"];
var confirmPassword = controllerContext.HttpContext.Request.Form["ConfirmPassword"];
if (password == confirmPassword)
propertyDescriptor.SetValue(bindingContext.Model, password);
else
propertyDescriptor.SetValue(bindingContext.Model, "");
return;
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
示例7: BindProperty
/// <summary>
/// Привязывает указанное свойство, используя заданные контекст контроллера и контекст привязки, а также заданный дескриптор свойства.
/// </summary>
/// <param name="controllerContext">Контекст, в котором функционирует контроллер.Сведения о контексте включают информацию о контроллере, HTTP-содержимом, контексте запроса и данных маршрута.</param><param name="bindingContext">Контекст, в котором привязана модель.Контекст содержит такие сведения, как объект модели, имя модели, тип модели, фильтр свойств и поставщик значений.</param><param name="propertyDescriptor">Описывает свойство, которое требуется привязать.Дескриптор предоставляет информацию, такую как тип компонента, тип свойства и значение свойства.Также предоставляет методы для получения или задания значения свойства.</param>
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.PropertyType == typeof(DateTime))
{
var submittedVal = controllerContext.HttpContext.Request[propertyDescriptor.Name];
if (!String.IsNullOrEmpty(submittedVal))
{
DateTime val = DateTime.MinValue;
try
{
val = Convert.ToDateTime(submittedVal);
}
catch (Exception)
{
}
propertyDescriptor.SetValue(bindingContext.Model, val);
}
}
else if (propertyDescriptor.PropertyType == typeof(DateTime?))
{
var submittedVal = controllerContext.HttpContext.Request[propertyDescriptor.Name];
if (!String.IsNullOrEmpty(submittedVal))
{
DateTime? val = null;
try
{
val = Convert.ToDateTime(controllerContext.HttpContext.Request[propertyDescriptor.Name]);
}
catch (Exception)
{
val = null;
}
propertyDescriptor.SetValue(bindingContext.Model, val);
}
} else
{
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
}
示例8: BindProperty
protected override void BindProperty(ControllerContext controllerContext,
ModelBindingContext bindingContext,
PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.Name == CategoryPDN) {
var input = controllerContext.HttpContext.Request.Form[CategoryKey];
var category = new Category { Id = Guid.Parse(input) };
propertyDescriptor.SetValue(bindingContext.Model, category);
return;
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
示例9: BindParentProperty
void BindParentProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
{
//var binder = ModelBinders.Binders.GetBinder(propertyDescriptor.PropertyType, true);
var parentBindingContext = new ModelBindingContext
{
//ModelType = propertyDescriptor.PropertyType, - not valid in .Net 4 replaced with following
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, propertyDescriptor.PropertyType),
ValueProvider = bindingContext.ValueProvider
};
//var parent = binder.BindModel(controllerContext, parentBindingContext);
var parent = BindModel(controllerContext, parentBindingContext);
propertyDescriptor.SetValue(bindingContext.Model, parent);
}
示例10: BindProperty
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
{
// Check if the property has the PropertyBinderAttribute, meaning
// it's specifying a different binder to use.
var propertyBinderAttribute = TryFindPropertyBinderAttribute(propertyDescriptor);
if (propertyBinderAttribute != null) {
var binder = propertyBinderAttribute.GetPropertyBinder();
var value = binder.BindProperty(controllerContext, bindingContext, propertyDescriptor);
propertyDescriptor.SetValue(bindingContext.Model, value);
} else // revert to the default behavior.
{
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
}
示例11: BindProperty
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
{
var model = (RichTextBoxPreValueModel) bindingContext.Model;
switch (propertyDescriptor.Name)
{
case "Features":
var features = model.GetFeatures().ToArray();
foreach (var e in features)
{
//get the values from the value provider for each element
var valueName = string.Concat(bindingContext.ModelName, ".", propertyDescriptor.Name, ".", e.Value);
var value = bindingContext.ValueProvider.GetValue(valueName);
if(value != null)
e.Selected = bool.Parse(((string[])value.RawValue)[0]);
}
propertyDescriptor.SetValue(bindingContext.Model, features);
break;
case "Stylesheets":
var stylesheets = model.GetStylesheets().ToArray();
foreach (var e in stylesheets)
{
//get the values from the value provider for each element
var valueName = string.Concat(bindingContext.ModelName, ".", propertyDescriptor.Name, ".", e.Value);
var value = bindingContext.ValueProvider.GetValue(valueName);
if (value != null)
e.Selected = bool.Parse(((string[])value.RawValue)[0]);
}
propertyDescriptor.SetValue(bindingContext.Model, stylesheets);
break;
default:
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
break;
}
}
示例12: BindProperty
protected override void BindProperty(
ControllerContext controllerContext,
ModelBindingContext bindingContext,
PropertyDescriptor propertyDescriptor)
{
var propertyBinderAttribute = TryFindPropertyBinderAttribute(propertyDescriptor);
if (propertyBinderAttribute != null) {
var binder = CreateBinder(propertyBinderAttribute);
var value = binder.BindModel(propertyBinderAttribute.BindParam, controllerContext, bindingContext, propertyDescriptor);
propertyDescriptor.SetValue(bindingContext.Model, value);
} else // revert to the default behavior.
{
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
}
示例13: BindProperty
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
{
string fullPropertyKey = CreateSubPropertyName(bindingContext.ModelName, propertyDescriptor.Name);
if (propertyDescriptor.PropertyType == typeof(double?) || propertyDescriptor.PropertyType == typeof(double)) {
var valueProvider = bindingContext.ValueProvider.GetValue(fullPropertyKey);
if (valueProvider != null) {
double value = 0;
if (double.TryParse(valueProvider.AttemptedValue, NumberStyles.Number, new CultureInfo("pt-BR").NumberFormat, out value)) {
propertyDescriptor.SetValue(bindingContext.Model, value);
}
}
} else
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
示例14: BindProperty
protected override void BindProperty(
ControllerContext controllerContext,
ModelBindingContext bindingContext,
PropertyDescriptor propertyDescriptor)
{
var propertyBinderAttribute = propertyDescriptor.TryFindPropertyBinderAttribute();
if (propertyBinderAttribute != null)
{
var binder = this.CreateBinder(propertyBinderAttribute);
var value = binder.BindModel(controllerContext, bindingContext, propertyDescriptor);
propertyDescriptor.SetValue(bindingContext.Model, value);
}
else
{
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
}
示例15: SetProperty
protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
{
//Bypass the built in validation entirely.
bool isNonNullableValueTypeWithNullValue = (value == null) && !TypeAllowsNullValue(propertyDescriptor.PropertyType);
var metadata = bindingContext.PropertyMetadata[propertyDescriptor.Name];
string key = CreateSubPropertyName(bindingContext.ModelName, metadata.PropertyName);
if (!propertyDescriptor.IsReadOnly && !isNonNullableValueTypeWithNullValue) {
try {
propertyDescriptor.SetValue(bindingContext.Model, value);
}
catch (Exception exception) {
if (bindingContext.ModelState.IsValidField(key)) {
bindingContext.ModelState.AddModelError(key, exception);
}
}
}
}