2011-08-10 7 views
0

구조체, 예를 들어 ITest에 대해 struct Test이라는 인터페이스를 확인하도록 Unity를 구성하고 싶습니다.Unity에서 구조체를 인터페이스에 바인딩 할 수 있습니까?

<unity> 
    <containers> 
     <container> 
      <types> 
       <type 
        type="my.ITest, asm" 
        mapTo="my.Test, asm"> 
       </type> 
      </types> 
     </container> 
    </containers> 
</unity> 

을하지만 난 다음 오류 받고 있어요 : 지금까지 다음이

Resolution of the dependency failed, type = "my.ITest", name = "(none)". 
Exception occurred while: while resolving. 
Exception is: InvalidOperationException - The type Test cannot be constructed. You must configure the container to supply this value. 
At the time of the exception, the container was:  
Resolving my.Test,(none) (mapped from my.ITest,(none)) 

왜?

답변

0

문제는 Unity를 사용하여 구조체를 생성하려고하는 것입니다. 구조체가 값 유형이기 때문에 Activator.CreateInstance는 (인터페이스 때문에) 만들려고 할 때 청크를 날려 버릴 것입니다. 예를 들어

:

var item = Activator.CreateInstance<Test>(); 

"는 인터페이스의 인스턴스를 만들 수 없습니다"예외를 슬로우. 내부적으로 Unity는 아마도 Activator.CreateInstance를 어딘가에 체인을 사용하고있을 것입니다. (저는 Unity의 코드 플렉스를 조금씩 보았습니다.) 그것이 바로 죽을 곳입니다.

구조체 대신 클래스 구현으로 변경하는 것이 좋습니다.

+1

왜'Activator.CreateInstance'는 구조체를 만들 수 없습니다? – Lee

+0

나는 이것을 시험했다; 구조체에 인터페이스가 있으면 CreateInstance 호출이 실패합니다. 이유를 알 수 없습니다. – Tejs

+1

'Int32'는 많은 인터페이스를 구현하며 잘 작동합니다. – Lee

관련 문제