Esempio 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);
        }
Esempio 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);
        }
Esempio 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);
    }
Esempio n. 4
0
 static void ShowConverted(IStringConvertible StringConvertible)
 {
     Console.WriteLine(StringConvertible.ToUpperCase());
     Console.WriteLine(StringConvertible.ToLowerCase());
 }