AssemblyQualifiedTypeNameConverter.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:AssemblyQualifiedTypeNameConverter.cs
  5. * 2.功能描述:类型配置转换器
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/16 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.ComponentModel;
  12. using System.Configuration;
  13. using System.Globalization;
  14. namespace Dongke.IBOSS.PRD.WCF.Services.ConfigSetting
  15. {
  16. /// <summary>
  17. /// 类型配置转换器
  18. /// </summary>
  19. public class AssemblyQualifiedTypeNameConverter : ConfigurationConverterBase
  20. {
  21. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  22. {
  23. string typeName = (string)value;
  24. if (string.IsNullOrEmpty(typeName))
  25. {
  26. return null;
  27. }
  28. Type result = Type.GetType(typeName, false);
  29. if (result == null)
  30. {
  31. throw new ArgumentException(string.Format("不能加载类型\"{0}\"", typeName));
  32. }
  33. return result;
  34. }
  35. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  36. {
  37. Type type = value as Type;
  38. if (null == type)
  39. {
  40. throw new ArgumentNullException("value");
  41. }
  42. return type.AssemblyQualifiedName;
  43. }
  44. }
  45. }