예제 #1
0
        public bool Evaluate(IDataDescriptor source, out IDataDescriptor result)
        {
            result = null;
            object value = source.Value;

            if (value == null)
            {
                return(false);
            }
            ParameterInfo[] indexerParams = IndexerDataDescriptor.GetIndexerTypes(value.GetType());
            object[]        convertedIndices;
            // Search indexer on source type
            if (indexerParams != null && ReflectionHelper.ConsumeParameters(_indices,
                                                                            indexerParams, false, out convertedIndices))
            { // Index on Item property
                result = new IndexerDataDescriptor(value, convertedIndices);
                return(true);
            }
            if (ReflectionHelper.ConvertTypes(_indices, new Type[] { typeof(int) },
                                              out convertedIndices))
            { // Collection index
                if (!ReflectionHelper.GetEnumerationEntryByIndex(value, (int)convertedIndices[0], out result))
                {
                    throw new XamlBindingException("Index '{0}' cannot be applied on '{1}'",
                                                   ToString(), value);
                }
                return(true);
            }
            return(false);
        }
예제 #2
0
        public bool Evaluate(IDataDescriptor source, out IDataDescriptor result)
        {
            result = null;
            Type       type;
            object     obj;
            MemberInfo mi;

            if (!ExtractWorkingData(source, _memberName + "Property", out type, out obj, out mi))
            {
                if (!ExtractWorkingData(source, _memberName, out type, out obj, out mi))
                {
                    return(false);
                }
            }
            if (mi is FieldInfo)
            { // Field access
                result = new FieldDataDescriptor(obj, (FieldInfo)mi);
                return(true);
            }
            if (mi is PropertyInfo)
            { // Property access
                PropertyInfo pi = (PropertyInfo)mi;
                // Handle indexed property
                object[] convertedIndices = null;
                // Check property indexer
                bool indicesOnProperty = _indices != null && _indices.Length > 0 &&
                                         ReflectionHelper.ConsumeParameters(_indices, pi.GetIndexParameters(),
                                                                            false, out convertedIndices);
                if (!indicesOnProperty)
                {
                    convertedIndices = null;
                }
                if (pi.PropertyType == typeof(AbstractProperty))
                { // Property value -> request value and return DependencyPropertyDataDescriptor
                    object val = pi.GetValue(obj, convertedIndices);
                    if (val == null)
                    {
                        return(false);
                    }
                    result = new DependencyPropertyDataDescriptor(obj, _memberName, (AbstractProperty)val);
                }
                else
                { // Simple property
                    result = new SimplePropertyDataDescriptor(obj, pi);
                    if (convertedIndices != null && convertedIndices.Length > 0)
                    {
                        ((SimplePropertyDataDescriptor)result).Indices = convertedIndices;
                    }
                }
                if (_indices != null && _indices.Length > 0 && !indicesOnProperty)
                {
                    // Item or collection index -> handle index expression per IndexerPathSegment
                    return(new IndexerPathSegment(_indices).Evaluate(result, out result));
                }
                return(true);
            }
            if (mi is MethodInfo)
            {
                // Method invocation is not supported in evaluation
                return(false);
            }
            // Unsupported member type
            return(false);
        }