コード例 #1
0
        private static object GetNestedObjectInstance(object component, string propertyPath, bool autoCreate)
        {
            string propertyName;

            if (propertyPath.Contains("."))
            {
                propertyName = propertyPath.Substring(0, propertyPath.IndexOf("."));
            }
            else
            {
                return(component);
            }

            IDynamicAccessor dynamicAccessor = DynamicAccessorFactory.GetDynamicAccessor(component.GetType());
            object           value           = dynamicAccessor.GetPropertyValue(component, propertyName);

            if (value == null)
            {
                if (autoCreate)
                {
                    PropertyDescriptor descriptor = ReflectionHelper.GetPropertyDescriptorFromPath(component.GetType(), propertyName);
                    value = Activator.CreateInstance(descriptor.PropertyType);
                    dynamicAccessor.SetPropertyValue(component, propertyName, value);
                }
                else
                {
                    return(value);
                }
            }
            return(GetNestedObjectInstance(value, propertyPath.Substring(propertyPath.IndexOf(".") + 1), autoCreate));
        }
コード例 #2
0
 public InterceptorContext(MethodInfo method, object target, IReadOnlyList <IDynamicAccessor> parameters, IDynamicAccessor returnValue, IDictionary <object, object> context)
 {
     Method      = method;
     Target      = target;
     Parameters  = parameters;
     ReturnValue = returnValue;
     Context     = context;
 }
コード例 #3
0
		public MetaAccessor (MemberInfo member)
		{
			Precondition.Require(member, () => Error.ArgumentNull("member"));
			
			Type memberType = member.GetMemberType();
			Type deferredType = GetDeferredType(memberType);
			
			_accessor = CreateAccessor(member);
			_isDeferred = (deferredType != null);
			_type = deferredType ?? memberType;
		}
コード例 #4
0
        public MetaAccessor(MemberInfo member)
        {
            Precondition.Require(member, () => Error.ArgumentNull("member"));

            Type memberType   = member.GetMemberType();
            Type deferredType = GetDeferredType(memberType);

            _accessor   = CreateAccessor(member);
            _isDeferred = (deferredType != null);
            _type       = deferredType ?? memberType;
        }
コード例 #5
0
        private IDynamicAccessor GetAccessorAndValidate(string memberName)
        {
            IDynamicAccessor accessor = GetAccessor(memberName);

            if (accessor == null)
            {
                throw Error.MissingMember(_type, memberName);
            }

            return(accessor);
        }
コード例 #6
0
        /// <summary>
        /// Determines whether the specified instance is valid.
        /// </summary>
        /// <param name="instance">The instance declaring the property to be validated</param>
        /// <returns>
        ///     <c>true</c> if the specified instance is valid; otherwise, <c>false</c>.
        /// </returns>
        public bool IsValid(object instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            IDynamicAccessor DynamicAccessor = DynamicAccessorFactory.GetDynamicAccessor(instance.GetType());

            object value = DynamicAccessor.GetPropertyValue(instance, _propertyName);

            return(Validate(value));
        }
コード例 #7
0
        public virtual TValue GetValue <TValue>(string memberName,
                                                TValue defaultValue, IFormatProvider provider)
        {
            IDynamicAccessor accessor = GetAccessor(memberName);

            if (accessor == null)
            {
                return(defaultValue);
            }

            return(Converter.ChangeType <TValue>(accessor.GetValue(_instance), provider));
        }
コード例 #8
0
 public object this[string memberName]
 {
     get
     {
         IDynamicAccessor accessor = GetAccessorAndValidate(memberName);
         return(accessor.GetValue(_instance));
     }
     set
     {
         IDynamicAccessor accessor = GetAccessorAndValidate(memberName);
         accessor.SetValue(_instance, value);
     }
 }
コード例 #9
0
        private static void GenerateXmlAttributes <T>(XmlWriter writer, IDynamicAccessor accessor, T instance) where T : IChartObject
        {
            foreach (var propertyName in accessor.GetPropertyNames())
            {
                var propertyValue = accessor.GetPropertyValue(instance, propertyName);
                if (propertyValue != null)
                {
                    Type propertyType = accessor.GetPropertyType(propertyName);

                    if (propertyType.IsSimpleType() || propertyType.IsValueType)
                    {
                        if (IsDebugEnabled)
                        {
                            log.Debug("Property name={0}, type={1}, value={2}", propertyName, propertyType, propertyValue);
                        }

                        // NOTE : Color인 경우는 HexString으로, Enum 값은 HashCode 값으로...
                        if (propertyType == typeof(Color?))
                        {
                            var color = (Color?)propertyValue;
                            writer.WriteAttributeString(propertyName, color.Value.ToHexString());
                        }
                        else if (propertyType.IsEnum)
                        {
                            writer.WriteAttributeString(propertyName, propertyValue.GetHashCode().ToString());
                        }
                        else
                        {
                            writer.WriteAttributeString(propertyName, propertyValue.ToString());
                        }
                    }
                    else if (propertyType.IsSameOrSubclassOf(typeof(ChartAttributeBase)))
                    {
                        var accessor2 = DynamicAccessorFactory.CreateDynamicAccessor(propertyType, false);
                        GenerateXmlAttributes(writer, accessor2, propertyValue as ChartAttributeBase);
                    }
                    else if (TypeTool.IsSameOrSubclassOrImplementedOf(propertyType, typeof(IEnumerable)))
                    {
                        // Nothing to do.
                    }
                    else
                    {
                        throw new NotSupportedException(string.Format("지원하지 않는 속성입니다.  property name={0}, type={1}", propertyName,
                                                                      propertyType.FullName));
                    }
                }
            }
        }
コード例 #10
0
 static IndexableCollection()
 {
     Accessor      = DynamicAccessorFactory.CreateDynamicAccessor <T>(MapPropertyOptions.Safety);
     PropertyInfos = Accessor.PropertyMap.Values.ToArray();
     PropertyNames = Accessor.GetPropertyNames();
 }
コード例 #11
0
 /// <summary>
 /// 객체의 모든 속성에 대해 속성명-속성값 형식을 반환합니다.
 /// </summary>
 /// <typeparam name="T">대상 객체의 수형</typeparam>
 /// <param name="accessor">IDynamicAccessor{T} 인스턴스</param>
 /// <param name="target">대상 객체</param>
 /// <returns></returns>
 public static IEnumerable <KeyValuePair <string, object> > GetPropertyNameValueCollection <T>(this IDynamicAccessor <T> accessor,
                                                                                               T target)
 {
     return
         (accessor.GetPropertyNames()
          .Select(name => new KeyValuePair <string, object>(name, accessor.GetPropertyValue(target, name))));
 }
コード例 #12
0
 /// <summary>
 /// 객체의 모든 필드 정보를 필드명-필드값 형식으로 반환합니다.
 /// </summary>
 /// <param name="accessor">IDynamicAccessor 인스턴스</param>
 /// <param name="target">대상 객체</param>
 /// <returns></returns>
 public static IEnumerable <KeyValuePair <string, object> > GetFieldNameValueCollection(this IDynamicAccessor accessor,
                                                                                        object target)
 {
     return
         (accessor.GetFieldNames()
          .Select(name => new KeyValuePair <string, object>(name, accessor.GetFieldValue(target, name))));
 }