Ejemplo n.º 1
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var str = value as string;

            if (str != null || value == null)
            {
                IStringConvertible obj = (IStringConvertible)Activator.CreateInstance(_Type);
                if (!obj.FromString(str, culture))
                {
                    throw new ArgumentException(string.Format("Can't convert this string to a {0}", _Type.Name), str);
                }
                return(obj);
            }

            return(null);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Employee employee = new Employee("Petr", "Tinka");

            Console.WriteLine(employee.ToUpperCase());
            //Console.WriteLine(employee.ToLowerCase());
            //IStringConvertible convertible = (IStringConvertible)employee;
            IStringConvertible convertible = employee;

            Console.WriteLine(convertible.ToLowerCase());
            ShowConverted(employee);

            MyClass mc = new MyClass();

            ShowConverted(mc);
        }
Ejemplo n.º 3
0
    public static object ConvertFromStr(Type type, string str)
    {
        if (convertersFromString.ContainsKey(type))
        {
            return convertersFromString[type].Invoke(str);
        }

        if (type.GetInterface(stringConvertibleTypeName) != null)
        {
            IStringConvertible convertible = (IStringConvertible)Activator.CreateInstance(type);
            bool success = convertible.ConvertFromString(str);
            if (success)
                return convertible;
        }

        Debug.LogWarning("there's no converter built in for type " +  type.Name + " and type is not IValueConvertible or conversion failed!");

        return TypeDescriptor.GetConverter(type).ConvertFromString(str);
    }
Ejemplo n.º 4
0
 static void ShowConverted(IStringConvertible StringConvertible)
 {
     Console.WriteLine(StringConvertible.ToUpperCase());
     Console.WriteLine(StringConvertible.ToLowerCase());
 }