c# – 为什么TypeConverter不工作?

前端之家收集整理的这篇文章主要介绍了c# – 为什么TypeConverter不工作?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想了解为什么下面的代码不能像预期的那样工作TypeDescriptor根本不是从属性获取自定义转换器.我只能假设我犯了一个明显的错误,但我看不到它.

– 编辑 – 当我在自己的控制台中运行时,这段代码似乎工作,我实际上是从一个更复杂的应用程序和不同的命名空间中调用一个转换器.

– 编辑 – 或者任何关于如何调试TypeDescriptor的建议,以便我可以看到发生了什么,然后我可以自己回答这个.

– 编辑 – 这个问题几乎肯定与不同的程序集中的程序有关.

– 编辑 – 看起来这不起作用,因为动态加载程序集的一些奇怪 – 这个代码运行在像架构这样的插件.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.ComponentModel;
  7.  
  8. namespace MyTest
  9. {
  10.  
  11. public class TestTester
  12. {
  13. public static void Main(string[] args)
  14. {
  15. object v = TypeDescriptor.GetConverter(typeof(MyTest.Test)).ConvertFromInvariantString("Test");
  16. }
  17. }
  18.  
  19. public class TestConverter : TypeConverter
  20. {
  21.  
  22. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  23. {
  24. return false;
  25. }
  26.  
  27. public override bool CanConvertFrom(ITypeDescriptorContext context,System.Type sourceType)
  28. {
  29. if (sourceType == typeof(string) || base.CanConvertFrom(context,sourceType))
  30. {
  31. return true;
  32. }
  33. return base.CanConvertFrom(context,sourceType);
  34. }
  35.  
  36. public override bool CanConvertTo(ITypeDescriptorContext context,Type destinationType)
  37. {
  38. if (destinationType == typeof(Test) || base.CanConvertTo(destinationType))
  39. {
  40. return true;
  41. }
  42. return base.CanConvertTo(context,destinationType);
  43. }
  44.  
  45. public override object ConvertFrom(ITypeDescriptorContext context,System.Globalization.CultureInfo culture,object value)
  46. {
  47. if (value.GetType() == typeof(string))
  48. {
  49. Test t = new Test();
  50. t.TestMember = value as string;
  51. return t;
  52. }
  53. return base.ConvertFrom(context,culture,value);
  54. }
  55.  
  56. public override object ConvertTo(ITypeDescriptorContext context,object value,Type destinationType)
  57. {
  58. if (destinationType == typeof(string) && value.GetType() == typeof(Test))
  59. {
  60. return ((Test)value).TestMember;
  61. }
  62. return base.ConvertTo(context,value,destinationType);
  63. }
  64.  
  65. }
  66.  
  67. [TypeConverterAttribute(typeof(TestConverter))]
  68. public struct Test
  69. {
  70. public string TestMember { get; set; }
  71. }
  72. }

解决方法

我也有这个问题,解决问题的方法订阅当前应用程序域的AssemblyResolve事件并手动解析程序集.

这远远不是一个很好的解决方案,但它似乎工作.我不知道为什么框架以这种方式行事.我真的很想找到一个更少的黑客方法解决这个问题.

  1. public void DoMagic()
  2. {
  3. // NOTE: After this,you can use your typeconverter.
  4. AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
  5. }
  6.  
  7. private Assembly CurrentDomain_AssemblyResolve(object sender,ResolveEventArgs args)
  8. {
  9. AppDomain domain = (AppDomain)sender;
  10. foreach (Assembly asm in domain.GetAssemblies())
  11. {
  12. if (asm.FullName == args.Name)
  13. {
  14. return asm;
  15. }
  16. }
  17. return null;
  18. }

猜你在找的C#相关文章