InvalidCastExeption with ObjectBuilder

Dec 12, 2006 at 8:03 AM
I'm using the ObjectBuilder with Brad Wilsons DependencyContainer. Here I see an InvalidCastException at:
ReflectionStrategy.cs:80
private ParameterAttribute GetInjectionAttribute(ParameterInfo parameterInfo)
{
ParameterAttribute[] attributes = (ParameterAttribute[])parameterInfo.GetCustomAttributes
(typeof (ParameterAttribute), true);


Object of type System.Object[] can not be converted to type Microsoft.Practices.ObjectBuilder.ParameterAttribute[]

The class is build like:

class ClassOne {
Dependency
public ClassTwo class2 {get; set;}
Dependency
public ClassThree [] class3 {get; set;}
}

ClassTwo and ClassThree contain no further dependencies, just data.
Calling :
DependencyContainer container = new DependencyContainer();
ClassOne resp = container.Get<ClassOne>();

ClassTwo gets build, ClassThree fails

What is wrong with this??

The problem is with the ClassThree Array, which I would expect to be initialized as
class3 = new ClassThree0. There could probably a strategy for this - but here I failed.

Exprecting good ideas from here

BP
Coordinator
Dec 14, 2006 at 6:51 PM
Yeah, something doesn't make sense here. That cast should always succeed. It's asking reflection for an array of the specific type of custom attribute.

I have no idea what the real problem is. Can you make a very small repro?
Dec 15, 2006 at 11:54 AM
Yaeh, I've got an extension to the DISample that I could send anywhere just knowing where it is.

In short:
one class ist added:
/// ++++ CODE
public class ObjectWithArray
{
private Object1 obj1;

Dependency
public Object1 Obj1 {
get { return obj1; }
set { obj1 = value; }
}

private Object1[] arrObj;

Dependency
public Object1[] ArrObj {
get { return arrObj; }
set { arrObj = value; }
}
}
/// ---- CODE

and one TestMethod is added:

/// ++++ CODE
TestMethod
public void ObjectWithArrayTest( ) {
Builder builder = new Builder();
DependencyContainer container = new DependencyContainer();

// ==>> throws InvalidCastException
ObjectWithArray obj = container.Get<ObjectWithArray>();

Assert.IsNotNull( obj );
Assert.IsNotNull( obj.Obj1 );
Assert.IsNotNull( obj.ArrObj );
}
/// ---- CODE