Igor Strekha
Igor Strekha

Reputation: 186

How to map object with enums properties to object with values?

I have the class.

public class ProductModel
{
    public long ProductId { get; set; }

    public int ContainerType { get; set; }

    public SolidForm SolidForm { get; set; }
}

SolidForm is Enum.

public enum SolidForm
{

    None = 0,

    Molten = 1,

    Solution = 2,

    InPowderedForm = 3
}

I need to create another model For Example

public class Product
{
    public long ProductId { get; set; }

    public int ContainerType { get; set; }

    public int SolidForm { get; set; }
}

Where property SolidForm is Value of Enum.

For example if I get in the first model SolidForm = SolidForm.Molten
after conversion in Product I want get int SolidForm = 1.

Upvotes: 1

Views: 3649

Answers (4)

Greg
Greg

Reputation: 11478

Though several answers exists, you could do the following:

public TType ConvertEnum<TType, TEnum>(TEnum content) 
{
     var validated = typeof(TEnum);
     if(!validated.IsEnum)
          throw new Exception("Non enum passed.");

     var conversion = content as TType;
     if(conversion != null)
          return conversion;

     return default(TType);
}

A clean reusable method, plus it should support conversion of different types, checked the fiddle and appears to work correctly for most implementations.

So in your case, when you set a value of the property it would be:

Product.SolidForm = ConvertEnum<int, SolidForm>(SolidForm.Molten);

All though I believe that by default:

public enum SolidForm
{
     None,
     Molten,
     Solution
}

Your code doesn't appear to need the equal since it represents the default incremented approach, since you aren't overriding the values.

By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example, in the following enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth.

Copy enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri}; Enumerators can use initializers to override the default values, as shown in the following example.

Copy enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri}; In this enumeration, the sequence of elements is forced to start from 1 instead of 0. However, including a constant that has the value of 0 is recommended. For more information, see Enumeration Types. Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of enumeration elements is int. To declare an enum of another integral type, such as byte, use a colon after the identifier followed by the type, as shown in the following example.

Update:

public static TDestination MapProperty<TSource, TDestination>(TSource source, TDestination destination)
{
    PropertyInfo[] sourceProperties = typeof(source).GetProperties();
    foreach(var property in sourceProperties)
    {
        var destinationProperty = typeof(TDestination).GetProperty(property.Name);
        if(destinationProperty != null)
        {
            // Error handling, validation of type, a bunch of other checks should go here.
            var value = ((destinationProperty.PropertyType)property.GetValue(source, null));
            destinationProperty.SetValue(destination, value, null);
        }
    }

    return destination;
}

Important to note, this will require a lot of exception handling. Since you'll have to know about enum, nullable types, etc. to avoid the conversion from failing. Also, if your code has such large objects the reflection may be slow.

The basics of this code, loop through source object, find match on destination object, then do a raw cast, then set value, then return the destination object. You may also need Activator.CreateInstance(typeof(Destination)); I wrote the code without a compiler really quick, so bank on refinement as I denoted above. This should be a solid starting point though.

Upvotes: 1

Aravind Belagaje
Aravind Belagaje

Reputation: 406

You can directly convert from enum to int just use typecasting

(int) SolidForm;

Upvotes: 0

Ya Wang
Ya Wang

Reputation: 1808

Enum to int

(int)ProductModel.SolidForm

int to enum

(SolidForm)Enum.ToObject(typeof(SolidForm), ProductModel.SolidForm)

What mapper are you using?

You can just list the enums in your enum declaration if they are going in order like that

Upvotes: 0

Leon Barkan
Leon Barkan

Reputation: 2703

like that?

        Product p = new Product
        {
            SolidForm = (int)SolidForm.Solution
        };

Upvotes: 0

Related Questions