如何根据接口得到接口的实现类?
例如我有一个接口IMyInterface,其实现类为TMyInterface,现在有一个变量
I: IMyInterface;
T: TMyInterface;
如何根据I给T赋值 ?
I := CreateComObject(CLSID_MYINTERFACE);
T := TMyInterface(I); //// Error!!!!
该如何做呢?
有几个方法,一个是在Interface里面,增加一个方法,就是取Object本身;或者实现IInterfaceComponentReference 接口:
IInterfaceComponentReference = interface
['{E28B1858-EC86-4559-8FCD-6B4F824151ED}']
function GetComponent: TComponent;
end;
另外一个方法:
Var
eResource: IResource;
eResourceObj: TResource;
eEntry: PInterfaceEntry;
eAutoObjFactory: TAutoObjectFactory;
Begin
eResource:= CreateComObject(CLASS_Resource) as IResource;
eAutoObjFactory:= TAutoObjectFactory(ComClassManager.GetFactoryFromClassID(CLASS_Resource));
eEntry:= eAutoObjFactory.DispIntfEntry;
eResourceObj:= TResource(Integer(eResource)-eEntry.IOffset);
eResourceObj.Path:= '这里设置不同的值';
End;