C # obtain the class name, private method, and dynamic execution method through reflection. Multiple values are returned.

Source: Internet
Author: User
My advertisement unit. Please click here. Thank you!

Let's look at the example.

Code content:

Entity class:

Public
Class userinfo
{
Public userinfo ()
{}

# Region member
Private int _ userid;
Private string
_ Username;
Private string _ password;
Private int
_ Roleid;
Private string _ telephone;
Private string
_ Usersex;
Private string _ address;
Private string
_ Email;
Private string _ logintime;
Private string
_ Createtime;
# Endregion


# Region attributes
Public int userid
{
Get {return
_ Userid ;}
Set {_ userid = value ;}
}

Public String Username
{
Get {return _ username;
}
Set {_ username = value ;}
}

Public String Password
{
Get {return _ password;
}
Set {_ password = value ;}
}

Public int roleid
{
Get {return _ roleid;
}
Set {_ roleid = value ;}
}

Public String telephone
{
Get {return _ telephone;
}
Set {_ telephone = value ;}
}

Public String usersex
{
Get {return _ usersex;
}
Set {_ usersex = value ;}
}

Public String address
{
Get {return _ address;
}
Set {_ address = value ;}
}

Public String email
{
Get {return _ email;
}
Set {_ email = value ;}
}

Public String logintime
{
Get {return _ logintime;
}
Set {_ logintime = value ;}
}

Public String createtime
{
Get {return _ createtime;
}
Set {_ createtime = value ;}
}

Private string returnautoid ()
{
Return
"Userid ";
}
# Endregion

}

Reflection Method class:
Using
System;
Using system. Collections. Generic;
Using system. LINQ;
Using
System. text;
Using system. reflection; namespace
Reflecti
{
Public class getmodelinfo
{
///
<Summary>
/// Get the Object Name and auto-increment ID through reflection
///
</Summary>
/// <Param
Name = "OBJ"> Object </param>
///
<Returns> Returns a string array </returns>
Public static string []
Core (Object OBJ)
{
String [] STR = new
String [2];
Type T = obj. GetType ();
Methodinfo MI =
T. getmethod ("returnautoid ",

Bindingflags. nonpublic
|
Bindingflags. instance,
Null, new
Type [] {}, null );

// Execute the returnautoid method through reflection and return the autoid Value
String str1 = mi. Invoke (OBJ,
Null). tostring ();
STR [1] = str1;
STR [0] =
T. Name. tostring ();
// Returns the OBJ name and returnautoid.

Return STR;
}
}
}

Call:
Userinfo _ userinfo =
New userinfo ();
Messageboxshow ("entity class name:" getmodelinfo. Core (_ userinfo) [1] +"
Automatic ID: "+ GetModel. Core (_ userinfo) [0]);

OK. Test it now. Through this example, you can draw many other gains.

Other knowledge points:

Use Assembly to define and load an assembly, load the modules listed in the assembly list, locate the type in this Assembly, and create instances of this type.

Use module to learn the following similar information: including the Module assembly and classes in the module. You can also obtain all global methods defined on the module or other specific non-Global methods.

Use constructorinfo to learn the following information: name, parameter, access modifier (such as public or private) and implementation details (such
Abstract or virtual. Use getconstructors or getconstructor of Type
Method to call a specific constructor.

Learn the following information using methodinfo: method name, return type, parameter, access modifier (such as public or private), and implementation details (such
Abstract or virtual. Use the getmethods or getmethod method of type to call a specific method.

Use fieldinfo to learn the following information: field name, access modifier (such as public or private), and implementation details (such
And obtain or set the field value.

Use eventinfo to learn the following similar information: event name, event handler data type, custom attribute, declaration type, and reflection type, and add or remove event handlers.

Use propertyinfo to learn the following similar information: attribute name, data type, declaration type, reflection type, read-only or writable status, and get or set the attribute value.

Use parameterinfo to learn the following similar information: Parameter Name, data type, whether the parameter is an input parameter or an output parameter, and the position of the parameter in the method signature.

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.