예제 #1
0
        /// <inheritdoc />
        public override bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            if (TrySetResultForNullableOperator(selectorInfo))
            {
                return(true);
            }

            if (selectorInfo.CurrentValue is not string currentValue)
            {
                return(false);
            }
            var selector = selectorInfo.SelectorText;

            _cultureInfo = GetCulture(selectorInfo.FormatDetails);

            // Search is case-insensitive
            if (!SelectorMethods.TryGetValue(selector, out var method))
            {
                return(false);
            }

            // Check if the Selector must match case-sensitive
            if (selectorInfo.FormatDetails.Settings.CaseSensitivity == CaseSensitivityType.CaseSensitive &&
                method.Method.Name != selector)
            {
                return(false);
            }

            return(method.Invoke(selectorInfo, currentValue));
        }
예제 #2
0
        /// <inheritdoc />
        public override bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            // Check for nullable and null value
            var current = selectorInfo.CurrentValue switch
            {
                JObject jsonObject => jsonObject.HasValues ? jsonObject : null,
                JValue jsonValue => jsonValue.Value,
                _ => selectorInfo.CurrentValue
            };

            if (TrySetResultForNullableOperator(selectorInfo))
            {
                return(true);
            }

            if (current is null)
            {
                return(false);
            }

            return(selectorInfo.CurrentValue switch
            {
                // Note: Operators are processed by ListFormatter

                JObject jObject => TryEvaluateJObject(jObject, selectorInfo),
                JToken jToken => TryEvaluateJToken(jToken, selectorInfo), // JValue derives from JToken
                _ => false
            });
예제 #3
0
        /// <summary>
        /// Performs the default index-based selector, same as String.Format.
        /// </summary>
        public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            var current       = selectorInfo.CurrentValue;
            var selector      = selectorInfo.SelectorText;
            var formatDetails = selectorInfo.FormatDetails;

            int selectorValue;

            if (int.TryParse(selector, out selectorValue))
            {
                // Argument Index:
                // Just like String.Format, the arg index must be in-range,
                // should be the first item, and shouldn't have any operator:
                if (selectorInfo.SelectorIndex == 0 &&
                    selectorValue < formatDetails.OriginalArgs.Length &&
                    selectorInfo.SelectorOperator == "")
                {
                    // This selector is an argument index.
                    selectorInfo.Result = formatDetails.OriginalArgs[selectorValue];
                    return(true);
                }

                // Alignment:
                // An alignment item should be preceded by a comma
                if (selectorInfo.SelectorOperator == ",")
                {
                    // This selector is actually an Alignment modifier.
                    selectorInfo.Placeholder.Alignment = selectorValue;
                    selectorInfo.Result = current; // (don't change the current item)
                    return(true);
                }
            }

            return(false);
        }
예제 #4
0
        public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            if (!(selectorInfo is FormattingInfo formattingInfo))
            {
                return(false);
            }
            if (!(formattingInfo.CurrentValue is SmartObjects smartObjects))
            {
                return(false);
            }

            var savedCurrentValue = formattingInfo.CurrentValue;

            foreach (var obj in smartObjects)
            {
                foreach (var sourceExtension in _formatter.SourceExtensions)
                {
                    formattingInfo.CurrentValue = obj;
                    var handled = sourceExtension.TryEvaluateSelector(formattingInfo);
                    if (handled)
                    {
                        formattingInfo.CurrentValue = savedCurrentValue;
                        return(true);
                    }
                }
            }

            formattingInfo.CurrentValue = savedCurrentValue;

            return(false);
        }
예제 #5
0
        public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            if (!(selectorInfo is FormattingInfo formattingInfo))
            {
                return(false);
            }
            if (!(formattingInfo.CurrentValue != null && formattingInfo.CurrentValue.IsValueTuple()))
            {
                return(false);
            }

            var savedCurrentValue = formattingInfo.CurrentValue;

            foreach (var obj in formattingInfo.CurrentValue.GetValueTupleItemObjectsFlattened())
            {
                foreach (var sourceExtension in _formatter.SourceExtensions)
                {
                    formattingInfo.CurrentValue = obj;
                    var handled = sourceExtension.TryEvaluateSelector(formattingInfo);
                    if (handled)
                    {
                        formattingInfo.CurrentValue = savedCurrentValue;
                        return(true);
                    }
                }
            }

            formattingInfo.CurrentValue = savedCurrentValue;

            return(false);
        }
예제 #6
0
        /// <summary>
        /// Converts the first character of each word to an uppercase character.
        /// </summary>
        private bool CapitalizeWords(ISelectorInfo selectorInfo, string currentValue)
        {
            if (string.IsNullOrEmpty(currentValue))
            {
                selectorInfo.Result = currentValue;
                return(true);
            }

            var textArray     = currentValue.ToCharArray();
            var previousSpace = true;

            for (var i = 0; i < textArray.Length; i++)
            {
                var c = textArray[i];
                if (char.IsWhiteSpace(c))
                {
                    previousSpace = true;
                }
                else if (previousSpace && char.IsLetter(c))
                {
                    textArray[i]  = char.ToUpper(c, _cultureInfo);
                    previousSpace = false;
                }
            }

            selectorInfo.Result = new string(textArray);
            return(true);
        }
예제 #7
0
		/// <summary>
		/// Performs the default index-based selector, same as String.Format.
		/// </summary>
		public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
		{
			var current = selectorInfo.CurrentValue;
			var selector = selectorInfo.SelectorText;
			var formatDetails = selectorInfo.FormatDetails;

			int selectorValue;
			if (int.TryParse(selector, out selectorValue))
			{
				// Argument Index:
				// Just like String.Format, the arg index must be in-range,
				// should be the first item, and shouldn't have any operator:
				if (selectorInfo.SelectorIndex == 0
					&& selectorValue < formatDetails.OriginalArgs.Length
					&& selectorInfo.SelectorOperator == "")
				{
					// This selector is an argument index.
					selectorInfo.Result = formatDetails.OriginalArgs[selectorValue];
					return true;
				}

				// Alignment:
				// An alignment item should be preceded by a comma
				if (selectorInfo.SelectorOperator == ",")
				{
					// This selector is actually an Alignment modifier.
					selectorInfo.Placeholder.Alignment = selectorValue;
					selectorInfo.Result = current; // (don't change the current item)
					return true;
				}

			}

			return false;
		}
예제 #8
0
 public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
 {
     // Note: Operators are processed by ListFormatter
     return(selectorInfo.CurrentValue switch
     {
         JObject _ => NewtonSoftJson.TryEvaluateSelector(selectorInfo),
         JsonElement _ => SystemTextJson.TryEvaluateSelector(selectorInfo),
         _ => false
     });
예제 #9
0
            // Note: Operators are processed by ListFormatter
            public static bool TryEvaluateSelector(ISelectorInfo selectorInfo)
            {
                var jsonElement = (JsonElement)selectorInfo.CurrentValue;

                var         je = jsonElement.Clone();
                JsonElement targetElement;

                if (selectorInfo.FormatDetails.Settings.CaseSensitivity == SmartFormat.Core.Settings.CaseSensitivityType.CaseInsensitive)
                {
                    targetElement = je.EnumerateObject().FirstOrDefault(jp => jp.Name.Equals(selectorInfo.SelectorText,
                                                                                             selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison())).Value;
                }
                else
                {
                    targetElement = je.GetProperty(selectorInfo.SelectorText);
                }

                switch (targetElement.ValueKind)
                {
                case JsonValueKind.Undefined:
                    throw new FormatException($"'{selectorInfo.SelectorText}'");

                case JsonValueKind.Null:
                    selectorInfo.Result = null;
                    break;

                case JsonValueKind.Number:
                    selectorInfo.Result = targetElement.GetDouble();
                    break;

                case JsonValueKind.False:
                    selectorInfo.Result = false;
                    break;

                case JsonValueKind.True:
                    selectorInfo.Result = true;
                    break;

                case JsonValueKind.String:
                    selectorInfo.Result = targetElement.GetString();
                    break;

                case JsonValueKind.Object:
                    selectorInfo.Result = targetElement;
                    break;

                case JsonValueKind.Array:
                    selectorInfo.Result = targetElement.EnumerateArray().ToArray();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                return(true);
            }
예제 #10
0
        /// <summary>
        /// If any of the <see cref="Placeholder"/>'s <see cref="Placeholder.Selectors"/> has
        /// nullable <c>?</c> as their first operator, and <see cref="ISelectorInfo.CurrentValue"/>
        /// is <see langword="null"/>, <see cref="ISelectorInfo.Result"/> will be set to <see langword="null"/>.
        /// </summary>
        /// <param name="selectorInfo"></param>
        /// <returns>
        /// <see langword="true"/>, if any of the <see cref="Placeholder"/>'s
        /// <see cref="Placeholder.Selectors"/> has  nullable <c>?</c> as their first
        /// operator, and <see cref="ISelectorInfo.CurrentValue"/> is <see langword="null"/>.
        /// </returns>
        /// <remarks>
        /// The nullable operator '?' can be followed by a dot (like '?.') or a square brace (like '.[')
        /// </remarks>
        protected virtual bool TrySetResultForNullableOperator(ISelectorInfo selectorInfo)
        {
            if (HasNullableOperator(selectorInfo) && selectorInfo.CurrentValue is null)
            {
                selectorInfo.Result = null;
                return(true);
            }

            return(false);
        }
예제 #11
0
            // Note: Operators are processed by ListFormatter
            public static bool TryEvaluateSelector(ISelectorInfo selectorInfo)
            {
                var jsonObject = (JObject)selectorInfo.CurrentValue;

                var result = jsonObject.GetValue(selectorInfo.SelectorText,
                                                 selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison());

                selectorInfo.Result = result ?? throw new FormatException($"'{selectorInfo.SelectorText}'");
                return(true);
            }
예제 #12
0
            bool ISource.TryEvaluateSelector(ISelectorInfo selectorInfo)
            {
                var value = _values?.TryGetValue(selectorInfo.Selector.Text);

                if (value == null)
                {
                    return(false);
                }

                selectorInfo.Result = value;
                return(true);
            }
예제 #13
0
			bool ISource.TryEvaluateSelector(ISelectorInfo selectorInfo)
			{
				if (_values == null)
					return false;

				var value = _values.TryGetValue(selectorInfo.Selector.Text);

				if (value == null)
					return false;

				selectorInfo.Result = value;
				return true;
			}
예제 #14
0
        /// <inheritdoc />
        public override bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            // Check for nullable and null value
            var current = selectorInfo.CurrentValue switch
            {
                JsonElement jsonElement => jsonElement.ValueKind == JsonValueKind.Null ? null : jsonElement,
                _ => selectorInfo.CurrentValue
            };

            if (TrySetResultForNullableOperator(selectorInfo))
            {
                return(true);
            }

            if (current is not JsonElement element)
            {
                return(false);
            }

            var je = element.Clone();

            JsonElement targetElement;

            if (selectorInfo.FormatDetails.Settings.CaseSensitivity == SmartFormat.Core.Settings.CaseSensitivityType.CaseInsensitive)
            {
                targetElement = je.EnumerateObject().FirstOrDefault(jp => jp.Name.Equals(selectorInfo.SelectorText,
                                                                                         selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison())).Value;
            }
            else
            {
                targetElement = je.GetProperty(selectorInfo.SelectorText !);
            }

            selectorInfo.Result = targetElement.ValueKind switch
            {
                JsonValueKind.Undefined => throw new FormatException($"'{selectorInfo.SelectorText}'"),
                      JsonValueKind.Null => null,
                      JsonValueKind.Number => targetElement.GetDouble(),
                      JsonValueKind.False => false,
                      JsonValueKind.True => true,
                      JsonValueKind.String => targetElement.GetString(),
                      JsonValueKind.Object => targetElement,
                      JsonValueKind.Array => targetElement.EnumerateArray().ToArray(),
                      _ => throw new ArgumentOutOfRangeException(nameof(selectorInfo), $"Unexpected kind of '{nameof(JsonValueKind)}'")
            };

            return(true);
        }
    }
}
예제 #15
0
        public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            // Note: Operators are processed by ListFormatter

            if (!(selectorInfo.CurrentValue is JObject jsonObject))
            {
                return(false);
            }

            var result = jsonObject.GetValue(selectorInfo.SelectorText,
                                             selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison());

            selectorInfo.Result = result ?? throw new FormatException($"'{selectorInfo.SelectorText}'");
            return(true);
        }
예제 #16
0
        public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            // Note: Operators are processed by ListFormatter
            switch (selectorInfo.CurrentValue)
            {
            case JObject _:
                return(NewtonSoftJson.TryEvaluateSelector(selectorInfo));

            case JsonElement _:
                return(SystemTextJson.TryEvaluateSelector(selectorInfo));

            default:
                return(false);
            }
        }
예제 #17
0
        /// <inheritdoc />>
        public override bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            var current = selectorInfo.CurrentValue;

            if (TrySetResultForNullableOperator(selectorInfo))
            {
                return(true);
            }

            if (current is null)
            {
                return(false);
            }

            var selector = selectorInfo.SelectorText;

            // See if current is an IDictionary and contains the selector:
            if (current is IDictionary rawDict)
            {
                foreach (DictionaryEntry entry in rawDict)
                {
                    var key = entry.Key as string ?? entry.Key.ToString() !;

                    if (key.Equals(selector, selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison()))
                    {
                        selectorInfo.Result = entry.Value;
                        return(true);
                    }
                }
            }

            // this check is for dynamics and generic dictionaries
            if (current is not IDictionary <string, object?> dict)
            {
                return(false);
            }

            // We're using the CaseSensitivityType of the dictionary,
            // not the one from Settings.GetCaseSensitivityComparison().
            // This is faster and has less GC than Key.Equals(...)
            if (!dict.TryGetValue(selector, out var val))
            {
                return(false);
            }

            selectorInfo.Result = val;
            return(true);
        }
예제 #18
0
        private bool Capitalize(ISelectorInfo selectorInfo, string currentValue)
        {
            if (currentValue.Length < 1 || char.IsUpper(currentValue[0]))
            {
                selectorInfo.Result = currentValue;
                return(true);
            }

            if (currentValue.Length < 2)
            {
                selectorInfo.Result = char.ToUpper(currentValue[0], _cultureInfo);
                return(true);
            }

            selectorInfo.Result = char.ToUpper(currentValue[0], _cultureInfo) + currentValue.Substring(1);
            return(true);
        }
예제 #19
0
        /// <summary>
        /// This allows an integer to be used as a selector to index an array (or list).
        /// This is better described using an example:
        /// CustomFormat("{Dates.2.Year}", {#1/1/2000#, #12/31/2999#, #9/9/9999#}) = "9999"
        /// The ".2" selector is used to reference Dates[2].
        /// </summary>
        public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            var current  = selectorInfo.CurrentValue;
            var selector = selectorInfo.SelectorText;

            if (!(current is IList currentList))
            {
                return(false);
            }

            // See if we're trying to access a specific index:
            var isAbsolute = selectorInfo.SelectorIndex == 0 && selectorInfo.SelectorOperator.Length == 0;

            if (!isAbsolute && int.TryParse(selector, out var itemIndex) &&
                itemIndex < currentList.Count)
            {
                // The current is a List, and the selector is a number;
                // let's return the List item:
                // Example: {People[2].Name}
                //           ^List  ^itemIndex
                selectorInfo.Result = currentList[itemIndex];
                return(true);
            }


            // We want to see if there is an "Index" property that was supplied.
            if (selector.Equals("index", StringComparison.OrdinalIgnoreCase))
            {
                // Looking for "{Index}"
                if (selectorInfo.SelectorIndex == 0)
                {
                    selectorInfo.Result = CollectionIndex;
                    return(true);
                }

                // Looking for 2 lists to sync: "{List1: {List2[Index]} }"
                if (0 <= CollectionIndex && CollectionIndex < currentList.Count)
                {
                    selectorInfo.Result = currentList[CollectionIndex];
                    return(true);
                }
            }

            return(false);
        }
예제 #20
0
		public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
		{
			var element = selectorInfo.CurrentValue as XElement;
			if (element != null)
			{
				var selector = selectorInfo.SelectorText;
				// Find elements that match a selector
				var selectorMatchedElements = element.Elements()
					.Where(x => x.Name.LocalName == selector).ToList();
				if (selectorMatchedElements.Any())
				{
					selectorInfo.Result = selectorMatchedElements;
					return true;
				}
			}

			return false;
		}
예제 #21
0
        public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            var element = selectorInfo.CurrentValue as XElement;

            if (element != null)
            {
                var selector = selectorInfo.SelectorText;
                // Find elements that match a selector
                var selectorMatchedElements = element.Elements()
                                              .Where(x => x.Name.LocalName == selector).ToList();
                if (selectorMatchedElements.Any())
                {
                    selectorInfo.Result = selectorMatchedElements;
                    return(true);
                }
            }

            return(false);
        }
예제 #22
0
        /// <inheritdoc />
        public override bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            if (TrySetResultForNullableOperator(selectorInfo))
            {
                return(true);
            }

            switch (selectorInfo.CurrentValue)
            {
            case null:
                return(false);

            case KeyValuePair <string, object?> kvp when kvp.Key == selectorInfo.SelectorText:
                selectorInfo.Result = kvp.Value;
                return(true);

            default:
                return(false);
            }
        }
예제 #23
0
        /// <inheritdoc />
        public override bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            var selector      = selectorInfo.SelectorText;
            var formatDetails = selectorInfo.FormatDetails;

            if (int.TryParse(selector, out var selectorValue) &&
                selectorInfo.SelectorIndex == 0 &&
                selectorValue < formatDetails.OriginalArgs.Count &&
                selectorInfo.SelectorOperator == string.Empty)
            {
                // Argument Index:
                // Just like string.Format, the arg index must be in-range,
                // must be the first item, and shouldn't have any operator
                // This selector is an argument index.
                // Get the value from arguments.
                selectorInfo.Result = formatDetails.OriginalArgs[selectorValue];
                return(true);
            }

            return(false);
        }
예제 #24
0
		/// <summary>
		/// This allows an integer to be used as a selector to index an array (or list).
		///
		/// This is better described using an example:
		/// CustomFormat("{Dates.2.Year}", {#1/1/2000#, #12/31/2999#, #9/9/9999#}) = "9999"
		/// The ".2" selector is used to reference Dates[2].
		/// </summary>
		public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
		{
			var current = selectorInfo.CurrentValue;
			var selector = selectorInfo.SelectorText;

			// See if we're trying to access a specific index:
			int itemIndex;
			var currentList = current as IList;
			var isAbsolute = (selectorInfo.SelectorIndex == 0 && selectorInfo.SelectorOperator.Length == 0);
			if (!isAbsolute && currentList != null && int.TryParse(selector, out itemIndex) && itemIndex < currentList.Count)
			{
				// The current is a List, and the selector is a number;
				// let's return the List item:
				// Example: {People[2].Name}
				//		   ^List  ^itemIndex
				selectorInfo.Result = currentList[itemIndex];
				return true;
			}


			// We want to see if there is an "Index" property that was supplied.
			if (selector.Equals("index", StringComparison.OrdinalIgnoreCase))
			{
				// Looking for "{Index}"
				if (selectorInfo.SelectorIndex == 0)
				{
					selectorInfo.Result = CollectionIndex;
					return true;
				}

				// Looking for 2 lists to sync: "{List1: {List2[Index]} }"
				if (currentList != null && 0 <= CollectionIndex && CollectionIndex < currentList.Count)
				{
					selectorInfo.Result = currentList[CollectionIndex];
					return true;
				}
			}

			return false;
		}
예제 #25
0
            // Note: Operators are processed by ListFormatter
            public static bool TryEvaluateSelector(ISelectorInfo selectorInfo)
            {
                if (selectorInfo.CurrentValue is null)
                {
                    return(false);
                }

                var jsonElement = (JsonElement)selectorInfo.CurrentValue;

                var         je = jsonElement.Clone();
                JsonElement targetElement;

                if (selectorInfo.FormatDetails.Settings.CaseSensitivity == SmartFormat.Core.Settings.CaseSensitivityType.CaseInsensitive)
                {
                    targetElement = je.EnumerateObject().FirstOrDefault(jp => jp.Name.Equals(selectorInfo.SelectorText,
                                                                                             selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison())).Value;
                }
                else
                {
                    targetElement = je.GetProperty(selectorInfo.SelectorText !);
                }

                selectorInfo.Result = targetElement.ValueKind switch
                {
                    JsonValueKind.Undefined => throw new FormatException($"'{selectorInfo.SelectorText}'"),
                          JsonValueKind.Null => null,
                          JsonValueKind.Number => targetElement.GetDouble(),
                          JsonValueKind.False => false,
                          JsonValueKind.True => true,
                          JsonValueKind.String => targetElement.GetString(),
                          JsonValueKind.Object => targetElement,
                          JsonValueKind.Array => targetElement.EnumerateArray().ToArray(),
                          _ => throw new ArgumentOutOfRangeException()
                };

                return(true);
            }
        }
        public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            var current  = selectorInfo.CurrentValue;
            var selector = selectorInfo.SelectorText;

            // See if current is a IDictionary and contains the selector:
            var rawDict = current as IDictionary;

            if (rawDict != null)
            {
                foreach (DictionaryEntry entry in rawDict)
                {
                    var key = entry.Key as string ?? entry.Key.ToString();

                    if (key.Equals(selector, selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison()))
                    {
                        selectorInfo.Result = entry.Value;
                        return(true);
                    }
                }
            }

            // this check is for dynamics and generic dictionaries
            var dict = current as IDictionary <string, object>;

            if (dict != null)
            {
                var val = dict.FirstOrDefault(x =>
                                              x.Key.Equals(selector, selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison())).Value;
                if (val != null)
                {
                    selectorInfo.Result = val;
                    return(true);
                }
            }

            return(false);
        }
예제 #27
0
		public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
		{
			var current = selectorInfo.CurrentValue;
			var selector = selectorInfo.SelectorText;

			// See if current is a IDictionary and contains the selector:
			var rawDict = current as IDictionary;
			if (rawDict != null)
			{
				foreach (DictionaryEntry entry in rawDict)
				{
					var key = (entry.Key as string) ?? entry.Key.ToString();

					if (key.Equals(selector, selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison()))
					{
						selectorInfo.Result = entry.Value;
						return true;
					}
				}
			}

			// this check is for dynamics and generic dictionaries
			var dict = current as IDictionary<string, object>;

			if (dict != null)
			{
				var val = dict.FirstOrDefault(x => x.Key.Equals(selector, selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison())).Value;
				if (val != null)
				{
					selectorInfo.Result = val;
					return true;
				}
			}

			return false;
		}
예제 #28
0
 private bool Length(ISelectorInfo selectorInfo, string currentValue)
 {
     selectorInfo.Result = currentValue.Length;
     return(true);
 }
		public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
		{
			const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;

			var current = selectorInfo.CurrentValue;
			var selector = selectorInfo.SelectorText;

			if (current == null)
			{
				return false;
			}

			// REFLECTION:
			// Let's see if the argSelector is a Selectors/Field/ParseFormat:
			var sourceType = current.GetType();
			
			// Important:
			// GetMembers (opposite to GetMember!) returns all members, 
			// both those defined by the type represented by the current T:System.Type object 
			// AS WELL AS those inherited from its base types.
			var members = sourceType.GetMembers(bindingFlags).Where(m => string.Equals(m.Name, selector, selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison()));
			foreach (var member in members)
			{
				switch (member.MemberType)
				{
					case MemberTypes.Field:
						//  Selector is a Field; retrieve the value:
						var field = (FieldInfo) member;
						selectorInfo.Result = field.GetValue(current);
						return true;
					case MemberTypes.Property:
					case MemberTypes.Method:
						MethodInfo method;
						if (member.MemberType == MemberTypes.Property)
						{
							//  Selector is a Property
							var prop = (PropertyInfo) member;
							//  Make sure the property is not WriteOnly:
							if (prop.CanRead)
							{
								method = prop.GetGetMethod();
							}
							else
							{
								continue;
							}
						}
						else
						{
							//  Selector is a method
							method = (MethodInfo) member;
						}

						//  Check that this method is valid -- it needs to return a value and has to be parameterless:
						//  We are only looking for a parameterless Function/Property:
						if (method.GetParameters().Length > 0)
						{
							continue;
						}

						//  Make sure that this method is not void!  It has to be a Function!
						if (method.ReturnType == typeof(void))
						{
							continue;
						}

						//  Retrieve the Selectors/ParseFormat value:
						selectorInfo.Result = method.Invoke(current, new object[0]);
						return true;
				}
			}
			
			return false;
		}
예제 #30
0
		public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
		{
			var current = selectorInfo.CurrentValue;
			var selector = selectorInfo.SelectorText;

			if (current == null)
			{
				return false;
			}

			// REFLECTION:
			// Let's see if the argSelector is a Selectors/Field/ParseFormat:
			var sourceType = current.GetType();

			var bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
			bindingFlags |= selectorInfo.FormatDetails.Settings.GetCaseSensitivityBindingFlag();

			var members = sourceType.GetMember(selector, bindingFlags);
			foreach (var member in members)
			{
				switch (member.MemberType)
				{
					case MemberTypes.Field:
						//  Selector is a Field; retrieve the value:
						var field = (FieldInfo) member;
						selectorInfo.Result = field.GetValue(current);
						return true;
					case MemberTypes.Property:
					case MemberTypes.Method:
						MethodInfo method;
						if (member.MemberType == MemberTypes.Property)
						{
							//  Selector is a Property
							var prop = (PropertyInfo) member;
							//  Make sure the property is not WriteOnly:
							if (prop.CanRead)
							{
								method = prop.GetGetMethod();
							}
							else
							{
								continue;
							}
						}
						else
						{
							//  Selector is a method
							method = (MethodInfo) member;
						}

						//  Check that this method is valid -- it needs to return a value and has to be parameterless:
						//  We are only looking for a parameterless Function/Property:
						if (method.GetParameters().Length > 0)
						{
							continue;
						}

						//  Make sure that this method is not void!  It has to be a Function!
						if (method.ReturnType == typeof(void))
						{
							continue;
						}

						//  Retrieve the Selectors/ParseFormat value:
						selectorInfo.Result = method.Invoke(current, new object[0]);
						return true;

				}
			}

			return false;
		}
예제 #31
0
 private bool ToLowerInvariant(ISelectorInfo selectorInfo, string currentValue)
 {
     selectorInfo.Result = currentValue.ToLowerInvariant();
     return(true);
 }
예제 #32
0
 private bool ToLower(ISelectorInfo selectorInfo, string currentValue)
 {
     selectorInfo.Result = currentValue.ToLower(_cultureInfo);
     return(true);
 }
예제 #33
0
 private bool TrimEnd(ISelectorInfo selectorInfo, string currentValue)
 {
     selectorInfo.Result = currentValue.TrimEnd();
     return(true);
 }
예제 #34
0
 private bool ToCharArray(ISelectorInfo selectorInfo, string currentValue)
 {
     selectorInfo.Result = currentValue.ToCharArray();
     return(true);
 }
예제 #35
0
        public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;

            var current  = selectorInfo.CurrentValue;
            var selector = selectorInfo.SelectorText;

            if (current == null)
            {
                return(false);
            }

            // REFLECTION:
            // Let's see if the argSelector is a Selectors/Field/ParseFormat:
            var sourceType = current.GetType();

            // Important:
            // GetMembers (opposite to GetMember!) returns all members,
            // both those defined by the type represented by the current T:System.Type object
            // AS WELL AS those inherited from its base types.
            var members = sourceType.GetMembers(bindingFlags).Where(m =>
                                                                    string.Equals(m.Name, selector, selectorInfo.FormatDetails.Settings.GetCaseSensitivityComparison()));

            foreach (var member in members)
            {
                switch (member.MemberType)
                {
                case MemberTypes.Field:
                    //  Selector is a Field; retrieve the value:
                    var field = (FieldInfo)member;
                    selectorInfo.Result = field.GetValue(current);
                    return(true);

                case MemberTypes.Property:
                case MemberTypes.Method:
                    MethodInfo method;
                    if (member.MemberType == MemberTypes.Property)
                    {
                        //  Selector is a Property
                        var prop = (PropertyInfo)member;
                        //  Make sure the property is not WriteOnly:
                        if (prop.CanRead)
                        {
                            method = prop.GetGetMethod();
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        //  Selector is a method
                        method = (MethodInfo)member;
                    }

                    //  Check that this method is valid -- it needs to return a value and has to be parameter-less:
                    //  We are only looking for a parameter-less Function/Property:
                    if (method.GetParameters().Length > 0)
                    {
                        continue;
                    }

                    //  Make sure that this method is not void!  It has to be a Function!
                    if (method.ReturnType == typeof(void))
                    {
                        continue;
                    }

                    //  Retrieve the Selectors/ParseFormat value:
                    selectorInfo.Result = method.Invoke(current, new object[0]);
                    return(true);
                }
            }

            return(false);
        }
예제 #36
0
 private bool ToBase64(ISelectorInfo selectorInfo, string currentValue)
 {
     selectorInfo.Result = Convert.ToBase64String(Encoding.UTF8.GetBytes(currentValue));
     return(true);
 }
예제 #37
0
 private bool FromBase64(ISelectorInfo selectorInfo, string currentValue)
 {
     selectorInfo.Result = Encoding.UTF8.GetString(Convert.FromBase64String(currentValue));
     return(true);
 }