Пример #1
0
        public static void SetValue(object target, string[] memberNames, Func <object> valueThunk, Action <ObjectResolvingContext> resolve)
        {
            if (valueThunk == null)
            {
                throw new ArgumentNullException("valueThunk");
            }

            if (target == null || memberNames == null || memberNames.Length < 1)
            {
                return;
            }

            object container = target;

            if (memberNames.Length > 1)
            {
                container = GetValue(target, memberNames, 0, memberNames.Length - 1, resolve);

                if (container == null)
                {
                    throw new InvalidOperationException(string.Format("The '{0}' member value is null of '{1}' type.", string.Join(".", memberNames, 0, memberNames.Length - 1), target.GetType().FullName));
                }
            }

            //创建构件解析上下文对象
            var context = new ObjectResolvingContext(target, container, memberNames[memberNames.Length - 1], valueThunk, string.Join(".", memberNames));

            //调用解析回调方法
            if (resolve == null)
            {
                DefaultResolve(context);
            }
            else
            {
                resolve(context);

                if (!context.Handled)
                {
                    DefaultResolve(context);
                }
            }

            var member = context.Member;

            if (member == null)
            {
                throw new KeyNotFoundException(string.Format("The '{0}' member is not exists in the '{1}' type.", context.MemberName, context.Container.GetType().FullName));
            }

            switch (member.MemberType)
            {
            case MemberTypes.Field:
                ((FieldInfo)member).SetValue(context.Container, JF.Common.Convert.ConvertValue(context.Value, ((FieldInfo)member).FieldType));
                break;

            case MemberTypes.Property:
                ((PropertyInfo)member).SetValue(context.Container, JF.Common.Convert.ConvertValue(context.Value, ((PropertyInfo)member).PropertyType), null);
                break;
            }
        }
Пример #2
0
        public static object GetValue(object target, string[] memberNames, int start, int length, Action <ObjectResolvingContext> resolve)
        {
            if (target == null || memberNames == null || memberNames.Length < 1)
            {
                return(target);
            }

            if (start < 0 || start >= memberNames.Length)
            {
                throw new ArgumentOutOfRangeException("start");
            }

            //创建解析上下文对象
            ObjectResolvingContext context = new ObjectResolvingContext(target, string.Join(".", memberNames));

            for (int i = 0; i < Math.Min(memberNames.Length - start, length); i++)
            {
                string memberName = memberNames[start + i];

                if (memberName == null || memberName.Trim().Length < 1)
                {
                    continue;
                }

                if (context.Value == null)
                {
                    continue;
                }

                context.Handled    = true;
                context.MemberName = memberName;
                context.Container  = context.Value;
                context.Value      = null;

                //解析对象成员
                if (resolve == null)
                {
                    DefaultResolve(context);
                }
                else
                {
                    resolve(context);

                    if (!context.Handled)
                    {
                        DefaultResolve(context);
                    }
                }

                //如果解析被终止则退出后续解析
                if (context.IsTerminated)
                {
                    break;
                }
            }

            return(context.Value);
        }