System.ComponentModel.TypeConverter.CanConvertFrom C# (CSharp) Method

CanConvertFrom() public method

public CanConvertFrom ( ITypeDescriptorContext context, Type sourceType ) : bool
context ITypeDescriptorContext
sourceType Type
return bool
        public virtual bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
            if (sourceType == typeof(InstanceDescriptor)) {
                return true;
            }
            return false;
        }

Same methods

TypeConverter::CanConvertFrom ( Type sourceType ) : bool

Usage Example

        private static bool InnerConvertObjectToType(object obj, Type type, bool throwOnError,
                                                     out object convertedObject)
        {
            Type originalType = obj.GetType();
            OriginalTypeConverter converter = TypeDescriptor.GetConverter(type);

            if (converter.CanConvertFrom(originalType))
            {
                try
                {
                    convertedObject = converter.ConvertFrom(null, CultureInfo.InvariantCulture, obj);
                    return(true);
                }
                catch
                {
                    if (throwOnError)
                    {
                        throw;
                    }

                    convertedObject = null;
                    return(false);
                }
            }

            if (converter.CanConvertFrom(typeof(string)) && !(obj is DateTime))
            {
                try
                {
                    string text = TypeDescriptor.GetConverter(originalType).ConvertToInvariantString(obj);

                    convertedObject = converter.ConvertFromInvariantString(text);
                    return(true);
                }
                catch
                {
                    if (throwOnError)
                    {
                        throw;
                    }

                    convertedObject = null;
                    return(false);
                }
            }

            if (type.IsInstanceOfType(obj))
            {
                convertedObject = obj;
                return(true);
            }

            if (throwOnError)
            {
                throw new InvalidOperationException();
            }

            convertedObject = null;
            return(false);
        }
All Usage Examples Of System.ComponentModel.TypeConverter::CanConvertFrom