Steps to map classes using ValueInjector

2019-08-16 22:00发布

问题:

Quickly getting to the problem the mapping does not occur for the following code. Could someone explain why? or what i should do for the mapping to occur?

var parent = new Parent();
parent.ChildOne.Add(new ChildOne() { Name = "Child One" });
parent.ChildTwo.Add(new ChildTwo() { Name = "Child Two" });
AnotherParent anotherParent = new AnotherParent();

anotherParent.InjectFrom<LoopValueInjection>(parent);

Required Class are below

Anothher child one

 public class AnotherChildOne
    {
        public string Name { get; set; }
    }

Another child two

 public class AnotherChildTwo
    {
        public string Name { get; set; }

    }

Another Parent

public class AnotherParent
    {
        public ICollection<AnotherChildOne> ChildOne { get; set; }

        public ICollection<AnotherChildTwo> ChildTwo { get; set; }

        public AnotherParent()
        {
            ChildOne = new Collection<AnotherChildOne>();
            ChildTwo = new Collection<AnotherChildTwo>();
        }


    }

Child Two

public class ChildTwo
    {
        public string Name { get; set; }

    }

Child One

 public class ChildOne
    {
        public string Name { get; set; }
    }

Parent

 public class Parent
    {

        public ICollection<ChildOne> ChildOne { get; set; }

        public ICollection<ChildTwo> ChildTwo { get; set; }

        public Parent()
        {
            ChildOne = new Collection<ChildOne>();
            ChildTwo = new Collection<ChildTwo>();
        }

    }

回答1:

I believe by default Value Injector will only inject the properties with the same name of the same type. You can get around this using a tweak to the CloneInjection sample from the Value Injector documentation as described here with this code:

public class CloneInjection : ConventionInjection
{
    protected override bool Match(ConventionInfo c)
    {
        return c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Value != null;
    }

    protected override object SetValue(ConventionInfo c)
    {
        //for value types and string just return the value as is
        if (c.SourceProp.Type.IsValueType || c.SourceProp.Type == typeof(string)
            || c.TargetProp.Type.IsValueType || c.TargetProp.Type == typeof(string))
            return c.SourceProp.Value;

        //handle arrays
        if (c.SourceProp.Type.IsArray)
        {
            var arr = c.SourceProp.Value as Array;
            var clone = Activator.CreateInstance(c.TargetProp.Type, arr.Length) as Array;

            for (int index = 0; index < arr.Length; index++)
            {
                var a = arr.GetValue(index);
                if (a.GetType().IsValueType || a.GetType() == typeof(string)) continue;
                clone.SetValue(Activator.CreateInstance(c.TargetProp.Type.GetElementType()).InjectFrom<CloneInjection>(a), index);
            }
            return clone;
        }


        if (c.SourceProp.Type.IsGenericType)
        {
            //handle IEnumerable<> also ICollection<> IList<> List<>
            if (c.SourceProp.Type.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable)))
            {
                var t = c.TargetProp.Type.GetGenericArguments()[0];
                if (t.IsValueType || t == typeof(string)) return c.SourceProp.Value;

                var tlist = typeof(List<>).MakeGenericType(t);
                var list = Activator.CreateInstance(tlist);

                var addMethod = tlist.GetMethod("Add");
                foreach (var o in c.SourceProp.Value as IEnumerable)
                {
                    var e = Activator.CreateInstance(t).InjectFrom<CloneInjection>(o);
                    addMethod.Invoke(list, new[] { e }); // in 4.0 you can use dynamic and just do list.Add(e);
                }
                return list;
            }

            //unhandled generic type, you could also return null or throw
            return c.SourceProp.Value;
        }

        //for simple object types create a new instace and apply the clone injection on it
        return Activator.CreateInstance(c.TargetProp.Type)
            .InjectFrom<CloneInjection>(c.SourceProp.Value);
    }
}

If you include the above CloneInjection code you will want to do this:

anotherParent.InjectFrom<CloneInjection>(parent);

instead of:

anotherParent.InjectFrom<LoopValueInjection>(parent);