Search code examples
c#valueinjecter

The type or name "KnownSourceValueInjection" could not be found


I Would like to know why is this method found as an answer on many questions here not working, there are errors in "KnownSourceValueInjection". Also "GetByName(...)"is not working, it says: "propertyinfo[]" does not contain a definition for "GetByName" accepting a first argument of type "propertyinfo[]" could be found.I'm working on a web service. I'm using:

  • using Omu.ValueInjecter;
  • using Omu.ValueInjecter.Utils;
  • using Omu.ValueInjecter.Injections;

This is the method.

    public class ReaderInjection : KnownSourceValueInjection<IDataReader>
    {
        protected override void Inject(IDataReader source, object target)
        {
            for (var i = 0; i < source.FieldCount; i++)
            {
                var activeTarget = target.GetProps().GetByName(source.GetName(i), true);
                if (activeTarget == null) continue;

                var value = source.GetValue(i);
                if (value == DBNull.Value) continue;

                activeTarget.SetValue(target, value);
            }
        }
    }

Solution

  • use KnownSourceInjection, it was renamed in the new version; and for the ReaderInjection see the source here

    public class ReaderInjection : KnownSourceInjection<IDataReader>
    {
        protected override void Inject(IDataReader source, object target)
        {
            for (var i = 0; i < source.FieldCount; i++)
            {
                var trgProp = target.GetType().GetProperty(source.GetName(i), BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                if (trgProp == null) continue;
    
                var value = source.GetValue(i);
                if (value == DBNull.Value) continue;
    
                trgProp.SetValue(target, value);
            }
        }
    }