ジェネリックの依存性注入ハンドラー(ベースのサービスロケーター)で作業しているときに、ジェネリックの問題が発生しています。
編集1(わかりやすくするため)
さて、私は実際にSimpleInjectorをDIリゾルバーとして使用しており、GetInstanceメソッドにクラス制約があるため、より完全なコードを次に示します。
public T GetInstance<T>() where T : class
{
try
{
// works
return _container.GetInstance<T>();
}
catch( ActivationException aEx )
{
return default( T );
}
}
public T GetInstance<T>()
{
try
{
if( typeof( T ).IsClass )
{
// does not work, T is not a reference type
return _container.GetInstance<T>();
}
}
catch( ActivationException aEx )
{
return default( T );
}
}
編集2-コメントでは奇妙に見えるので、最終的なコード:
public T GetInstance<T>()
{
try
{
if( typeof( T ).IsClass )
{
return (T) _container.GetInstance(typeof(T));
}
}
catch( ActivationException aEx )
{
return default( T );
}
}