Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. [TypeConverter(typeof(MyTypeConverter))]
  2. public class MyClass
  3. {
  4. }
  5.  
  6. public class Converter : TypeConverter
  7. {
  8. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  9. {
  10. if (sourceType == typeof(int)
  11. || sourceType == typeof(string)
  12. || sourceType == typeof(bool))
  13. {
  14. return true;
  15. }
  16. return base.CanConvertFrom(context, sourceType);
  17. }
  18.  
  19. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  20. {
  21. if (destinationType == typeof(int)
  22. || destinationType == typeof(string)
  23. || destinationType == typeof(bool))
  24. {
  25. return true;
  26. }
  27. return base.CanConvertTo(context, destinationType);
  28. }
  29.  
  30. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  31. {
  32. if (destinationType == typeof(int))
  33. {
  34. return 42;
  35. }
  36. if (destinationType == typeof(string))
  37. {
  38. return "String is fine for convertion";
  39. }
  40. if (destinationType == typeof(bool))
  41. {
  42. return true;
  43. }
  44. return base.ConvertTo(context, culture, value, destinationType);
  45. }
  46.  
  47. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  48. {
  49. var sourceType = value.GetType();
  50. if (sourceType == typeof(int)
  51. || sourceType == typeof(string)
  52. || sourceType == typeof(bool))
  53. {
  54. return new Convertible() { ConvertionString = value.ToString() };
  55. }
  56. return base.ConvertFrom(context, culture, value);
  57. }
  58. }
  59.  
  60. [TypeConverter(typeof(Converter))]
  61. public class Convertible
  62. {
  63. public string ConvertionString
  64. {
  65. get;
  66. set;
  67. }
  68. }
  69.  
  70. var source = new Convertible();
  71. var intValue = TypeDescriptor.GetConverter(typeof(Convertible)).ConvertTo(source, typeof(int));
  72. var stringValue = TypeDescriptor.GetConverter(typeof(Convertible)).ConvertTo(source, typeof(string));
  73. var boolValue = TypeDescriptor.GetConverter(typeof(Convertible)).ConvertTo(source, typeof(bool));
  74. Console.WriteLine(intValue); //42
  75. Console.WriteLine(stringValue); //String is fine for convertion
  76. Console.WriteLine(boolValue); //True
  77.  
  78. var fromIntValue = TypeDescriptor.GetConverter(typeof(Convertible)).ConvertFrom(42) as Convertible;
  79. var fromStringValue = TypeDescriptor.GetConverter(typeof(Convertible)).ConvertFrom("String is fine for convertion") as Convertible;
  80. var fromBoolValue = TypeDescriptor.GetConverter(typeof(Convertible)).ConvertFrom(true) as Convertible;
  81.  
  82. Console.WriteLine(fromIntValue.ConvertionString); //42
  83. Console.WriteLine(fromStringValue.ConvertionString); //String is fine for convertion
  84. Console.WriteLine(fromBoolValue.ConvertionString); //True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement