Beispiel #1
0
        protected internal virtual TypeConverter GetTypeConverter()
        {
            var tca = ClrType.GetCustomAttributes(typeof(TypeConverterAttribute), false).Cast <TypeConverterAttribute>().FirstOrDefault();

            if (tca != null)
            {
                return(Activator.CreateInstance(Type.GetType(tca.ConverterTypeName)) as TypeConverter);
            }
            if (ClrType.IsEnum
#if NETFX_CORE
                    ()
#endif
                )
            {
                return(new DelegateTypeConverter <object>((context, culture, val) => Enum.Parse(ClrType, val, false)));
            }
            if (ClrType.IsGenericType
#if NETFX_CORE
                    ()
#endif
                && ClrType.GetGenericTypeDefinition().Equals(typeof(Nullable <>)))
            {
                var nullableKind      = Nullable.GetUnderlyingType(ClrType);
                var nullableType      = ClrType;
                var baseTypeConverter = XamlType.GetXamlType(nullableKind).TypeConverter;
                return(new DelegateTypeConverter <object>((context, culture, val) =>
                {
                    var result = baseTypeConverter.ConvertFrom(context, culture, val);
                    return Activator.CreateInstance(nullableType, result);
                }));
            }
            return(null);
        }
Beispiel #2
0
            internal override IEnumerable <Type> GetChildTypes()
            {
                // Collect information...
                Debug.Assert(
                    ClrType.IsGenericType() &&
                    typeof(IGrouping <,>).Equals(ClrType.GetGenericTypeDefinition()));
                var keyType          = ClrType.GetGenericArguments()[0];
                var groupElementType = ClrType.GetGenericArguments()[1];

                // key
                yield return(keyType);

                // group
                yield return(typeof(IEnumerable <>).MakeGenericType(groupElementType));
            }
Beispiel #3
0
            internal override Expression Emit(List <TranslatorResult> propertyTranslatorResults)
            {
                // Create expression of the form:
                // new Grouping<K, T>(children[0], children[1])

                // Collect information...
                Debug.Assert(
                    ClrType.IsGenericType() &&
                    typeof(IGrouping <,>).Equals(ClrType.GetGenericTypeDefinition()));
                Debug.Assert(propertyTranslatorResults.Count == 2);
                var keyType          = ClrType.GetGenericArguments()[0];
                var groupElementType = ClrType.GetGenericArguments()[1];
                var groupType        = typeof(Grouping <,>).MakeGenericType(keyType, groupElementType);
                var constructor      = groupType.GetConstructors().Single();

                // new Grouping<K, T>(children[0], children[1])
                Expression newGrouping = Expression.Convert(
                    Expression.New(constructor, GetPropertyReaders(propertyTranslatorResults)), ClrType);

                return(newGrouping);
            }