If you didn't need to use interfaces, you could get by using concrete types:
using System;
using Microsoft.Practices.Unity;
using Newtonsoft.Json;
namespace TestGrounds
{
public class TestClass
{
#region Properties
public int TestIntegerProperty { get; set; }
public string TestStringProperty { get; set; }
#endregion
}
internal class Program
{
#region Static Methods
private static void Main(string[] args)
{
const string json =
@"{ TestIntegerProperty: 1, TestStringProperty: 'Hello', AnotherTestPropertyToIgnore: 'Sup' }";
registerDependencyFromJson<TestClass>(json);
Console.ReadKey();
}
private static void registerDependencyFromJson<T>(string json) where T: class, new()
{
var deserializedObject = JsonConvert.DeserializeObject<T>(json);
var type = deserializedObject.GetType();
var container = new UnityContainer();
container.RegisterInstance(type, type.Name, deserializedObject, new ContainerControlledLifetimeManager());
}
#endregion
}
}
Which might be better anyway, since interfaces could have required method implementations on them that any sort of proxying wouldn't handle very well (though, I think Castle has a method interceptor of some sort). Concrete types get rid of any sort of assumption there; the only real requirement is new().
Update:
Here's a sample creating the type from a string name, and also showing an invalid type:
using System;
using Microsoft.Practices.Unity;
using Newtonsoft.Json;
namespace TestGrounds
{
public class TestClass
{
#region Properties
public int TestIntegerProperty { get; set; }
public string TestStringProperty { get; set; }
#endregion
}
public class BadTestClass : TestClass
{
#region Properties
public double TestDoubleProperty { get; set; }
#endregion
#region Constructors
public BadTestClass(double testDouble)
{
TestDoubleProperty = testDouble;
}
#endregion
}
internal class Program
{
#region Static Methods
private static void Main(string[] args)
{
const string json =
@"{ TestIntegerProperty: 1, TestStringProperty: 'Hello', AnotherTestPropertyToIgnore: 'Sup' }";
var type = Type.GetType("TestGrounds.TestClass", true);
var badType = Type.GetType("TestGrounds.BadTestClass", true);
registerDependencyFromJson(type, json);
try
{
registerDependencyFromJson(badType, json);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
private static void registerDependencyFromJson(Type type, string json)
{
// type requires a default constructor for this to work
var constructor = type.GetConstructor(Type.EmptyTypes);
if(constructor == null)
{
throw new ArgumentException("Type must have a parameterless constructor.");
}
var deserializedObject = JsonConvert.DeserializeObject(json, type);
var container = new UnityContainer();
container.RegisterInstance(type, type.Name, deserializedObject, new ContainerControlledLifetimeManager());
}
#endregion
}
}