示例#1
0
        protected virtual void Initialize(object component,
                                          IEnumerable <KeyValuePair <string, object> > values,
                                          IServiceProvider serviceProvider = null)
        {
            if (component == null)
            {
                throw new ArgumentNullException(nameof(component));
            }

            if (values == null)
            {
                return;
            }

            serviceProvider = serviceProvider ?? ServiceProvider.Null;
            var exceptionHandler = serviceProvider.GetServiceOrDefault(
                Activation.IgnoreErrors
                );

            var props = Template.GetPropertyCache(component);

            foreach (var kvp in values.Distinct(KeyComparer))
            {
                PropertyInfo pd = props.GetValueOrDefault(kvp.Key);

                if (pd == null)
                {
                    exceptionHandler(null, RuntimeFailure.PropertyMissing(kvp.Key));
                    continue;
                }

                if (pd.SetMethod == null || !pd.SetMethod.IsPublic)
                {
                    var dest = pd.GetValue(component);
                    Template.DefaultCopyContent(kvp.Value, dest);
                    continue;
                }

                object defaultValue = kvp.Value;
                try {
                    // Conversion from string
                    var text = defaultValue as string;
                    if (text != null)
                    {
                        defaultValue = Activation.FromText(pd.PropertyType, text);
                    }
                    pd.SetValue(component, defaultValue);
                } catch (Exception ex) {
                    exceptionHandler(null, ex);
                }
            }
        }
 private static bool StaticDefaultCoercion(PropertyInfo property, object value, Type requiredType, out object result)
 {
     if (value is string str)
     {
         try {
             result = Activation.FromText(requiredType, str, null, null);
             return(true);
         } catch {
             // Type conversion problem
         }
     }
     result = null;
     return(false);
 }
示例#3
0
        protected override bool TryGetPropertyCore(string property, Type requiredType, out object value)
        {
            // Review the local storage to determine if it is contained
            requiredType = requiredType ?? typeof(object);
            if (InnerMap.TryGetValue(property, out value))
            {
                if (requiredType.GetTypeInfo().IsInstanceOfType(value) || value == null)
                {
                    return(true);
                }

                // Type coercion
                var str = value as string;
                if (str != null)
                {
                    value = Activation.FromText(requiredType, str, null, null);
                    return(true);
                }
            }

            value = null;
            return(base.TryGetPropertyCore(property, requiredType, out value));
        }
示例#4
0
        public static object GetDefaultValue(this PropertyInfo property)
        {
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }
            var data = property.CustomAttributes.FirstOrDefault(t => t.AttributeType == typeof(DefaultValueAttribute));

            if (data != null && data.ConstructorArguments.Count == 2)
            {
                // Do a parse to extract the value
                return(Activation.FromText((Type)data.ConstructorArguments[0].Value,
                                           (string)data.ConstructorArguments[1].Value));
            }

            var attr = property.GetCustomAttribute <DefaultValueAttribute>();

            if (attr == null)
            {
                return(property.PropertyType.GetDefaultValue());
            }
            return(attr.Value);
        }