C # Reflection (Reflection) implements the idea of a Property get or set value for a class _ Practical tips

Source: Internet
Author: User
Tags reflection
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 ());
}
}
}
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.