C # Converts an object to the specified type

Source: Internet
Author: User
Original address: Click to open the link

For: normal object with default parameterless constructor

  #region convert an object to the specified type////<summary>///Convert an object to the specified type////</summary>//<param Name= "obj" > objects to be converted </param>//<param name= "type" > Target type </param>///<returns> converted  Like </returns> public static object Converttoobject (object obj, type type) {if (type = = NULL)            return obj; if (obj = = null) return type. Isvaluetype?            Activator.CreateInstance (type): null;            Type Underlyingtype = Nullable.getunderlyingtype (type); if (type. IsAssignableFrom (obj.            GetType ()))//If the type of object to be converted is compatible with the target type, you do not need to convert {return obj; } else if (Underlyingtype??? type). Isenum)//If the base type of the object to be converted is an enumeration {if (Underlyingtype! = null && string. IsNullOrEmpty (obj.                ToString ()))//If the target type is a nullable enumeration, and the object to be converted is NULL, the null value {return null is returned directly;           } else {         Return Enum.parse (underlyingtype??? type, obj.                ToString ()); }} else if (typeof (IConvertible).                IsAssignableFrom (underlyingtype?? type))//If the base type of the target type implements IConvertible, the direct conversion {try                {return Convert.changetype (obj, Underlyingtype??? type, NULL); } catch {return underlyingtype = = null?                Activator.CreateInstance (type): null;                }} else {TypeConverter converter = Typedescriptor.getconverter (type); if (converter. CanConvertFrom (obj. GetType ())) {return converter.                ConvertFrom (obj); } ConstructorInfo constructor = type.                GetConstructor (type.emptytypes); if (constructor! = null) {Object o = constructor.                    Invoke (NULL); Propertyinfo[] Propertys = type.                    GetProperties (); Type Oldtype = obj.                    GetType (); foreach (PropertyInfo property in Propertys) {PropertyInfo p = Oldtype.getprope Rty (property.                        Name); if (property. CanWrite && P! = null && p.canread) {property. SetValue (O, Converttoobject (p.getvalue (obj, null), property.                        PropertyType), NULL);                }} return o;        }} return obj; } #endregion


The above is C # to convert an object to the specified type of content, more relevant content please follow topic.alibabacloud.com (www.php.cn)!

  • 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.