Ejemplo n.º 1
0
        public static object Build(Type type, Dictionary <string, object> properties)
        {
            object obj = TypeSystem.CreateInstance(type);

            foreach (string key in properties.Keys)
            {
                object       item = properties[key];
                PropertyInfo settablePropertyInfo = TypeSystem.GetSettablePropertyInfo(type, key);
                if (settablePropertyInfo == null)
                {
                    FieldInfo fieldInfoFromPropertyName = TypeSystem.GetFieldInfoFromPropertyName(type, key);
                    if (fieldInfoFromPropertyName != null)
                    {
                        item = TypeSystem.ConvertEnumerableToCollection(item, fieldInfoFromPropertyName.FieldType);
                        fieldInfoFromPropertyName.SetValue(obj, item);
                    }
                    else
                    {
                        object[] assemblyQualifiedName = new object[2];
                        assemblyQualifiedName[0] = key;
                        assemblyQualifiedName[1] = type.AssemblyQualifiedName;
                        throw new ArgumentException(ExceptionHelpers.GetExceptionMessage(Resources.SettablePropertyNotFound, assemblyQualifiedName), "instance");
                    }
                }
                else
                {
                    item = TypeSystem.ConvertEnumerableToCollection(item, settablePropertyInfo.PropertyType);
                    settablePropertyInfo.SetValue(obj, item, null);
                }
            }
            return(obj);
        }
Ejemplo n.º 2
0
        public static object GetFieldValue(object instance, string propertyName)
        {
            object    value;
            Type      type = instance.GetType();
            FieldInfo fieldInfoFromPropertyName = TypeSystem.GetFieldInfoFromPropertyName(type, propertyName);

            if (fieldInfoFromPropertyName != null)
            {
                try
                {
                    value = fieldInfoFromPropertyName.GetValue(instance);
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    TraceHelper.Current.DebugMessage(exception.ToTraceMessage(string.Concat("GetFieldValue failed to parse property: ", propertyName)));
                    if (!exception.IsIgnorablePropertyException())
                    {
                        if (!exception.IsSevereException())
                        {
                            object[] message = new object[2];
                            message[0] = propertyName;
                            message[1] = exception.Message;
                            throw new ArgumentException(ExceptionHelpers.GetExceptionMessage(Resources.PropertyRetrievalFailed, message), exception);
                        }
                        else
                        {
                            throw;
                        }
                    }
                    else
                    {
                        value = null;
                    }
                }
                return(value);
            }
            else
            {
                return(null);
            }
        }