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 ));
}
}
}