2016-09-16 3 views
0

나는 인터페이스 참조 IInterfaceA를 반환하는 방법이 있고, 일을 IInterfaceB에 캐스팅 할이 :캐스트

IInterfaceA a = SomeMethodThatReturnAnIInterfaceA(); 
IInterfaceB b = (IInterfaceB)a; 

public IInterfaceA : OtherInterfaceA, OtherInterfaceB {} 
public IInterfaceB : IInterfaceB {} 

그러나 런타임에 내가 가지고 :

Unable to cast COM object of type 'OPCAutomation.OPCGroupClass' to interface 
type 'SemaforosNNM.OPC.OPCDaGroup'. This operation failed because the 
QueryInterface call on the COM component for the interface with IID 
'{70F93164-7F80-37E3-8EFB-DAB08298316E}' failed due to the following error: 
Interfaz no compatible (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). 

무엇 우리 여기서 누락 된거야?

public interface IAnimal //IInterfaceA 
public interface ICat : IAnimal //IInterfaceB 
public interface IDog : IAnimal //another IInterfaceB 

IAnimal someAnimal = GetMeADog(); 
ICat catAndDogsDontMix = (ICat)someAnimal; //ouch! 

당신은 지금 이해하고 수행

public interface IInterfaceB : IInterfaceA 

이의이 이해하기 쉽게 이름을 넣어 보자 :

public interface IInterfaceB: IInterfaceB 

현실에 있습니다

+2

처음에는 컴파일 할 코드가 없습니다. –

+2

두 개의 간섭이 관련되어 있다는 표시는 없습니다. 이에 따라 [mcve] 지침과 [편집] 글을 읽으십시오. –

답변

0

나는 다음과 같은 라인을 같은데요 왜 "자식"인터페이스로 변환 할 수 없습니까?

런타임 오류가 표시되지만 이상한 점은 무엇입니까? 게시 한 코드는 컴파일되지 않아야합니다. 코드가 문제를 올바르게 reproving하고 있습니까? 어쩌면 aobject입니까?