c#-Reflective Knowledge points

Source: Internet
Author: User

1. Reflection is based on Sytem.type, and many of the methods inside are System.Reflection .
  
 
  1. MethodInfo[] tt= t.GetType().GetMethods(); //t是string的实例,获得string类型的所有方法,这里面的GetMethods有个BindingFlags枚举类型,写了很多 常用的需要的类
  2. foreach (var item in tt) {
  3. Console.WriteLine(item.ToString());
  4. }
  5. Console.ReadKey();


2. Assembly-based reflection
 
    
  
  1. assembly = assembly load "mscorlib,2.0.0.0.0,b77acu87d987asdf98" //the token value here is casually written
  2. type Span class= "pun" >[] types = objassembly gettypes
   
 
  1. Assembly objAss = Assembly.GetExecutingAssembly();
  2. Type t = objAss.GetType("Reflection.car", false, true);//第一个false表示找不到时不引发异常,第二个true表示忽略大小写查找
  3. Span class= "Typ" >car oo = ( car activator Span class= "pun". createinstance t //creates an instance of a class based on the established type, because it is not until what is returned, so covarince is an object
  4. Span class= "Typ" >methodinfo mi = T getmethod "move" //move is the method name in the car class
  5. var ismove = (bool)mi.Invoke(oo, null); // 调用MethodInfo的方法Invoke,核实方法move是不是在类oo中
  6. if (ismove) {
  7. Console.WriteLine("is move ");
  8. }
one of the car is a class, the 3rd behavior I modified after the code, the original code directly for object, not too rigorous, although can also use, but so write more dead some    
Where assembly usage refer to other pages


From for notes (Wiz)

C#-reflection Knowledge points

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.