C # support for conversion of any two types (example: String convert to Type)

Source: Internet
Author: User

Sometimes we suddenly use some rare usage when writing a program. For example, we encountered such a problem during programming today. I have never encountered this problem before. I don't know how to solve it at the beginning. In the search process, it is found that the. net class library has provided a ready-made processing method. Therefore, we will record it here for future reference only and hope to have a reference for a friend who needs the same. Let's take a look at this problem!

  Problem description:A generic method, such as public List <T> getList <T> (), returns a List set. The element type in the List set is specified when the generic method is called. In the specific implementation of the getList method, some types of data may need to be converted to the T type, and then added to the List set.

  Example:Call the getList <int> () method. The data is of the string type. Therefore, you must convert the string type to the int type before adding the data to the List <int> set.

  Solution:

    Under the namespace System, we found a ChangeType method under the Convert class, which has three reload methods, as shown in:

    

For more information about this method, see MSDN: http://technet.microsoft.com/zh-cn/library/system.convert.changetype (en-us). aspx

With this method, let's take a look at how to solve this problem.

  1. Obtain the System. Type object of generic Type T, and use typeof (T ).
  2. Use the Convert. ChangeType method to Convert a String to an object equivalent to a T object.
  3. Convert the obtained object to T.

You can extract the above process into a public method and look at the following code.

01    public static T FromType<T,TK>(TK text) 02          { 03              try 04              { 05                  return (T) Convert.ChangeType(text, typeof (T), CultureInfo.InvariantCulture); 06              } 07              catch 08              { 09                  return default (T); 10              } 11          }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.