C#_SRC:Just a piece of Reflection code
// this is just a Reflection sample
using System;
namespace ConsoleApplication1
{
class Test
{
public string str1 = "";
public string str2 = "";
public void Assign( Test t )
{
Class1.Assign( typeof(Test), this, t );
}
}
class Class1
{
public static void Assign( Type type, Object o1, Object o2 )
{
System.Reflection.MemberInfo[] members = type.GetMembers();
foreach( System.Reflection.MemberInfo member in members )
{
System.Reflection.FieldInfo fi = type.GetField( member.Name );
if( fi != null )
{
fi.SetValue( o1, fi.GetValue( o2 ) );
}
}
}
[STAThread]
static void Main(string[] args)
{
Test t1 = new Test();
Test t2 = new Test();
t1.str1 = "t1::str1";
t1.str2 = "t1::str2";
t2.str1 = "t2::str1";
t2.str2 = "t2::str2";
t2.Assign( t1 );
Console.WriteLine( "{0} {1}", t2.str1, t2.str2 );
}
}
}
Thursday, April 20, 2006