C # Reflection (i) "Go"

Source: Internet
Author: User

Not too familiar with the reflection of yesterday, thought the reflection is very mysterious, looking for the answer on the Internet. Today I found a piece of code to knock, enlightened! In fact, reflection is so simple!
Reflection is a mechanism through which we can know the type information of an unknown type. For example, there is an object A, this object is not defined by us, perhaps is captured through the network, perhaps the use of generic definition, but we want to know the type information of this object, Want to know what methods or attributes this object has. Even we want to call the method of this object further. The point is that now we only know it is an object, do not know its type, naturally do not know what its methods and other information. What should we do then? The reflection mechanism is to solve such a problem. We can know the type information of an unknown type object through the reflection mechanism.
For example, we have a DLL file that we want to invoke inside the class. Now assume that the definition of the class of this DLL file, the number is not fixed, is often changed. Maybe one day you want to add a class definition to this DLL. Maybe you think it's okay, now the point is we're going to call this DLL in another assembly, and that's what our program has to be able to adapt to this DLL change, which means that even if you change the definition of the DLL file, we don't need to change our assembly. At this point we will use an unknown DLL. What are we going to do? Again, the reflection mechanism helps us, and we can do it through reflection.
To be blunt, reflection is the type of information that we know about unknown types. There's no mystery to tell!

Today I'll start with an example of getting assembly information.
Let's give an example. The idea of an example is this: we have a DLL. There are many classes about motion in the DLL. Each class records a piece of information about a sport. We need to know about this DLL in another program: (If you don't understand what I mean, please be patient and follow my steps to change the process!) )
First step: We build a file Sport.cs. The contents are as follows:

  using  System;  Publicabstractclass  Sport {     protected string  name;       Public Abstract string getduration ();       Public Abstract string  GetName (); }


Let's compile it with the command "Csc/t:library Sport.cs".
In the second step, we will build a file called SomeSports.cs, which reads as follows:

usingSystem; Public classfootball:sport{ PublicFootball () {name="Football"; }    Public Override stringgetduration () {return "Four minute quarters"; }        Public Override stringGetName () {returnname; }} Public classhockey:sport{ PublicHockey () {name="Hockey"; }    Public Override stringgetduration () {return "Three minute periods"; }    Public Override stringGetName () {returnname; }} Public classsoccer:sport{ PublicSoccer () {name="Soccer"; }    Public Override stringgetduration () {return "minute halves"; }    Public Override stringGetName () {returnname; }}

Below we compile the file with the command "Csc/t:library/r:sport.dll SomeSports.cs".

Now we have our motion information DLL file. Now we want to know what classes are in the program. Please go to the last step:
Step Three: We create the file Assemblydemo. CS ". The contents are as follows:

usingSystem;usingSystem.Reflection; Public classassemblydemo{ Public Static voidMain (string[] args) {         inti,j; //==========================          //First the command line arguments is evaluated.if there isn ' t//At least ONE,A usage message is printed//=================================          if(args. GetLength (0) <1) {Console.WriteLine ("usage is assemblydemo<library_name>"); }          Else          {          //========================          //An Assembly object was obtained from the command line argument//========================Assembly Assembly=assembly.loadfrom (args[0]); Type[] Types=Assembly.          GetTypes (); Console.WriteLine (assembly. GetName (). Name+"contains the following types");  for(i=0; I<types. GetLength (0);++i) {Console.WriteLine ("\ r ("+i+")  "+Types[i].           Name); } I=types. Length-1; Console.Write ("Make selection (0-"+i+");"); J=Convert.ToInt32 (Console.ReadLine ());               Console.WriteLine (); if(Types[j]. IsSubclassOf (typeof(Sport))) {ConstructorInfo ci=TYPES[J]. GetConstructor (Newtype[0]); Sport Sport= (Sport) ci. Invoke (Newobject[0]); Console.WriteLine (Sport. GetName ()+" has"+Sport.               Getduration ()); }               Else{Console.WriteLine (types[j]. Name+"is not a sub-class of Sport"); }          }     }}}

Let's compile the file with the command "Csc/r:sport.dll AssemblyDemo.cs".
Below we run the program with "Assemblydemo SomeSports.dll".

Further procedures require us to enter the option, we enter 1, it shows the result: Hockeyhasthree minute periods.

Well, it's here today, and I'll show you how to use the reflection mechanism to access the object's type information.

C # Reflection (i) "Go"

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.