/// <summary> /// Performs the default index-based selector, same as String.Format. /// </summary> public void EvaluateSelector(object current, Selector selector, ref bool handled, ref object result, FormatDetails formatDetails) { int selectorValue; if (int.TryParse(selector.Text, 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 (selector.SelectorIndex == 0 && selectorValue < formatDetails.OriginalArgs.Length && selector.Operator == "") { // This selector is an argument index. result = formatDetails.OriginalArgs[selectorValue]; handled = true; } // Alignment: // An alignment item should be preceeded by a comma else if (selector.Operator == ",") { // This selector is actually an Alignment modifier. result = current; // (don't change the current item) formatDetails.Placeholder.Alignment = selectorValue; // Set the alignment handled = true; } } }
public void EvaluateSelector(object current, Selector selector, ref bool handled, ref object result, FormatDetails formatDetails) { // 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.Text, Smart.Settings.GetCaseSensitivityComparison())) { result = entry.Value; handled = true; return; } } } // 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.Text, Smart.Settings.GetCaseSensitivityComparison())).Value; if (val != null) { result = val; handled = true; } } }
public void EvaluateSelector(object current, Selector selector, ref bool handled, ref object result, FormatDetails formatDetails) { // See if current is a IDictionary and contains the selector: var dict = current as IDictionary; if (dict != null && dict.Contains(selector.Text)) { result = dict[selector.Text]; handled = true; } }
public void EvaluateSelector(object current, Selector selector, ref bool handled, ref object result, FormatDetails formatDetails) { // See if current is a IDictionary and contains the selector var dict = current as IDictionary; if (dict != null && dict.Contains(selector.Text)) { result = dict[selector.Text]; handled = true; return; } // See if current is an IDictionary<string, object> (also an "expando" object) // and contains the selector. var genericDictionary = current as IDictionary<string, object>; if (genericDictionary != null && genericDictionary.ContainsKey(selector.Text)) { result = genericDictionary[selector.Text]; handled = true; } }
/// <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 void EvaluateSelector(object current, Selector selector, ref bool handled, ref object result, FormatDetails formatDetails) { // See if we're trying to access a specific index: int itemIndex; var currentList = current as IList; var isAbsolute = (selector.SelectorIndex == 0 && selector.Operator.Length == 0); if (!isAbsolute && currentList != null && int.TryParse(selector.Text, 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 result = currentList[itemIndex]; handled = true; } // We want to see if there is an "Index" property that was supplied. if (selector.Text.Equals("index", StringComparison.OrdinalIgnoreCase)) { // Looking for "{Index}" if (selector.SelectorIndex == 0) { result = CollectionIndex; handled = true; return; } // Looking for 2 lists to sync: "{List1: {List2[Index]} }" if (currentList != null && 0 <= CollectionIndex && CollectionIndex < currentList.Count) { result = currentList[CollectionIndex]; handled = true; } } }
public void EvaluateSelector(object current, Selector selector, ref bool handled, ref object result, FormatDetails formatDetails) { if (current == null) { return; } // REFLECTION: // Let's see if the argSelector is a Selectors/Field/ParseFormat: var sourceType = current.GetType(); var members = sourceType.GetMember(selector.Text); foreach (var member in members) { switch (member.MemberType) { case MemberTypes.Field: // Selector is a Field; retrieve the value: var field = (FieldInfo) member; result = field.GetValue(current); handled = true; return; 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: result = method.Invoke(current, new object[0]); handled = true; return; } } }