Hello I'm using reflection to convert object type A to equivalent object type A2, this two types has the same properties and attributes and for the conversion I'm using this rutine:
public static void CopyObject<T>(object sourceObject, ref T destObject)
{
// If either the source, or destination is null, return
if (sourceObject == null || destObject == null)
return;
// Get the type of each object
Type sourceType = sourceObject.GetType();
Type targetType = destObject.GetType();
// Loop through the source properties
foreach (PropertyInfo p in sourceType.GetProperties())
{
// Get the matching property in the destination object
PropertyInfo targetObj = targetType.GetProperty(p.Name);
// If there is none, skip
if (targetObj == null)
{
// targetObj = Activator.CreateInstance(targetType);
continue;
}
// Set the value in the destination
targetObj.SetValue(destObject, p.GetValue(sourceObject, null), null);
}
}
This works great for simple objects whith equivalent properties names, but the problem is when the soruce and taget object are of any ENUM type.
This line:
foreach (PropertyInfo p in sourceType.GetProperties())
Return no PropertyInfo object, so the loop does not run and the changes are not made, ther is no error, just not working.
So, is ther anyway to convert using reflection one object of enum type A to object of enum type A1 I know is something does not make any sense, but I need this to adapt my code to and existin application that I do not have the source code.
The idea is:
Having two enums
public enum A
{
vallue1=0,
value2=1,
value3=2
}
public enum A1
{
vallue1=0,
value2=1,
value3=2
}
and two objects of those enums types:
A prop1 {get;set;}
A1 prop2 {get;set;}
I need two get the enum value of prop1 and set that value in prop2 getting the equivalent value in the second enumeration, in a generic way (that's why I'm using reflection)?
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire