Spring.Net error: PropertyAccessExceptions. Spring.Core.TypeMismatchException: Cannot convert property value

836 Views Asked by At

I have a class:

public GenericImplementation:IGeneric<ICar>
{
    public IDrive DriveImplementation {get;set;}
}

And an interface:

public interface IGeneric<ICar> 
{

}

public interface IDrive<T>
{
     void Drive(T);
}

And an implementation:

public class Drive:IDrive<ICar>
{
   public void Drive(ICar car)
   {
   }
}

And a class that uses IGeneric:

public class GenericUser:IUser
{
  public IGeneric GenericImplementation {get;set;}
}

All classes exist under carnamespace.

In spring.net configuration, I have:

<object id="Drive" type="carnamespace.Drive, carnamespace"/>
<object id="GenericImplementation" type="carnamespace.GenericImplementation, carnamespace">
  <property name="DriveImplementation" ref="Drive"/>
</object>

<object id="GenUser" type="carnamespace.GenericUser, carnamespace">
  <property name="GenericImplementation" value="GenericImplementation"/>
</object>

But I keep getting this error:

PropertyAccessExceptionsException (1 errors); nested PropertyAccessExceptions are: 
[Spring.Core.TypeMismatchException: Cannot convert property value of type [carnamespace.GenericImplementation] to required type [carnamespace.IGeneric`1[[carnamespace.ICar, carnamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]] for property 'GenericImplementation'., Inner Exception: Spring.Core.TypeMismatchException: 

Can someone please help me understand what the resolution is?

1

There are 1 best solutions below

0
On

There is one obvious flaw in your configuration:

<!-- ... snip ... -->   
<object id="GenUser" type="carnamespace.GenericUser, carassembly"> <!-- assemblyname after comma -->
  <!-- 
  Use ref instead of value!
  <property name="GenericImplementation" value="GenericImplementation"/>
  -->
  <property name="GenericImplementation" ref="GenericImplementation"/>
</object>

This gave me a PropertyAccessExceptions with a nested TypeMismatchException too.

I've had a hard time reproducing your problem, because your code doesn't compile. If this doesn't solve your problem, please post a compiling code sample. Also, make sure you can at least construct your objects in code, e.g.:

public void ConstructFromCode()
{
    var drive = new Drive();
    var genImp = new GenericImplementation {DriveImplementation = drive};
    var genUser = new GenericUser {GenericImplementation = genImp};
}