Abstract class as property type.

Aug 22, 2012 at 2:31 PM

Hello,

Emit Mapper gets very angry when I ask him to map an object of type AbstractContainer (see below).

 public abstract class AbstractClass
{
 public abstract int Value { get; set; }
 public string ParentName { get; set; }
}
public class ExtendAbstract : AbstractClass
{
 public override int Value { get; set; }
 public string ChildName { get; set; }
}
public class AbstractContainer
{
 public AbstractClass InnerAbstract { get; set; }
 public ExtendAbstract InnerExtend { get; set; }
}

 Is there a way of achieving this without modifying the classes?

Thanks,

JackTheRapper.

Developer
Aug 24, 2012 at 2:33 PM

what is mapping code?

I have not issues with such code:

 

public abstract class AbstractClass
{
   public abstract int Value { get; set; }
   public string ParentName { get; set; }
}

public class ExtendAbstract : AbstractClass
{
   public override int Value { get; set; }
   public string ChildName { get; set; }
}

public class AbstractContainerFrom
{
   public AbstractClass InnerAbstract { get; set; }
   public ExtendAbstract InnerExtend { get; set; }
}

public class AbstractContainerTo
{
   public AbstractClass InnerAbstract { get; set; }
   public ExtendAbstract InnerExtend { get; set; }
}
 
public void Test()
{
           var m = ObjectsMapperManager.DefaultInstance.GetMapper<AbstractContainerFrom, AbstactContainerTo>();
            var from = new AbstractContainerFrom()
                {
                    InnerAbstract = new ExtedAbstract() 
                    { 
                         ParentName = "parent1", 
                         ChildName = "child1", 
                         Value = 1 
                    },
                    InnerExtend = new ExtedAbstract() 
                    { 
                          ParentName = "parent2", 
                          ChildName = "child2", 
                          Value = 2 
                    } 
                };
            var to = m.Map(from);
            Assert.AreEqual("parent1",to.InnerAbstract.ParentName);
            Assert.AreEqual("parent2",to.InnerExtend.ParentName);
            Assert.AreEqual("child2",to.InnerExtend.ChildName);
}

Aug 30, 2012 at 1:19 PM

Thanks qwer_ty! I have not been precise enough. My classes are:

 

        public abstract class AbstractClassFrom
        {
            public abstract int Value { get; set; }
            public string ParentName { get; set; }
        }

        public abstract class AbstractClassTo
        {
            public abstract int Value { get; set; }
            public string ParentName { get; set; }
        }

        public class ExtendAbstractFrom : AbstractClassFrom
        {
            public override int Value { get; set; }
            public string ChildName { get; set; }
        }

        public class ExtendAbstractTo : AbstractClassTo
        {
            public override int Value { get; set; }
            public string ChildName { get; set; }
        }

        public class AbstractContainerFrom
        {
            public AbstractClassFrom InnerAbstract { get; set; }
            public ExtendAbstractFrom InnerExtend { get; set; }
        }

        public class AbstractContainerTo
        {
            public AbstractClassTo InnerAbstract { get; set; }
            public ExtendAbstractTo InnerExtend { get; set; }
        }

In this situation, Emit Mapper won't map AbstractContainerFrom to AbstractContainerTo. I have managed to solve the problem by using,

DefaultMapConfig ConvertUsing<From, To>(Func<From, To> converter)

with an appropriate converter.