private static T GetMemberValueCore <T>(object origin, MemberToken[] members, Func <MemberGettingContext, MemberGettingResult> resolve, Action <object, MemberToken> onError) { var owner = origin; MemberGettingContext context = null; foreach (var member in members) { if (resolve == null) { object memberValue; if (!TryGetMemberValueCore(owner, member, out memberValue)) { onError(owner, member); return(default(T)); } owner = memberValue; } else { context = new MemberGettingContext(owner, member, context); MemberGettingResult result; if ((result = resolve(context)).Cancel) { return((T)result.Value); } owner = result.Value; } } return((T)owner); }
public static void SetMemberValue <T>(object origin, MemberToken[] members, Func <T> valueFactory, Func <MemberGettingContext, object> getter = null, Action <MemberSettingContext <T> > setter = null) { if (origin == null) { throw new ArgumentNullException(nameof(origin)); } if (members == null) { throw new ArgumentNullException(nameof(members)); } if (valueFactory == null) { throw new ArgumentNullException(nameof(valueFactory)); } if (members.Length == 0) { return; } var owner = origin; MemberGettingContext context = null; for (int i = 0; i < members.Length - 1; i++) { if (getter == null) { if (!TryGetMemberValueCore(owner, members[i], out owner)) { throw new ArgumentException(string.Format("The '{0}' member is not exists in the '{1}' type.", members[i], (owner is Type ? ((Type)owner).FullName : owner.GetType().FullName))); } } else { //创建获取操作上下文对象 context = new MemberGettingContext(owner, members[i], context); //执行获取操作 owner = getter(context); if (owner == null) { throw new InvalidOperationException($"The result of getter is null."); } } } if (setter == null) { SetMemberValueCore(owner, members[members.Length - 1], valueFactory, true); } else { setter(new MemberSettingContext <T>(owner, members[members.Length - 1], valueFactory, context)); } }
internal MemberGettingContext(object owner, MemberToken member, MemberGettingContext parent = null) { if(owner == null) throw new ArgumentNullException(nameof(owner)); if(member == null) throw new ArgumentNullException(nameof(member)); _owner = owner; _parent = parent; _memberToken = member; }
public static bool TrySetMemberValue <T>(object origin, MemberToken[] members, Func <T> valueFactory, Func <MemberGettingContext, object> getter = null, Action <MemberSettingContext <T> > setter = null) { if (origin == null || members == null || valueFactory == null || members.Length == 0) { return(false); } var owner = origin; MemberGettingContext context = null; for (int i = 0; i < members.Length - 1; i++) { if (getter == null) { if (!TryGetMemberValueCore(owner, members[i], out owner)) { return(false); } } else { //创建获取操作上下文对象 context = new MemberGettingContext(owner, members[i], context); //执行获取操作 owner = getter(context); if (owner == null) { return(false); } } } if (setter == null) { SetMemberValueCore(owner, members[members.Length - 1], valueFactory, false); } else { setter(new MemberSettingContext <T>(owner, members[members.Length - 1], valueFactory, context)); } return(true); }
internal MemberSettingContext(object owner, MemberToken memberToken, Func <T> valueFactory, MemberGettingContext parent = null) : base(owner, memberToken, parent) { if (valueFactory == null) { throw new ArgumentNullException(nameof(valueFactory)); } _valueFactory = valueFactory; }
internal MemberSettingContext(object owner, MemberToken memberToken, T value, MemberGettingContext parent = null) : base(owner, memberToken, parent) { _value = value; _valueEvaluated = 1; }
internal MemberGettingContext(object owner, MemberPathSegment memberSegment, MemberGettingContext parent = null) { _owner = owner ?? throw new ArgumentNullException(nameof(owner)); _parent = parent; _memberSegment = memberSegment; }