예제 #1
0
        private static object GetPropertyOrFieldValue(object container, string name)
        {
            Precondition.Require(container, () => Error.ArgumentNull("container"));
            Precondition.Defined(name, () => Error.BindingExpressionCannotBeEmpty("name"));

            Type         type = container.GetType();
            PropertyInfo pi   = type.GetProperty(name,
                                                 BindingFlags.Instance | BindingFlags.Public |
                                                 BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            if (pi != null)
            {
                return(pi.CreateAccessor().GetValue(container));
            }

            FieldInfo fi = type.GetField(name,
                                         BindingFlags.Instance | BindingFlags.Public |
                                         BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            if (fi != null)
            {
                return(fi.CreateAccessor().GetValue(container));
            }

            throw Error.MissingMember(container.GetType(), name);
        }
예제 #2
0
        public PrefixWrapper(IValueSet collection, string prefix, bool ignoreCase)
        {
            Precondition.Require(collection, () => Error.ArgumentNull("collection"));
            Precondition.Defined(prefix, () => Error.ArgumentNull("prefix"));

            _collection = collection;
            _prefix     = prefix;
            _comparison = (ignoreCase)
                                ? StringComparison.OrdinalIgnoreCase
                                : StringComparison.Ordinal;
        }
예제 #3
0
        private static object GetIndexedPropertyValue(object container, string expr)
        {
            Precondition.Require(container, () => Error.ArgumentNull("container"));
            Precondition.Defined(expr, () => Error.BindingExpressionCannotBeEmpty("expr"));

            int start = expr.IndexOfAny(_indexExprStartChars);
            int end   = expr.IndexOfAny(_indexExprEndChars, start + 1);

            if (start < 0 || end < 0 || end > start)
            {
                throw Error.InvalidBindingExpressionFormat("expr");
            }

            string indexPart    = expr.Substring(start + 1, end - start - 1).Trim();
            string propertyPart = null;
            object indexValue   = null;
            bool   isIntIndex   = false;
            object instance;

            if (start != 0)
            {
                propertyPart = expr.Substring(0, start);
            }

            if (indexPart.Length != 0)
            {
                if (indexPart[0] == '\"' && indexPart[indexPart.Length - 1] == '\"' ||
                    indexPart[0] == '\'' && indexPart[indexPart.Length - 1] == '\'')
                {
                    indexValue = indexPart.Substring(1, indexPart.Length - 2);
                }

                else if (!char.IsDigit(indexPart[0]))
                {
                    indexValue = indexPart;
                }
                else
                {
                    int index;
                    if (isIntIndex = int.TryParse(indexPart, NumberStyles.Integer,
                                                  CultureInfo.InvariantCulture, out index))
                    {
                        indexValue = index;
                    }
                    else
                    {
                        indexValue = indexPart;
                    }
                }
            }

            if (indexValue == null)
            {
                throw Error.InvalidIndexerExpressionFormat("expr");
            }

            if (!String.IsNullOrEmpty(propertyPart))
            {
                instance = GetPropertyOrFieldValue(container, propertyPart);
            }
            else
            {
                instance = container;
            }

            if (instance == null)
            {
                return(indexValue);
            }

            Array array = (instance as Array);

            if (array != null && isIntIndex)
            {
                return(array.GetValue((int)indexValue));
            }

            if (instance is IList && isIntIndex)
            {
                return(((IList)instance)[(int)indexValue]);
            }

            PropertyInfo pi = instance.GetType().GetProperty("Item",
                                                             BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                             null, null, new Type[] { indexValue.GetType() }, null);

            if (pi != null)
            {
                return(pi.GetValue(instance, new object[] { indexValue }));
            }

            throw Error.IndexerNotFound(instance.GetType(), "expr");
        }