internal static bool TryGetMemberValueCore(object owner, MemberToken token, out object value) { value = null; var memberInfo = GetMemberInfo(owner, token); if (memberInfo == null) { return(false); } switch (memberInfo.MemberType) { case MemberTypes.Field: value = ((FieldInfo)memberInfo).GetValue(owner); return(true); case MemberTypes.Property: value = ((PropertyInfo)memberInfo).GetValue(owner, token.Parameters); return(true); case MemberTypes.Method: value = ((MethodInfo)memberInfo).Invoke(owner, token.Parameters); return(true); } return(false); }
internal static void SetMemberValueCore <T>(object owner, MemberToken member, Func <T> valueFactory) { if (owner == null) { throw new ArgumentNullException(nameof(owner)); } var type = owner is Type ? (Type)owner : owner.GetType(); var memberInfo = GetMemberInfo(type, member); if (memberInfo == null) { throw new ArgumentException(string.Format("The '{0}' member is not exists in the '{1}' type.", member, (owner is Type ? ((Type)owner).FullName : owner.GetType().FullName))); } switch (memberInfo.MemberType) { case MemberTypes.Field: ((FieldInfo)memberInfo).SetValue(owner, valueFactory()); break; case MemberTypes.Property: ((PropertyInfo)memberInfo).SetValue(owner, valueFactory()); break; default: throw new InvalidOperationException($"Dont support set value of '{member}' member in the '{type}' type."); } }
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 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 MemberToken GetMember(Type type, string path) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (string.IsNullOrEmpty(path)) { return(null); } var parts = path.Split('.'); var members = this.GetMembers(type); MemberToken member = null; for (int i = 0; i < parts.Length; i++) { var part = parts[i].Trim(); if (string.IsNullOrEmpty(part) || part == "*") { var elementType = Common.TypeExtension.GetCollectionElementType(member == null ? type : member.Type); if (elementType == null) { return(null); } members = this.GetMembers(elementType); } else { if (members != null && members.TryGet(part, out member)) { members = this.GetMembers(member.Type); } else { return(null); } } } return(member); }
internal static MemberInfo GetMemberInfo(object owner, MemberToken token) { if (owner == null) { return(null); } var type = owner is Type ? (Type)owner : owner.GetType(); var members = type.FindMembers(MemberTypes.Field | MemberTypes.Property, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, (member, criteria) => { MemberToken memberToken = (MemberToken)criteria; if (memberToken.IsIndexer && member.MemberType == MemberTypes.Property) { var parameters = ((PropertyInfo)member).GetIndexParameters(); if (parameters.Length != memberToken.Parameters.Length) { return(false); } for (int i = 0; i < parameters.Length; i++) { if (parameters[i].ParameterType != memberToken.Parameters[i].GetType()) { return(false); } } return(true); } return(string.Equals(member.Name, memberToken.Name, StringComparison.OrdinalIgnoreCase)); }, token); if (members != null && members.Length > 0) { return(members[0]); } return(null); }
internal MemberSettingContext(object owner, MemberToken memberToken, T value, MemberGettingContext parent = null) : base(owner, memberToken, parent) { _value = value; _valueEvaluated = 1; }