2016-05-30 11 views
0

私は、一般的なインターフェイスの簡単なrefistatiionUnity DI。ジェネリック型の登録とIsRegistered方法

unityContainer.RegisterType(typeof(IMyInterface<>), typeof(MyClass<>)); 

それは簡単unityContainer.Resolve<IMyInterface<MyGenericType>>MyClass<MyGenericType>を解決することができますし、それが機能を持っています。

しかしunityContainer.IsRegistered<IMyInterface<MyGenericType>>()False

質問はされています 1.なぜ? 2. unityContainer.Resolve<IMyInterface<MyGenericType>>が可能かどうかを確認するにはどうすればよいですか?

答えて

0

私の調査結果です。

public static class UnityExtenstions 
{ 
    public static bool IsRegisteredAdvanced<T>(this IUnityContainer unityContainer) 
    { 
     return IsRegisteredAdvanced(unityContainer, typeof(T)); 
    } 

    public static bool IsRegisteredAdvanced(this IUnityContainer unityContainer, Type type) 
    { 
     var result = unityContainer.IsRegistered(type); 
     if (result == false) 
     { 
      var genericTypeDefinition = GetGenericTypeDefinition(type); 
      if (genericTypeDefinition != null) 
      { 
       result = unityContainer.IsRegistered(genericTypeDefinition); 
      } 
     } 
     return result; 
    } 

    private static Type GetGenericTypeDefinition(Type type) 
    { 
     if (type == null) return null; 
     var typeInfo = type.GetTypeInfo(); 
     return (typeInfo.IsGenericType == true) 
      && (typeInfo.IsGenericTypeDefinition == false) 
      ? type.GetGenericTypeDefinition() 
      : null; 
    } 
} 
関連する問題