Recently, a friend called Insus. Learn about Reflection (Reflection), which provides objects (type types) that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind a type to an existing object, or get a type from an existing object and call its methods or access its fields and properties. If you use attributes in your code, you can use reflection to access them.
The following example is the Insus exercise that sets and get values for a category's properties.
First write a class, then write a read-write property:
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
<summary>
Summary description for
</summary>
Namespace Insus.net
{
public class Member
{
private string _name;
public string Name
{
Get
{
return _name;
}
Set
{
_name = value;
}
}
Public member ()
{
//
Todo:add constructor Logic here
//
}
}
}
Insus.net always write the ASP.net program, the practice is also at the site.
Create a Web page that references two namespace:
Copy Code code as follows:
Using Insus.net;
Using System.Reflection;
Read-Write properties:
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using Insus.net;
Using System.Reflection;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
Instantiating classes
member Objmember = new Member ();
Set value on property
PropertyInfo pi = Objmember.gettype (). GetProperty ("Name", BindingFlags.Public | BindingFlags.Instance);
if (null!= pi && pi. CanWrite)
{
Pi. SetValue (Objmember, "insus.net", null);
}
On Property Get value
PropertyInfo PII = Objmember.gettype (). GetProperty ("Name", BindingFlags.Public | BindingFlags.Instance);
if (null!= PII && pi. CanRead)
{
Object obj_name = Pii. GetValue (objmember, NULL);
Response.Write (Obj_name.tostring ());
}
}
}