Deep cloning fails because of missing setter

Aug 3, 2011 at 4:40 PM

I try to deep clone an object via

myObject.InjectFrom( _origObject );

Now I get an exception that says that a setter is missing. Sure, not all properties of my business objects have setters.

If all properties have setters I get a TargetParameterCountException. What is this about?

Coordinator
Aug 3, 2011 at 5:06 PM

well, this a reason why the DeepClone injection is not yet embedded in the dll, but posted as a codeplex page : )

you should be doing actually

myObject.InjectFrom<CloneInjection>(originObject);

and if you get an error try debugging and see what's the real problem, (probably modify the code)

Aug 3, 2011 at 7:51 PM

Ok, thank you. The problem is:

I have a class that implements IDataErrorInfo (Silverlight app), so exists a property

public string Error { get; }

You can also implement public string Error { get; set; } , same effect:

TargetParameterCountException is thrown. Why?

Coordinator
Aug 3, 2011 at 8:07 PM

do you know the specific line where you get this error ?

do all your types that are being cloned have default constructors ?

Aug 3, 2011 at 9:01 PM
Edited Aug 3, 2011 at 9:46 PM

Just debugged the silverlight source code. As I said I implement Interface IDataErrorInfo. The problem is not the Error property but

 

      public string this[string columnName]
      {
         get
         {
            return null;
         }
      }

 

There I get the TargetParameterCountException at Extension.cs in

 

public static object GetValue(this PropertyInfo propertyInfo, object from)
        {
            return propertyInfo.GetValue(from, null);
        }

This exception can be removed by simply wrapping the return with try/catch and return null in case of an TargetParameterCountException.

Doing so I get the next exception:

Object of type 'System.Collections.Generic.List`1[xyz]' cannot be converted to type 'System.Collections.ObjectModel.ObservableCollection`1[abc]'.

in CloneInjection class' SetValue method. If write

var tlist = typeof(ObservableCollection<>).MakeGenericType(t); 
instead of
var tlist = typeof(List<>).MakeGenericType(t);
it works as long as T from source and target are the same. Does someone has a solution to the ObservableCollection problem (maybe only in Silverlight a problem??)?

Coordinator
Aug 4, 2011 at 9:47 AM
Edited Aug 4, 2011 at 9:48 AM

when you do 

addMethod.Invoke(list, new[] { e });
basically the tlist.Add(item) happens
and of course your going to get an error if your adding an element of type T1 to an ObservableCollection<T2>,
the thing is though that this should not happen since you are cloning an object which means that you create an object of the same type pass all the values from a to b
and after start cloning all the reference types properties and since you are matching them by name (c.SourceProp.Name == c.TargetProp.Name in Match method) 
going from Collection<T1> to Collection<T2> can not occur