Feature Request: Display property set method name if it cannot be found on the target.

Jan 30, 2012 at 3:22 PM
Edited Jan 30, 2012 at 3:28 PM

Hi.

Thank you for ValueInjecter.Silverlight 2.3.

In my first time trying Value Injector, I receive a generic exception trying to unflatten the values of one type into another, like this:

Type2 type2 = new Type2();
//set some values to properties on type2

Type1 type1 = new Type1();
type1.InjectFrom<UnflatLoopValueInjection>(type2);


System.ArgumentException occurred
Message=Property set method not found.
StackTrace:
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
at Omu.ValueInjecter.Silverlight.Extensions.SetValue(PropertyInfo propertyInfo, Object where, Object value)
at Omu.ValueInjecter.Silverlight.UnflatLoopValueInjection.Inject(Object source, Object target)
at Omu.ValueInjecter.Silverlight.ValueInjection.Map(Object source, Object target)
at Omu.ValueInjecter.Silverlight.StaticValueInjecter.InjectFrom[T](Object target, Object[] source)
at MySilverlightApp.Views.MyView..ctor(List`1 param1)
InnerException:

I would have expected the exception message to be more detailed and display exactly which property setter method name was not found.
Thank you. :)
public class Type1
{
public string Prop1 {get; set;}
}
public class Type2
{
public string Prop1 {get; set;}
public string Prop2 {get; set;}
}
In this case, I have to tell vi to ignore Prop2 by using the code indicated here: http://valueinjecter.codeplex.com/discussions/234706
If there are a lot of properties, I have to hunt down exactly which properties do not exist in the target.
For example, here is a modified \Silverlight\ValueInjecter.Silverlight\Extensions.cs that displays the property set method name if it cannot be found:
        public static void SetValue(this PropertyInfo propertyInfo, object where, object value)
        {
            try
            {
                propertyInfo.SetValue(where, value, null);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Failed to set property '{0}'", propertyInfo.Name), ex);
            }
        }
Coordinator
Jan 30, 2012 at 4:54 PM

Hi,

I tried your scenario, and I didn't got any error:

using System;
using Omu.ValueInjecter.Silverlight;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            var t1 = new Type1{Prop1 = "aa"};
            var t2 = new Type2{Prop1 = "x1", Prop2 = "x2"};

            t1.InjectFrom<UnflatLoopValueInjection>(t2);
            
        }
    }

    public class Type1
    {
        public string Prop1 { get; set; }
    }

    public class Type2
    {
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }
    }
}

Also, I don't see any need for Unflattening here

Jan 30, 2012 at 5:44 PM

Sorry, I will try to be more precise.

I was getting this error with entity types.  I had to ignore the HasValidationErrors and ValidationErrors properties because they do not have public setters.

I have property name that will require unflattening, Type1 and Type2 were just simplified from my real world example.

.Ignore(new string[]{"HasValidationErrors""ValidationErrors"})
Coordinator
Jan 30, 2012 at 6:11 PM

well, ValueInjecter will try to set value if it find the corresponding property, but if there isn't one, error will not be thrown, so if you have a property on the other side that corresponds to the rules (same name/type etc.) and it has no setter, you know for sure that you'll get an error

Jan 30, 2012 at 6:29 PM

That is understandable.

I would just like to see a more verbose error message that at least includes the property name, as my code changes indicated.

Thank you.

Coordinator
Jan 30, 2012 at 8:06 PM

well the good thing about VI is that you can build your own ValueInjections, as many as you want, and use them instead of the default ones,

you can copy the Source of UnflatLoopValueInjection and modify it as you need