. Net obtains the attributes, methods, and members of a class, and obtains the attribute values through the attribute name. net attribute values

Source: Internet
Author: User

. Net obtains the attributes, methods, and members of a class, and obtains the attribute values through the attribute name. net attribute values
// Custom class model m = new model ();
// Obtain the Type instance of the class // Type t = typeof (model );
// Obtain m's Type instance Type t = m. GetType ();
String className = t. Name + "\ r \ n ";
// Obtain all methods System. Reflection. MethodInfo [] methods = t. GetMethods ();
// Obtain all the member System. Reflection. MemberInfo [] members = t. GetMembers ();
// Obtain all attributes System. Reflection. PropertyInfo [] properties = t. GetProperties ();
// Obtain the class property name and the attribute value foreach (System. reflection. propertyInfo s int. getProperties () {className + = s. name + ":" + m. getType (). getProperty (s. name ). getValue (m, null ). toString () + "\ r \ n ";}

 


C ++ traverses all attribute names of a class and obtains the values of all attributes of the class's instances.

The reflection mechanism of. NET is very distinctive. It is supported by VB, C #, and VC languages.
Through the reflection mechanism, you can easily enumerate various information of a class. In addition to attributes, you can also obtain various information such as constructors, methods, and fields. The types can be public or non-public, instantiated and static attributes and methods.
Here is a piece of VC. NET instance code:
Using namespace System;
Using namespace System: Reflection;

// Define the class Student for testing
Public ref class Student
{
Private:
String ^ name;
Int32 number;
String ^ address;

Public:
Student (String ^ name, String ^ address, Int32 number)
{
This-> name = name;
This-> address = address;
This-> number = number;
}

Property String ^ Name
{
String ^ get ()
{
Return name;
}
Void set (String ^ value)
{
Name = value;
}
}

Property Int32 Number
{
Int32 get ()
{
Return number;
}
Void set (Int32 value)
{
Number = value;
}
}

Protected:
Property String ^ Address
{
String ^ get ()
{
Return address;
}
Void set (String ^ value)
{
Address = value;
}
}
};

Void DisplayPropertyInfo (Student ^ student, array <PropertyInfo ^> ^ properties );
Int main (array <System: String ^> ^ args)
{
Student ^ student = gcnew Student ("Alice", "No.193, St. Robinson, NY, USA", 1 );
Student-> Name = "Alice. Caley ";
Student-> Number = 2;
Console: WriteLine (student-> Name );
Console: WriteLine (student-> Number );
Console: WriteLine ();

// Reflection
Type ^ type = student-> GetType ();

// Public instance attributes
Array <PropertyInfo ^> ^ publicProperties = type-> GetProperties (static_cast <BindingFlags> (BindingFlags: Public | BindingFlags: Instance ));
Console: WriteLine ("[Display public property info]");
DisplayPropertyInfo (student, publicProperties );
Cons ...... remaining full text>

For objects, properties, and methods in net

Nodes is an attribute of the treeview object, but this attribute is a set that contains many elements. Each element is a treenode type object.

This statement adds node1, A treenode object, to its nodes set.

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.