C# Tutorial: Get,Set properties for object.

3 04 2009

I lazy when do one work every coding, that’s copy DAO to Entity in Hibernate (my company’s framework) please dont ask why do that ^^.

Here is may code:

static void Main(string[] args)
{
User dto = new User();
//dto.ID = new Guid();
dto.FirstName = "Bao";
dto.MiddleName = "Duc Phuc";
dto.LastName = "Nguyen";

UserBE be = new UserBE();
be = ConvertDTOtoBE<User, UserBE>(dto, be) as UserBE;

PropertyInfo[] pi1 = typeof(User).GetProperties();
PropertyInfo[] pi2 = typeof(UserBE).GetProperties();
Console.WriteLine(typeof(User).Name);
foreach (PropertyInfo pi in pi1)
{
Console.WriteLine(pi.Name + " : " + pi.GetValue(dto, null));
}
Console.WriteLine("\n" + typeof(UserBE).Name);
foreach (PropertyInfo pi in pi2)
{
Console.WriteLine(pi.Name + " : " + pi.GetValue(be, null));
}
Console.WriteLine();
}

public static T2 ConvertDTOtoBE<T1, T2>(T1 t1, T2 t2)
where T2:class,new()
{
//T2 t2 = new T2();
t2 = new T2();
Type type1 = typeof(T1);

PropertyInfo[] pi1 = type1.GetProperties();
PropertyInfo[] pi2 = typeof(T2).GetProperties();
PropertyInfo piTmp;
foreach (PropertyInfo pi in pi1)
{
piTmp = pi2.FirstOrDefault(p => p.Name == pi.Name);
if (piTmp != null && piTmp.CanWrite && piTmp.PropertyType.Equals(pi.PropertyType))
{
piTmp.SetValue(t2, pi.GetValue(t1, null),null);
//Console.WriteLine(pi.Name + " : " + pi.GetValue(t1, null));
}
}
return t2;
}




C# Tutorial – Using Reflection to Get Object Information

2 04 2009
public class MyObject { //public fields public string myStringField; public int myIntField; public MyObject myObjectField;
//public properties public string MyStringProperty { get; set; } public int MyIntProperty { get; set; } public MyObject MyObjectProperty { get; set; } //public events public event EventHandler MyEvent1; public event EventHandler MyEvent2; }

To get a list of public fields in an object, we’ll use Type’s GetField method:

Type myObjectType = typeof(MyObject);
System.Reflection.FieldInfo[] fieldInfo = myObjectType.GetFields(); foreach (System.Reflection.FieldInfo info in fieldInfo) Console.WriteLine(info.Name); // Output: // myStringField // myIntField // myObjectField

An important thing to note here is that the fields are not guaranteed to come out in any particular order. If you use GetFields, you should never depend on the order being consistent. The FieldInfo class that gets returned actually contains a lot of useful information. It also contains the ability to set that field on an instance of MyObject – that’s where the real power comes in.

MyObject myObjectInstance = new MyObject();
foreach (System.Reflection.FieldInfo info in fieldInfo) { switch (info.Name) { case "myStringField": info.SetValue(myObjectInstance, "string value"); break; case "myIntField": info.SetValue(myObjectInstance, 42); break; case "myObjectField": info.SetValue(myObjectInstance, myObjectInstance); break; } } //read back the field information foreach (System.Reflection.FieldInfo info in fieldInfo) { Console.WriteLine(info.Name + ": " + info.GetValue(myObjectInstance).ToString()); } // Output: // myStringField: string value // myIntField: 42 // myObjectField: MyObject

Combining this ability with the ability to create custom attributes provides a framework on which almost any serialization technique can be built.

Properties and events are retrieved almost identically to fields:

Type myObjectType = typeof(MyObject);
//Get public properties System.Reflection.PropertyInfo[] propertyInfo = myObjectType.GetProperties(); foreach (System.Reflection.PropertyInfo info in propertyInfo) Console.WriteLine(info.Name); // Output: // MyStringProperty // MyIntProperty // MyObjectProperty //Get events System.Reflection.EventInfo[] eventInfo = myObjectType.GetEvents(); foreach (System.Reflection.EventInfo info in eventInfo) Console.WriteLine(info.Name); // Output: // MyEvent1 // MyEvent2