Exemplo n.º 1
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string stringValue)
            {
                TypeConverterHelper currentHelper = helper;
                if (currentHelper == null)
                {
                    IDestinationTypeProvider targetService = context.GetService(typeof(IDestinationTypeProvider)) as IDestinationTypeProvider;
                    Type targetType = targetService.GetDestinationType();

                    if (!this.helpers.Value.TryGetValue(targetType, out currentHelper))
                    {
                        currentHelper = GetTypeConverterHelper(targetType, this.baseType, this.helperType);
                        if (!this.helpers.Value.TryAdd(targetType, currentHelper))
                        {
                            if (!this.helpers.Value.TryGetValue(targetType, out currentHelper))
                            {
                                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.TypeConverterHelperCacheAddFailed(targetType)));
                            }
                        }
                    }
                }
                object result = currentHelper.UntypedConvertFromString(stringValue, context);
                return(result);
            }

            return(base.ConvertFrom(context, culture, value));
        }