Return type based on string

Source: Internet
Author: User

In the C # programming process, the Type. GetType (string) method is used to obtain the corresponding Type of a string. However, the search for this string is not in all programming sets and referenced assembly. Therefore, you need to write one by yourself.

/// <Summary> /// type search for the running and referenced assembly /// </summary> /// <param name = "type"> </param>/ // <returns> </returns> private static Type GetTypeFromAssembly (string type) {var x = Type. getType (type, false, true); if (x! = Null) return x; var executingAssembly = Assembly. GetExecutingAssembly (); x = executingAssembly. GetType (type); if (x! = Null) return x; var assNames = executingAssembly. getReferencedAssemblies (); foreach (var name in assNames) {if (name. fullName. startsWith ("System", StringComparison. ordinalIgnoreCase) | name. fullName. startsWith ("mscorlib", StringComparison. ordinalIgnoreCase) continue; var assembly = Assembly. load (name); x = assembly. getType (type); if (x! = Null) return x;} return null; throw new TypeLoadException (string. Format ("No {0} type is found in all running programs. ", Type ));}

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