Showing posts with label Generic WCF Converter. Show all posts
Showing posts with label Generic WCF Converter. Show all posts

Tuesday, February 16, 2010

Generic WCF Converter function

public TDestination Convert(TSource objSource, TDestination objDestination)
{
if (objSource == null)
{
return default(TDestination);
}

if (objDestination == null)
{
return default(TDestination);
}

// Read Destination Properties
foreach (PropertyInfo propInfo in objDestination.GetType().GetProperties())
{
if (!propInfo.PropertyType.IsGenericType)
{
// Read Source Properties
object val = null;
foreach (PropertyInfo info in objSource.GetType().GetProperties())
{
// Match Source & Destination Properties
if (propInfo.Name == info.Name)
{
if (info.CanRead)
{
val = info.GetValue(objSource, null);
}

if (propInfo.CanWrite)
{
// If Source is Enum Type and Destination is byte type
if (info.PropertyType.IsEnum == true && propInfo.PropertyType.IsEnum == false)
{
byte tmpVal = System.Convert.ToByte(info.PropertyType.GetField(info.GetValue(objSource, null).ToString()).GetRawConstantValue());
propInfo.SetValue(objDestination, tmpVal, null);
}
else
{
propInfo.SetValue(objDestination, val, null);
}
}

break;
}
}
}
}

return objDestination;
}