private List <MethodCtorMatch> MatchMethodsToProperties(DecodedEntityClass entityInfo) { var nonReadOnlyPropertyInfo = PropertyInfos.Where(y => y.PropertyType != DtoPropertyTypes.ReadOnly) .Select(x => x.PropertyInfo).ToList(); _allPossibleSetterMatches = _matcher.GradeAllMethods(entityInfo.PublicSetterMethods, nonReadOnlyPropertyInfo, HowTheyWereAskedFor.DefaultMatchToProperties).ToList(); return(_allPossibleSetterMatches.Where(x => x.PropertiesMatch.Score >= PropertyMatch.PerfectMatchValue).ToList()); }
/// <summary> /// 通过反射设置属性值 /// </summary> /// <param name="key">属性名</param> /// <param name="value">值</param> void SetPropertyValue(string key, string value) { try { PropertyInfo propertyInfo = PropertyInfos.Where(x => x.Name.ToLower() == key.ToLower()).FirstOrDefault(); //获取指定名称的属性 if (propertyInfo != null) { propertyInfo.SetValue(request, value, null); } } catch { throw; } }
private string GetFields(FieldTypes fieldType) { string returnValue = null; switch (fieldType) { case FieldTypes.Fields: // Concatenated fields for SELECT command returnValue = string.Join(", ", PropertyInfos.Select(p => string.Format("[{0}]", p.Name)).ToArray()); break; case FieldTypes.Values: // Concatenated fields for INSERT command returnValue = string.Join(", ", PropertyInfos.Select(p => string.Format("@{0}", p.Name)).ToArray()); break; case FieldTypes.Updates: // Concatenated fields for UPDATE command returnValue = string.Join(", ", PropertyInfos.Where(p => p.Name != this.IdField).Select(p => string.Format("[{0}] = @{1}", p.Name, p.Name)).ToArray()); break; } return(returnValue); }
internal static List <IPatchBase> ReadPatchDocumentProperties <T>(JsonReader reader, string path, BindAttribute bindAttribute = null, Action <JsonReader> patchTypeAction = null, Action <IPatchPrimitive> keyAction = null) { if (reader == null) { return(null); } var modelTypeAssemblyName = typeof(T).AssemblyQualifiedName; bool cacheProperties; const string patchTypeKey = PatchArrayDocument <object> .PatchTypeKey; // ReSharper disable once AssignmentInConditionalExpression var modelProperties = (cacheProperties = PropertyInfos.All(x => x.Key.Key1 != modelTypeAssemblyName)) ? typeof(T).GetTypeInfo().GetProperties() : PropertyInfos.Where(x => x.Key.Key1 == modelTypeAssemblyName).Select(x => x.Value).ToArray(); var values = new List <IPatchBase>(); var startPath = reader.Path; bindAttribute = bindAttribute ?? typeof(T).GetAttribute <BindAttribute>(); path = string.IsNullOrWhiteSpace(path) ? string.Empty : path + "."; var keys = new List <IPatchPrimitive>(); while (reader.Read()) { if (reader.Path == startPath) { break; } if (reader.TokenType != JsonToken.PropertyName) { while (reader.Read()) { if (reader.Path == startPath) { return(null); } } } var readerValueAsName = (string)reader.Value; if (readerValueAsName == patchTypeKey) { if (patchTypeAction != null) { patchTypeAction(reader); } else { reader.Skip(); } continue; } var propertyInfo = modelProperties.FirstOrDefault(x => { var jsonPropertyNameAttribute = x.GetCustomAttribute <JsonPropertyAttribute>(); if (jsonPropertyNameAttribute != null) { return(string.Equals(jsonPropertyNameAttribute.PropertyName, readerValueAsName)); } return(readerValueAsName.Equals(new CamelCaseNamingStrategy().GetPropertyName(x.Name, false)) || readerValueAsName.Equals(x.Name) || readerValueAsName.Equals(new SnakeCaseNamingStrategy().GetPropertyName(x.Name, false))); }); // If property not found, we should report an error with it. if (propertyInfo == null) { values.Add(new PatchPrimitive <object> { Errors = new List <Error> { new Error { ErrorType = typeof(InvalidCastException), JsonName = readerValueAsName, JsonPath = reader.Path, Message = string.Format(Resources.UnknownProperty, readerValueAsName), Name = readerValueAsName, Path = reader.Path } }, Found = true, JsonName = readerValueAsName, JsonPath = reader.Path, Name = readerValueAsName, Path = reader.Path, Value = null }); // Since this is an unknown property, we should not parse it. reader.Skip(); continue; } var propertyName = propertyInfo.Name; var isKey = propertyInfo.GetCustomAttribute <KeyAttribute>() != null; var isBindable = IsBindable(bindAttribute, propertyName); if (!isBindable && (!isKey || keyAction == null)) { reader.Skip(); continue; } var value = (IPatchBase)SelectStrategyMethodInfo.MakeGenericMethod(propertyInfo.PropertyType) .Invoke(null, new object[] { reader, propertyName, path + propertyName, propertyInfo }); value.IgnoreApply = propertyInfo.GetCustomAttribute <IgnorePatchAttribute>() != null; if (isKey && keyAction != null) { keys.Add((IPatchPrimitive)value); } if (isBindable) { values.Add(value); } } foreach (var propertyInfo in modelProperties) { if (cacheProperties) { PropertyInfos.Add(modelTypeAssemblyName, propertyInfo.Name, propertyInfo); } if (values.Any(x => x.Name == propertyInfo.Name)) { continue; } var propertyName = propertyInfo.Name; var isKey = propertyInfo.GetCustomAttribute <KeyAttribute>() != null; var isBindable = IsBindable(bindAttribute, propertyName); if (!isBindable && (!isKey || keyAction == null)) { continue; } var value = (IPatchBase)SelectStrategyMethodInfo.MakeGenericMethod(propertyInfo.PropertyType) .Invoke(null, new object[] { null, propertyName, path + propertyName, propertyInfo }); value.IgnoreApply = propertyInfo.GetCustomAttribute <IgnorePatchAttribute>() != null; if (isKey && keyAction != null && keys.All(x => x.Name != value.Name)) { keys.Add((IPatchPrimitive)value); } if (isBindable) { values.Add(value); } } if (keys.Any()) { foreach (var key in keys) { // ReSharper disable once PossibleNullReferenceException keyAction.Invoke(key); } } return(values); }