ConventionInjection removal

Jun 5, 2015 at 3:39 PM
With the latest update, ConventionInjection has been removed. I was extending this injection to provide matching between properties with the same names but where one side may have been nullable and the other not or enums and ints.

I was then subclassing that to provide an injection that ignored certain properties.

How would you recommend adjusting this for the new release? The subclasses are provided below.
    public class CustomMatch : ConventionInjection
    {
        protected override object SetValue(ConventionInfo c)
        {
            object rv = c.SourceProp.Value;
            if (c.SourceProp.Type.IsSubclassOf(typeof(Enum)) && !c.TargetProp.Type.IsSubclassOf(typeof(Enum)))
            {
                rv = Convert.ChangeType(rv, ((Enum)rv).GetTypeCode());
            }
            else if (!c.SourceProp.Type.IsSubclassOf(typeof(Enum)) && c.TargetProp.Type.IsSubclassOf(typeof(Enum)))
            {
                rv = Convert.ChangeType(rv, c.TargetProp.Type);
            }
            return rv;
        }

        protected override bool Match(ConventionInfo c)
        {
            return c.SourceProp.Name == c.TargetProp.Name &&                                    // Names match
                    (c.SourceProp.Type == c.TargetProp.Type                                     // Types match
                    || c.SourceProp.Type == Nullable.GetUnderlyingType(c.TargetProp.Type)       // Non-null setting nullable of same type
                    || (Nullable.GetUnderlyingType(c.SourceProp.Type) == c.TargetProp.Type
                            && c.SourceProp.Value != null)                                      // Nullable setting non-null of same type
                    || (c.SourceProp.Type == typeof(int) && c.TargetProp.Type.IsSubclassOf(typeof(Enum)))    // int setting enum
                    || (Nullable.GetUnderlyingType(c.SourceProp.Type) == typeof(int) && c.TargetProp.Type.IsSubclassOf(typeof(Enum))
                            && c.SourceProp.Value != null)                                                  // nullable int setting enum
                    || (c.SourceProp.Type.IsSubclassOf(typeof(Enum)) && c.TargetProp.Type == typeof(int))   // enum setting int
                    || (c.SourceProp.Type.IsSubclassOf(typeof(Enum)) && Nullable.GetUnderlyingType(c.TargetProp.Type) == typeof(int))   // enum setting nullable int
                    );
        }
    }

    public class IgnoreProperties : CustomMatch
    {
        private readonly IEnumerable<string> _ignores;

        public IgnoreProperties(params string[] s)
        {
            _ignores = s;
        }

        protected override bool Match(ConventionInfo c)
        {
            return _ignores.Contains(c.SourceProp.Name) ? false : base.Match(c);
        }
    }
Coordinator
Jun 5, 2015 at 5:27 PM
Edited Jun 5, 2015 at 5:29 PM
use the LoopInjection,
it already has a constructor with params string[] properties to ignore

have a look here: https://github.com/omuleanu/ValueInjecter/wiki/custom-injections-examples