Using System;
Using System. Collections. Generic;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. reflection;
Namespace Weblinq
{
Public Partial Class _ Default: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
User u = New User ();
U. Age = 18 ;
U. Name = " Rhythmk " ;
Man man = New MAN ();
Setvalue (u, man );
If (Man ! = Null )
{
Response. Write (man. Name );
}
Else {
Response. Write ( " Null " );
}
}
Public Static Void Setvalue ( Object Oldobj, Object Newobj)
{
Type oldtype = Oldobj. GetType ();
Type newtype = Newobj. GetType ();
Propertyinfo [] oldobjpropertyinfos = Oldtype. getproperties ();
For ( Int I = 0 ; I < Oldobjpropertyinfos. length; I ++ )
{
If (Oldobjpropertyinfos [I]. Canread)
{
String Strpropertyname = Oldobjpropertyinfos [I]. Name;
Object Newvalue = Getpropertyvalue (oldobj, strpropertyname );
If (Newvalue ! = Null )
Setpropertyvalue (newobj, strpropertyname, newvalue );
}
}
}
Private Static Void Setpropertyvalue ( Object OBJ, String Strpropertyname, Object Val)
{
Type T = OBJ. GetType ();
Propertyinfo [] propertyinfos = T. getproperties ();
Foreach (Propertyinfo pi In Propertyinfos)
{
If (PI. Name = Strpropertyname)
{
If (PI. canwrite)
Pi. setvalue (OBJ, Val, Null );
}
}
}
Private Static Object Getpropertyvalue ( Object OBJ, String Strpropertyname)
{
Type T = OBJ. GetType ();
Propertyinfo [] propertyinfos = T. getproperties ();
Foreach (Propertyinfo pi In Propertyinfos)
{
If (PI. Name = Strpropertyname)
{
If (PI. Canread)
Return Pi. getvalue (OBJ, Null );
}
}
Return Null ;
}
}
}
It is mainly to avoid assigning values between data classes generated by projects such as LINQ to SQL and actual classes.
Instead of performing
A. property1 = B. property1;
A. property2 = B. property2;
A. property3 = B. property3;