C # Reflection when the GetType method looks for the analysis of type

Source: Internet
Author: User

Reflection is a powerful mechanism within a high-level language. C # also provides us with a powerful reflection mechanism. Reflection is very simple to use, and the most common steps are:

1, defines a type object, type MyType;

2, initialize the object through a string or other stream, MyType = Type.GetType ("MyClass");

When the Type.GetType () method executes, how does the system find the correct class definition based on the string? Look at the code below

[C-sharp]View PlainCopy
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Reflection;
  4. Using System.IO;
  5. Namespace test{
  6. Public Delegate Object twoint32s (Int32 N1, Int32 n2);
  7. Public Delegate Object onestring (String s1);
  8. Class APP
  9. {
  10. public static void Main (string[] args)
  11. {
  12. //type deltype = Type.GetType ("twoint32s");//Wrong Call method
  13. Type Deltype = Type.GetType ("test.onestring"); Correct method of invocation
  14. if (Deltype = = null)
  15. {
  16. Console.WriteLine ("Invalid deltype argument:" + "twoint32s");
  17. return;
  18. }
  19. }
  20. }
  21. }

This code shows that type.gettype when parsing a type, if the parameter string does not specify namespace will cause the program not to parse the type correctly.

If we remove the namespace, we get the following code.

[C-sharp]View PlainCopy
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Reflection;
  4. Using System.IO;
  5. Public Delegate Object twoint32s (Int32 N1, Int32 n2);
  6. Public Delegate Object onestring (String s1);
  7. Class APP
  8. {
  9. public static void Main (string[] args)
  10. {
  11. Type Deltype;
  12. Deltype = Type.GetType ("System.IO.File"); Correct method
  13. //deltype = Type.GetType ("File");//Error
  14. Deltype = Type.GetType ("twoint32s"); Correct method of invocation
  15. //type deltype = Type.GetType ("test.onestring");
  16. if (Deltype = = null)
  17. {
  18. Console.WriteLine ("Invalid deltype argument:" + "twoint32s");
  19. return;
  20. }
  21. }
  22. }

This means that if a type is contained within a namspace, the type information must be obtained using the full path containing the namespace.

C # Reflection when the GetType method looks for the analysis of 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: 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.