0

I've been using Omu.ValueInjecter for a while now but only to 'inject' values into a newly created object.

    public K MapNew(T source) {

        K target = new K();

        target.InjectFrom(source);

        return target;
    }

The code above works well, mapping values from the source to the newly created target.

However, what I'm trying (and failing to do) is to map values from a source object to an existing target. The code below is similar to the one above with the difference being that I'm not creating a new object.

    public T MapToTarget(T target, K source) {
        return target.InjectFrom(source) as T;
    }

Is this possible? The code above merely returns the target with its properties un-modified.

EDIT

Note, in both cases both source and target objects have the same property names.

NOTE

Thanks to Omu for all the help, much appreciated.

1 Answer 1

1

the InjectFrom() method matches the properties with same name and type, so if there are properties that match they will be modified

with valueinjecter you only inject into existing objects

7
  • The property names of the source and target are the same, but in the second bit of code I want the values of the source to be written into the target. That isn't happening. I'm wondering if I'm either doing something wrong or it can't be done. Aug 7, 2014 at 12:16
  • @DanielHollinrake names and types, it has to be the exact name and type; if you want to transform from one type to another you should create a custom valueinjection
    – Omu
    Aug 7, 2014 at 14:09
  • Hiya, just to confirm that the properties I'm expecting to match are the exact name and type. There are some properties on the target object that don't exist on the source but in this case I don't want, or expect, a match. Aug 7, 2014 at 15:18
  • in this case it should work, put the source and target classes in your question, or upload a zip with a mini project showing the problem
    – Omu
    Aug 7, 2014 at 17:49
  • 1
    there are examples of nullable injections here: valueinjecter.codeplex.com/…
    – Omu
    Aug 8, 2014 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.