I'm trying to call the Buildup method like this
Type type = typeof(T);
I've been having so many problems with it that I've completely stripped the class down by removing all the dependent properties and methods. I would think then that in theory it would just do nothing to the object but it continues to fail every single
time. It seems like it's related to the fact that there are no public constructors on the object but I don't see why that would matter because according to the documentation:
Unity exposes a method named BuildUp that you can use to pass existing object instances through the container in order to apply dependency injection to that object. This is an alternative to resolving the object
using any of the other techniques available with Unity. However, remember that the
BuildUp method cannot inject dependent objects into constructor parameters, because the object has already been created; it is not created by Unity.
However, the reason I believe the issue is related to the constructor is because I've stepped through Unity's code and this method in
DynamicMethodConstructorStrategy.cs is the method throwing the exception (the bold code is returning true)
// Verify the type we're trying to build is actually constructable -
// CLR primitive types like string and int aren't.
private static void GuardTypeIsNonPrimitive(IBuilderContext context, SelectedConstructor selectedConstructor)
var typeToBuild = context.BuildKey.Type;
if(typeToBuild == typeof(string) ||
selectedConstructor == null) <---- is always true because there are no public constructors on the class
throw new InvalidOperationException(
Am I doing something completely wrong here? I don't even understand why the buildup method would be implementing such a function if the object is already instantiated.