Пример #1
0
        /// <summary>
        /// Given the instance <paramref name="obj"/> and the <paramref name="memberName"/>,
        /// this method searches the best matching member on the instance. It first searches
        /// a property with name [PropertyName]Property, casts it to
        /// <see cref="AbstractProperty"/> and returns a <see cref="DependencyPropertyDataDescriptor"/>
        /// for it in the parameter <paramref name="dd"/>. If there is no such property, this method
        /// searches a simple property with the given name, returning a property descriptor for it.
        /// Then, the method will search for a field with the specified name, returning a
        /// <see cref="FieldDataDescriptor"/> for it.
        /// If there is no member found with the given name, this method returns false and a
        /// <c>null</c> value in <paramref name="dd"/>.
        /// </summary>
        /// <param name="obj">The object where to search the member with the
        /// specified <paramref name="memberName"/>.</param>
        /// <param name="memberName">The name of the member to be searched.</param>
        /// <param name="dd">Data descriptor which will be returned for the property or member,
        /// if it was found, else a <c>null</c> value will be returned.</param>
        /// <returns><c>true</c>, if a member with the specified name was found, else <c>false</c>.</returns>
        public static bool FindMemberDescriptor(object obj, string memberName, out IDataDescriptor dd)
        {
            if (obj == null)
            {
                throw new NullReferenceException("Property target object 'null' is not supported");
            }
            DependencyPropertyDataDescriptor dpdd;

            if (DependencyPropertyDataDescriptor.CreateDependencyPropertyDataDescriptor(
                    obj, memberName, out dpdd))
            {
                dd = dpdd;
                return(true);
            }
            SimplePropertyDataDescriptor spdd;

            if (SimplePropertyDataDescriptor.CreateSimplePropertyDataDescriptor(
                    obj, memberName, out spdd))
            {
                dd = spdd;
                return(true);
            }
            FieldDataDescriptor fdd;

            if (FieldDataDescriptor.CreateFieldDataDescriptor(obj, memberName, out fdd))
            {
                dd = fdd;
                return(true);
            }
            dd = null;
            return(false);
        }
Пример #2
0
        public IDataDescriptor Retarget(object newTarget)
        {
            if (newTarget == null)
            {
                throw new NullReferenceException("Target object 'null' is not supported");
            }
            if (!_fld.DeclaringType.IsAssignableFrom(newTarget.GetType()))
            {
                throw new InvalidOperationException(string.Format(
                                                        "Type of new target object is not compatible with this property descriptor (expected type: {0}, new target type: {1}",
                                                        _fld.DeclaringType.Name, newTarget.GetType().Name));
            }
            FieldDataDescriptor result = new FieldDataDescriptor(newTarget, _fld);

            return(result);
        }
Пример #3
0
        public static bool CreateFieldDataDescriptor(object targetObj,
                                                     string fieldName, out FieldDataDescriptor result)
        {
            result = null;
            if (targetObj == null)
            {
                throw new NullReferenceException("Target object 'null' is not supported");
            }
            FieldInfo fi;

            if (!FindField(targetObj.GetType(), fieldName, out fi))
            {
                return(false);
            }
            result = new FieldDataDescriptor(targetObj, fi);
            return(true);
        }
Пример #4
0
 /// <summary>
 /// Returns the information if the specified <paramref name="other"/> descriptor
 /// is targeted at the same field on the same object.
 /// </summary>
 /// <param name="other">Other descriptor whose target object and field should be compared.</param>
 public bool TargetEquals(FieldDataDescriptor other)
 {
     return(_obj.Equals(other._obj) && _fld.Equals(other._fld));
 }
Пример #5
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);
        }
Пример #6
0
 /// <summary>
 /// Returns the information if the specified <paramref name="other"/> descriptor
 /// is targeted at the same field on the same object.
 /// </summary>
 /// <param name="other">Other descriptor whose target object and field should be compared.</param>
 public bool TargetEquals(FieldDataDescriptor other)
 {
   return _obj.Equals(other._obj) && _fld.Equals(other._fld);
 }
Пример #7
0
 public IDataDescriptor Retarget(object newTarget)
 {
   if (newTarget == null)
     throw new NullReferenceException("Target object 'null' is not supported");
   if (!_fld.DeclaringType.IsAssignableFrom(newTarget.GetType()))
     throw new InvalidOperationException(string.Format(
         "Type of new target object is not compatible with this property descriptor (expected type: {0}, new target type: {1}",
         _fld.DeclaringType.Name, newTarget.GetType().Name));
   FieldDataDescriptor result = new FieldDataDescriptor(newTarget, _fld);
   return result;
 }
Пример #8
0
 public static bool CreateFieldDataDescriptor(object targetObj,
     string fieldName, out FieldDataDescriptor result)
 {
   result = null;
   if (targetObj == null)
     throw new NullReferenceException("Target object 'null' is not supported");
   FieldInfo fi;
   if (!FindField(targetObj.GetType(), fieldName, out fi))
     return false;
   result = new FieldDataDescriptor(targetObj, fi);
   return true;
 }