当前位置: 首页>>代码示例>>C#>>正文


C# PropertyDescriptor.SetValue方法代码示例

本文整理汇总了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))));
     }
 }
开发者ID:vlko,项目名称:vlko,代码行数:20,代码来源:AntiXssAttribute.cs

示例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);
            }
        }
开发者ID:tebass,项目名称:LtiSamples,代码行数:25,代码来源:CustomModelBinder.cs

示例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)));
     }
 }
开发者ID:vlko,项目名称:jobs,代码行数:12,代码来源:AntiXssAttribute.cs

示例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);
     }
 }
开发者ID:softgears,项目名称:kartel,代码行数:32,代码来源:JsonModelBinder.cs

示例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);
                    }
                }
            }
        }
开发者ID:mikalai-silivonik,项目名称:bnh,代码行数:47,代码来源:BnhModelBinder.cs

示例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);
        }
开发者ID:Ordojan,项目名称:Electronix,代码行数:18,代码来源:UserBinder.cs

示例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);
     }
 }
开发者ID:softgears,项目名称:PartDesk,代码行数:43,代码来源:DateTimeModelBinder.cs

示例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);
        }
开发者ID:Ordojan,项目名称:Online-movie-store,代码行数:14,代码来源:MovieModelBinder.cs

示例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);
        }
开发者ID:ryansroberts,项目名称:Snooze,代码行数:14,代码来源:SubUrlModelBinder.cs

示例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);
     }
 }
开发者ID:realzhaorong,项目名称:vocadb,代码行数:14,代码来源:PropertyModelBinder.cs

示例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;
            }

        }
开发者ID:paulsuart,项目名称:rebelcmsxu5,代码行数:36,代码来源:RichTextBoxPreValueModelBinder.cs

示例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);
     }
 }
开发者ID:dnmsk,项目名称:rProject,代码行数:15,代码来源:ExtendedModelBinder.cs

示例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);
        }
开发者ID:vitor-tadashi,项目名称:marte-updates,代码行数:16,代码来源:CustomModelBinder.cs

示例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);
     }
 }
开发者ID:yifeng-shen,项目名称:leishi,代码行数:17,代码来源:DQDefaultModelBinder.cs

示例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);
                        }
                    }
                }
            }
开发者ID:rsmolnikov,项目名称:FluentValidation,代码行数:19,代码来源:FluentValidationModelBinder.cs


注:本文中的System.ComponentModel.PropertyDescriptor.SetValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。