Obtain the object value through reflection

Source: Internet
Author: User

Obtain the value of the specified name of the object through reflection.

Purpose: supports a wide range of data objects.

1. Name column in datarow

2. Name in the key value set

3. Name in Dictionary data

4. Name field of the class (subclass supported by the class)

 

/// <Summary>
/// Obtain the value of the specified name in the object
/// </Summary>
/// <Param name = "OBJ"> datarowview and Object Class Object </param>
/// <Param name = "name"> field or class member </param>
/// <Returns> </returns>
Public static object getvalue (Object OBJ, string name)
{
If (OBJ = NULL | string. isnullorempty (name ))
{
Return NULL;
}
// Datarow priority
If (obj is datarowview | obj is datarow)
{
Return datahelper. getvalue (OBJ, name );
}
// Key value set
If (obj is namevaluecollection)
{
Return (namevaluecollection) OBJ) [name];
}
// Class that implements the idictionary Interface
If (obj. GetType (). getinterface ("idictionary", true )! = NULL)
{
Return (idictionary) OBJ) [name];
}
// Class reflection
Int P = Name. indexof (".");
If (P =-1)
{
Int PS = Name. indexof ("(");
If (PS =-1)
{
// Attributes
Propertyinfo pinfo = obj. GetType (). getproperty (name );
If (pinfo! = NULL)
{
Return pinfo. getvalue (OBJ, null );
}
// Field
Fieldinfo finfo = obj. GetType (). getfield (name );
If (finfo! = NULL)
{
Return finfo. getvalue (OBJ );
}
// Method
Methodinfo minfo = obj. GetType (). getmethod (name );
If (minfo! = NULL)
{
Return minfo. Invoke (OBJ, null );
}
Else
{
Return NULL;
}
}
Else
{
// Method with Parameters
Int Pe = Name. indexof (")");
If (PE =-1)
{
Pe = Name. length;
}
Methodinfo minfo = obj. GetType (). getmethod (name. substring (0, PS ));
If (minfo! = NULL)
{
Return minfo. Invoke (OBJ, datahelper. getstrings (name. substring (PS + 1, Pe-PS-1). Replace ("'","")));
}
Else
{
Return NULL;
}
}
}
Else
{
// Contains subclass
String name1 = Name. substring (0, P );
Object obj1 = NULL;
Propertyinfo pinfo = obj. GetType (). getproperty (name1 );
If (pinfo! = NULL)
{
Obj1 = pinfo. getvalue (OBJ, null );
}
Else
{
Fieldinfo finfo = obj. GetType (). getfield (name1 );
If (finfo! = NULL)
{
Obj1 = finfo. getvalue (OBJ );
}
}
If (obj1 = NULL)
{
Return NULL;
}
Else
{
Return getvalue (obj1, name. substring (p + 1 ));
}
}
}

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.