/// <summary>Tries the get an EasyField from the backing of a property</summary> /// <param name="property">The property</param> /// <param name="easyField">The easy field</param> /// <returns>If it could find a field to use</returns> public bool TryGet(PropertyInfo property, out EasyField easyField) { // TODO: make it easier to search for different strings var members = property.DeclaringType .GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); var name = property.Name; var search = new string[_stringTransformers.Length]; for (var i = 0; i < _stringTransformers.Length; i++) { search[i] = _stringTransformers[i](name); } foreach (var i in members) { if (search.Contains(i.Name) && i is FieldInfo field) { easyField = new EasyField(field); return(true); } } easyField = default; return(false); }
/// <summary>Tries to get an EasyField from the backing of a property</summary> /// <typeparam name="TSource">The type of the source</typeparam> /// <typeparam name="TProperty">The type of the property</typeparam> /// <param name="property">The <see cref="MemberInfo"/> to use (must be a <see cref="FieldInfo"/> or a <see cref="PropertyInfo"/></param> /// <param name="easyField">The easy field</param> /// <returns>If it could find a field to use</returns> /// <exception cref="System.ArgumentException">member</exception> public bool TryGet <TSource, TProperty>(PropertyInfo property, out EasyField <TSource, TProperty> easyField) { var res = TryGet(property, out EasyField ef); if (ef == default) { easyField = default; return(res); } easyField = new EasyField <TSource, TProperty>(ef.Field); return(res); }
/// <summary>Tries to get an EasyField from a field/property</summary> /// <typeparam name="TSource">The type of the source</typeparam> /// <typeparam name="TProperty">The type of the property</typeparam> /// <param name="propertyLambda"><code>e => e.MyProperty</code> syntax sugar</param> /// <param name="easyField">The easy field</param> /// <exception cref="System.ArgumentException">Expression '<paramref name="propertyLambda"/>' refers to a method, not a property/field</exception> /// <exception cref="System.ArgumentException">Expression '<paramref name="propertyLambda"/>' refers to a member that is not from the type <typeparamref name="TSource"/></exception> /// <exception cref="System.ArgumentException">member</exception> /// <returns>If it could find a field to use</returns> public bool TryGet <TSource, TProperty>(Expression <Func <TSource, TProperty> > propertyLambda, out EasyField <TSource, TProperty> easyField) { var member = propertyLambda.GetMemberInfo(); if (member is PropertyInfo propertyInfo) { return(TryGet(propertyInfo, out easyField)); } else if (member is FieldInfo fieldInfo) { easyField = new EasyField <TSource, TProperty>(fieldInfo); return(true); } // this shouldn't happen... throw new Exception($"Unsupported item"); }