--1. Part members of a class
--2. How to remember the relationship between members
[Mytableattribute ("T_userinfo")] Public classUserinfo:person, UserService {Private int_age2; Private int_age; [DisplayName ("Age")] Public intAge {Get { return_age; } Set{_age=value; }} [DisplayName ("name")] Public stringName {Get;Set; } Public voidShowuserinfo () {Console.WriteLine (string. Format ("Name:{0},age:{1}", Name, _age)); } protected voidShowName () {Console.WriteLine ("ShowName:"+Name); } }
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple =true, inherited =true)] Public classMytableattribute:attribute {Private string_tablename; PublicMytableattribute (stringname) {_tablename=name; } Public stringName {Get{return_tablename;} } }
--3. Assemblies
// Loading Assemblies (brain) // gets the specified assembly name under the current run directory Assembly-Assembly.Load ("reflectiondemo"); // assembly.loadfile (), Assembly.LoadFrom () loads the assembly of the specified file
--4. Type
//load all meridians of the central nervous//get all types under this Assemblytype[] Types =The . GetTypes (); //gets the information for a class based on the full name of the Class (Life gate space. Class name)Type type1 =. GetType ("Reflectiondemo.person"); //Ignore case or cannot find the type throws an exception//Type type2 =. GetType ("Reflectiondemo.person2", Throwonerror:true, Ignorecase:true); //get public Types in an assemblytype[] Publictypes =The . Getexportedtypes (); //gets the type of the classType Classusertype =typeof(UserInfo); //get the type of an instanceUserInfo UI =NewUserInfo (); Type Instancetype=UI. GetType (); //gets the name of the typeConsole.WriteLine (string. Format ("Typefullname:{0},typename:{1}", Instancetype.fullname, Instancetype.name)); //whether to inherit from a classConsole.WriteLine ("is inherited from a class-----"+typeof(UserInfo). IsSubclassOf (typeof(person))); //whether an interface is implemented (whether the implementation type of the interface is the specified type)Console.WriteLine ("whether an interface has been implemented-----"+typeof(UserService). IsAssignableFrom (typeof(UserInfo))); //is the type of publicConsole.WriteLine ("is the type of public-----"+ classusertype.ispublic);
--5. Fields, properties, methods, attributes
//get field BindingFlags bit mark get field different way//T.getfield (); T.getfields ()FieldInfo fiage = T.getfield ("_age", BindingFlags.Public|BindingFlags.NonPublic|bindingflags.static|bindingflags.instance|bindingflags.declaredonly); //Get properties like fieldt.getproperties (); PropertyInfo Pi= T.getproperty (""); //Pi. CanRead;//can read//Pi. CanWrite;//can write//Get MethodMethodinfo[] Methods =T.getmethods (); MethodInfo Method= T.getmethod ("Showuserinfo"); //Get AttributesMytableattribute tabattr = T.getcustomattribute (typeof(Mytableattribute)) asMytableattribute; if(Tabattr! =NULL) {Console.WriteLine ("SELECT * from"+tabattr.name); }
Next, reflected data manipulation
. The relationship between the human meridian and the reflection-related classes in net combat