1

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!

  • Out of curiosity, why go through this trouble at all? What's the need for a type A2 that is a mirror of all properties of type A, per the very beginning of your question? Why can't you just use the A type? Also, did you find [this answer about converting between enums](https://stackoverflow.com/questions/1818131/convert-an-enum-to-another-type-of-enum)? – Sean Skelly Apr 08 '20 at 22:33
  • I don't think enum has properties. Those items on enum are fields. – weichch Apr 08 '20 at 23:19
  • Sean Skelly I need to do this because I'm working on a wrapper for a service that works with three differetn webservices that use for example thre different enums with equivalent values, so, converting that three different enums to one (and viceversa) I can do all the calls to all methods in three services from only one wrapper method based in some conditions...perhaps I'm thinking this in the wrong way but it works, except from enums... – Rodrigo Fulanito Apr 09 '20 at 16:38

1 Answers1

1

If you need to cast by value, you could do:

if (sourceType.IsEnum && targetType.IsEnum)
{
    destObject = (T)sourceObject;
    return;
}

Or by name:

if (sourceType.IsEnum && targetType.IsEnum)
{
    destObject = (T)Enum.Parse(typeof(T), sourceObject.ToString());
    return;
}
weichch
  • 9,306
  • 1
  • 13
  • 25