IOCHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IocApplication
{
public static class IOCHelper
{
private static Func<Type, object> _resolveFunction;
private static Func<object, Type, object> _resolveKeyedFunction;
public static void SetResolveFunction(Func<Type, object> resolveFunction)
{
_resolveFunction = resolveFunction;
}
public static T Resolve<T>()
{
if (_resolveKeyedFunction == null)
{
throw new InvalidOperationException("Resolve function is not set.");
}
else
{
return (T)_resolveFunction(typeof(T));
}
}
public static object Resolve(Type type)
{
if(_resolveFunction == null)
{
throw new InvalidOperationException("异常");
}
else
{
return _resolveFunction(type);
}
}
public static T ResolveKeyed<T>(object key)
{
if(_resolveKeyedFunction == null)
{
throw new InvalidOperationException("异常");
}
else
{
return (T)_resolveKeyedFunction(key,typeof(T));
}
}
}
}