Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="propertyType">The Type of the property on the view.</param>
        /// <param name="sourceType">The Type of value in the DataContext, The GenericType parameter: T is this type.</param>
        /// <returns></returns>
        private StringFromTDelegate GetTheStringFromTDelegateInt(Type sourceType, Type propertyType)
        {
            // IsConvert is performed when going from native (or T) to string.

            MethodInfo          methInfoGetProp = GMT_TYPE.GetMethod("GetStringFromT", BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(sourceType);
            StringFromTDelegate result          = (StringFromTDelegate)Delegate.CreateDelegate(typeof(StringFromTDelegate), methInfoGetProp);

            return(result);
        }
Пример #2
0
        // Value is native object, we need to return a targetType (hopefully a string at this point.)
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType == typeof(string))
            {
                if (value == null)
                {
                    return(string.Empty);
                }
            }

            //System.Diagnostics.Debug.Assert(value == null || targetType.IsAssignableFrom(typeof(string)), $"PropFactory expected target type to be string, but was type: {targetType}.");

            // The parameter, if only specifying one type, is specifying the type
            // of the native (i.e., source) object.
            TwoTypes tt = FromMkUpExtParam(parameter, typeof(string));

            if (tt.IsEmpty)
            {
                throw new InvalidOperationException("Type information was not available.");
            }
            else if (targetType == tt.SourceType /* || targetType == typeof(object)*/)
            {
                return(value);
            }
            if (tt.DestType == typeof(object))
            {
                return(value);
            }

            Type vType = value.GetType();

            if (vType == tt.DestType || tt.DestType.IsAssignableFrom(vType))
            {
                System.Diagnostics.Debug.WriteLine($"The Destination Type specified in the binding parameter is assignable from the run-time type of the value being converted: {vType}.");
                System.Diagnostics.Debug.Assert(tt.DestType.IsAssignableFrom(value.GetType()), $"DestinationType: { tt.DestType } is not assignable from the run-time type of the value to be converted: {value.GetType()}.");
                return(value);
            }
            else if (tt.DestType != typeof(string))
            {
                throw new NotImplementedException("Converting to a type other than a string is not supported.");
            }
            else
            {
                StringFromTDelegate del = _converter.GetTheStringFromTDelegate(tt.SourceType, tt.DestType);
                return(del(value));
            }
        }