Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8680857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:21:25+00:00 2026-06-12T21:21:25+00:00

Quickly getting to the problem the mapping does not occur for the following code.

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:21:26+00:00Added an answer on June 12, 2026 at 9:21 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple (not?) problem with getting a focus on an JInternalFrame after
I'm just getting the hang of Rails console, and finding it useful for quickly
Can someone please help quickly mute or unmute the stage volume in Flash CS3
I'm having trouble getting a Java Applet to communicate with the Javascript code on
So I've written a quick bit of code to help quickly convert between business
I'm having a fundamental problem with getting scrolling to work normally on my iPhone
I'm just getting started using ADO.NET and DataSets and DataTables. One problem I'm having
fairly new Android developer here. I've come across a strange problem that I'm not
I use the following line of code within a single method to explicitly check
I need help with the Pythonic looping overhead of the following problem: I'm writing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.