예제 #1
0
        // Token: 0x060007AB RID: 1963 RVA: 0x00017D74 File Offset: 0x00015F74
        internal static bool IsStaticProperty(object accessor)
        {
            DependencyProperty    dependencyProperty;
            PropertyInfo          propertyInfo;
            PropertyDescriptor    propertyDescriptor;
            DynamicObjectAccessor dynamicObjectAccessor;

            PropertyPath.DowncastAccessor(accessor, out dependencyProperty, out propertyInfo, out propertyDescriptor, out dynamicObjectAccessor);
            if (propertyInfo != null)
            {
                MethodInfo getMethod = propertyInfo.GetGetMethod();
                return(getMethod != null && getMethod.IsStatic);
            }
            return(false);
        }
        /// <summary>
        /// ConvertTo - Attempt to convert a PropertyPath to the given type
        /// </summary>
        /// <returns>
        /// The object which was constructed.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// An ArgumentNullException is thrown if the example object is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// An ArgumentException is thrown if the example object is not null and is not a Brush,
        /// or if the destinationType isn't one of the valid destination types.
        /// </exception>
        /// <param name="typeDescriptorContext"> The ITypeDescriptorContext for this call.
        ///  If this is null, then no namespace prefixes will be included.</param>
        /// <param name="cultureInfo"> The CultureInfo which is respected when converting. </param>
        /// <param name="value"> The PropertyPath to convert. </param>
        /// <param name="destinationType">The type to which to convert the PropertyPath instance. </param>
        public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext,
                                         CultureInfo cultureInfo,
                                         object value,
                                         Type destinationType)
        {
            if (null == value)
            {
                throw new ArgumentNullException("value");
            }

            if (null == destinationType)
            {
                throw new ArgumentNullException("destinationType");
            }

            if (destinationType != typeof(String))
            {
                throw new ArgumentException(SR.Get(SRID.CannotConvertType, typeof(PropertyPath), destinationType.FullName));
            }

            PropertyPath path = value as PropertyPath;

            if (path == null)
            {
                throw new ArgumentException(SR.Get(SRID.UnexpectedParameterType, value.GetType(), typeof(PropertyPath)), "value");
            }

            if (path.PathParameters.Count == 0)
            {
                // if the path didn't use paramaters, just write it out as it is
                return(path.Path);
            }
            else
            {
                // if the path used parameters, convert them to (NamespacePrefix:OwnerType.DependencyPropertyName) syntax
                string originalPath                      = path.Path;
                Collection <object> parameters           = path.PathParameters;
                XamlDesignerSerializationManager manager = typeDescriptorContext == null ?
                                                           null :
                                                           typeDescriptorContext.GetService(typeof(XamlDesignerSerializationManager)) as XamlDesignerSerializationManager;
                ValueSerializer         typeSerializer    = null;
                IValueSerializerContext serializerContext = null;
                if (manager == null)
                {
                    serializerContext = typeDescriptorContext as IValueSerializerContext;
                    if (serializerContext != null)
                    {
                        typeSerializer = ValueSerializer.GetSerializerFor(typeof(Type), serializerContext);
                    }
                }

                StringBuilder builder = new StringBuilder();

                int start = 0;
                for (int i = 0; i < originalPath.Length; ++i)
                {
                    // look for (n)
                    if (originalPath[i] == '(')
                    {
                        int j;
                        for (j = i + 1; j < originalPath.Length; ++j)
                        {
                            if (originalPath[j] == ')')
                            {
                                break;
                            }
                        }

                        int index;
                        if (Int32.TryParse(originalPath.AsSpan(i + 1, j - i - 1),
                                           NumberStyles.Integer,
                                           TypeConverterHelper.InvariantEnglishUS.NumberFormat,
                                           out index))
                        {
                            // found (n). Write out the path so far, including the opening (
                            builder.Append(originalPath.AsSpan(start, i - start + 1));

                            object pathPart = parameters[index];

                            // get the owner type and name of the accessor
                            DependencyProperty    dp;
                            PropertyInfo          pi;
                            PropertyDescriptor    pd;
                            DynamicObjectAccessor doa;
                            PropertyPath.DowncastAccessor(pathPart, out dp, out pi, out pd, out doa);

                            Type   type;       // the accessor's ownerType, or type of indexer parameter
                            string name;       // the accessor's propertyName, or string value of indexer parameter

                            if (dp != null)
                            {
                                type = dp.OwnerType;
                                name = dp.Name;
                            }
                            else if (pi != null)
                            {
                                type = pi.DeclaringType;
                                name = pi.Name;
                            }
                            else if (pd != null)
                            {
                                type = pd.ComponentType;
                                name = pd.Name;
                            }
                            else if (doa != null)
                            {
                                type = doa.OwnerType;
                                name = doa.PropertyName;
                            }
                            else
                            {
                                // pathPart is an Indexer Parameter
                                type = pathPart.GetType();
                                name = null;
                            }

                            // write out the type of the accessor or index parameter
                            if (typeSerializer != null)
                            {
                                builder.Append(typeSerializer.ConvertToString(type, serializerContext));
                            }
                            else
                            {
                                // Need the prefix here
                                string prefix = null;
                                if (prefix != null && prefix != string.Empty)
                                {
                                    builder.Append(prefix);
                                    builder.Append(':');
                                }
                                builder.Append(type.Name);
                            }

                            if (name != null)
                            {
                                // write out the accessor name
                                builder.Append('.');
                                builder.Append(name);
                                // write out the closing )
                                builder.Append(')');
                            }
                            else
                            {
                                // write out the ) that closes the parameter's type
                                builder.Append(')');

                                name = pathPart as string;
                                if (name == null)
                                {
                                    // convert the parameter into string
                                    TypeConverter converter = TypeDescriptor.GetConverter(type);
                                    if (converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(typeof(string)))
                                    {
                                        try
                                        {
                                            name = converter.ConvertToString(pathPart);
                                        }
                                        catch (NotSupportedException)
                                        {
                                        }
                                    }
                                }

                                // write out the parameter's value string
                                builder.Append(name);
                            }

                            // resume after the (n)
                            i     = j;
                            start = j + 1;
                        }
                    }
                }

                if (start < originalPath.Length)
                {
                    builder.Append(originalPath.AsSpan(start));
                }

                return(builder.ToString());
            }
        }
        /// <summary>Converts the specified value object to the <see cref="T:System.Windows.PropertyPath" /> type.</summary>
        /// <param name="typeDescriptorContext">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context.</param>
        /// <param name="cultureInfo">The <see cref="T:System.Globalization.CultureInfo" /> to use as the current culture.</param>
        /// <param name="value">The <see cref="T:System.Windows.PropertyPath" /> to convert.</param>
        /// <param name="destinationType">The destination type. This is expected to be the <see cref="T:System.String" /> type.</param>
        /// <returns>The converted destination <see cref="T:System.String" />.</returns>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="value" /> was provided as <see langword="null" />.</exception>
        /// <exception cref="T:System.ArgumentException">The <paramref name="value" /> was not <see langword="null" />, but was not of the expected <see cref="T:System.Windows.PropertyPath" /> type.- or -The <paramref name="destinationType" /> was not the <see cref="T:System.String" /> type.</exception>
        // Token: 0x060007CA RID: 1994 RVA: 0x00018B98 File Offset: 0x00016D98
        public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (null == destinationType)
            {
                throw new ArgumentNullException("destinationType");
            }
            if (destinationType != typeof(string))
            {
                throw new ArgumentException(SR.Get("CannotConvertType", new object[]
                {
                    typeof(PropertyPath),
                    destinationType.FullName
                }));
            }
            PropertyPath propertyPath = value as PropertyPath;

            if (propertyPath == null)
            {
                throw new ArgumentException(SR.Get("UnexpectedParameterType", new object[]
                {
                    value.GetType(),
                    typeof(PropertyPath)
                }), "value");
            }
            if (propertyPath.PathParameters.Count == 0)
            {
                return(propertyPath.Path);
            }
            string path = propertyPath.Path;
            Collection <object> pathParameters = propertyPath.PathParameters;
            XamlDesignerSerializationManager xamlDesignerSerializationManager = (typeDescriptorContext == null) ? null : (typeDescriptorContext.GetService(typeof(XamlDesignerSerializationManager)) as XamlDesignerSerializationManager);
            ValueSerializer         valueSerializer        = null;
            IValueSerializerContext valueSerializerContext = null;

            if (xamlDesignerSerializationManager == null)
            {
                valueSerializerContext = (typeDescriptorContext as IValueSerializerContext);
                if (valueSerializerContext != null)
                {
                    valueSerializer = ValueSerializer.GetSerializerFor(typeof(Type), valueSerializerContext);
                }
            }
            StringBuilder stringBuilder = new StringBuilder();
            int           num           = 0;

            for (int i = 0; i < path.Length; i++)
            {
                if (path[i] == '(')
                {
                    int num2 = i + 1;
                    while (num2 < path.Length && path[num2] != ')')
                    {
                        num2++;
                    }
                    int index;
                    if (int.TryParse(path.Substring(i + 1, num2 - i - 1), NumberStyles.Integer, TypeConverterHelper.InvariantEnglishUS.NumberFormat, out index))
                    {
                        stringBuilder.Append(path.Substring(num, i - num + 1));
                        object                obj = pathParameters[index];
                        DependencyProperty    dependencyProperty;
                        PropertyInfo          propertyInfo;
                        PropertyDescriptor    propertyDescriptor;
                        DynamicObjectAccessor dynamicObjectAccessor;
                        PropertyPath.DowncastAccessor(obj, out dependencyProperty, out propertyInfo, out propertyDescriptor, out dynamicObjectAccessor);
                        Type   type;
                        string text;
                        if (dependencyProperty != null)
                        {
                            type = dependencyProperty.OwnerType;
                            text = dependencyProperty.Name;
                        }
                        else if (propertyInfo != null)
                        {
                            type = propertyInfo.DeclaringType;
                            text = propertyInfo.Name;
                        }
                        else if (propertyDescriptor != null)
                        {
                            type = propertyDescriptor.ComponentType;
                            text = propertyDescriptor.Name;
                        }
                        else if (dynamicObjectAccessor != null)
                        {
                            type = dynamicObjectAccessor.OwnerType;
                            text = dynamicObjectAccessor.PropertyName;
                        }
                        else
                        {
                            type = obj.GetType();
                            text = null;
                        }
                        if (valueSerializer != null)
                        {
                            stringBuilder.Append(valueSerializer.ConvertToString(type, valueSerializerContext));
                        }
                        else
                        {
                            string text2 = null;
                            if (text2 != null && text2 != string.Empty)
                            {
                                stringBuilder.Append(text2);
                                stringBuilder.Append(':');
                            }
                            stringBuilder.Append(type.Name);
                        }
                        if (text != null)
                        {
                            stringBuilder.Append('.');
                            stringBuilder.Append(text);
                            stringBuilder.Append(')');
                        }
                        else
                        {
                            stringBuilder.Append(')');
                            text = (obj as string);
                            if (text == null)
                            {
                                TypeConverter converter = TypeDescriptor.GetConverter(type);
                                if (converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(typeof(string)))
                                {
                                    try
                                    {
                                        text = converter.ConvertToString(obj);
                                    }
                                    catch (NotSupportedException)
                                    {
                                    }
                                }
                            }
                            stringBuilder.Append(text);
                        }
                        i   = num2;
                        num = num2 + 1;
                    }
                }
            }
            if (num < path.Length)
            {
                stringBuilder.Append(path.Substring(num));
            }
            return(stringBuilder.ToString());
        }