<small id='RYHLl'></small><noframes id='RYHLl'>

  • <legend id='RYHLl'><style id='RYHLl'><dir id='RYHLl'><q id='RYHLl'></q></dir></style></legend>

      <tfoot id='RYHLl'></tfoot>

      1. <i id='RYHLl'><tr id='RYHLl'><dt id='RYHLl'><q id='RYHLl'><span id='RYHLl'><b id='RYHLl'><form id='RYHLl'><ins id='RYHLl'></ins><ul id='RYHLl'></ul><sub id='RYHLl'></sub></form><legend id='RYHLl'></legend><bdo id='RYHLl'><pre id='RYHLl'><center id='RYHLl'></center></pre></bdo></b><th id='RYHLl'></th></span></q></dt></tr></i><div id='RYHLl'><tfoot id='RYHLl'></tfoot><dl id='RYHLl'><fieldset id='RYHLl'></fieldset></dl></div>

          <bdo id='RYHLl'></bdo><ul id='RYHLl'></ul>

        如何查找和调用特定类型的 .Net TypeConverter?

        时间:2023-06-09

          <bdo id='WRsns'></bdo><ul id='WRsns'></ul>

            <tbody id='WRsns'></tbody>
          <legend id='WRsns'><style id='WRsns'><dir id='WRsns'><q id='WRsns'></q></dir></style></legend>
            1. <i id='WRsns'><tr id='WRsns'><dt id='WRsns'><q id='WRsns'><span id='WRsns'><b id='WRsns'><form id='WRsns'><ins id='WRsns'></ins><ul id='WRsns'></ul><sub id='WRsns'></sub></form><legend id='WRsns'></legend><bdo id='WRsns'><pre id='WRsns'><center id='WRsns'></center></pre></bdo></b><th id='WRsns'></th></span></q></dt></tr></i><div id='WRsns'><tfoot id='WRsns'></tfoot><dl id='WRsns'><fieldset id='WRsns'></fieldset></dl></div>

              <small id='WRsns'></small><noframes id='WRsns'>

                <tfoot id='WRsns'></tfoot>

                  本文介绍了如何查找和调用特定类型的 .Net TypeConverter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想实现一个通用的运行时类型转换函数,它使用 .Net TypeConverters 进行转换.

                  I would like to implement a general purpose runtime type conversion function that makes use .Net TypeConverters to do the conversion.

                  有谁知道如何查找和调用特定类型的 TypeConverter?

                  Does anyone know how to how to look up and invoke a TypeConverter for a particular type?

                  考虑这个 C# 示例:

                  Consider this C# example:

                  //
                  // Convert obj to the type specified by 'toType'.
                  // 
                  object ConvertTo(object obj, Type toType)
                  {
                      if (TypeIsEqualOrDerivesFrom(obj.GetType(), toType)) <-- I know how to implement this.
                      {
                          // The type of obj is the same as the type specified by 'toType' or
                          // the type of obj derives from the type specified by 'toType'.
                          return obj;
                      }
                  
                      if (TypeConverterExists(obj.GetType(), toType) <-- How do I implement this?
                      {
                          // There exists a type convertor that is capable of converting from 
                          // the type of obj to the type specified by 'toType'.
                          return InvokeTypeConverter(obj, toType); <-- How do I implement this?
                      }
                  
                      throw new TypeConversionFailedException();
                  }
                  

                  推荐答案

                      TypeConverter converter = TypeDescriptor.GetConverter(sourceType);
                      if(converter.CanConvertTo(destinationType)) {
                          return converter.ConvertTo(obj, destinationType);   
                      }
                      converter = TypeDescriptor.GetConverter(destinationType);
                      if(converter.CanConvertFrom(sourceType)) {
                          return converter.ConvertFrom(obj);
                      }
                  

                  你也可以看看 Convert.ChangeType(obj, destinationType)

                  这篇关于如何查找和调用特定类型的 .Net TypeConverter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:TypeConverter vs. Convert vs. TargetType.Parse 下一篇:即使签名匹配,也无法将一种类型的委托分配给另一种类型

                  相关文章

                    <tfoot id='8ikre'></tfoot>

                    <small id='8ikre'></small><noframes id='8ikre'>

                  1. <legend id='8ikre'><style id='8ikre'><dir id='8ikre'><q id='8ikre'></q></dir></style></legend>

                  2. <i id='8ikre'><tr id='8ikre'><dt id='8ikre'><q id='8ikre'><span id='8ikre'><b id='8ikre'><form id='8ikre'><ins id='8ikre'></ins><ul id='8ikre'></ul><sub id='8ikre'></sub></form><legend id='8ikre'></legend><bdo id='8ikre'><pre id='8ikre'><center id='8ikre'></center></pre></bdo></b><th id='8ikre'></th></span></q></dt></tr></i><div id='8ikre'><tfoot id='8ikre'></tfoot><dl id='8ikre'><fieldset id='8ikre'></fieldset></dl></div>
                      <bdo id='8ikre'></bdo><ul id='8ikre'></ul>