How to determine the type of interface implementation object

I am trying to write unit test for a simple factory class that creates one of several possible implementation objects and returns it as an interface reference.

DUnit has a built-in procedure CheckIs(AObject: TObject; AClass: TClass; msg: string)that, based on its name and the parameters that it accepts, should test fail if the class type of the object does not match the expected one. The only problem is that it requires an object reference, not an interface reference.

So, I am trying to use CheckTrueand perform the comparison in the body of the test, but I am not so familiar with the support of validation like Delphi, since I am from C #.

I know that the operator is isout of the question, since it only works with object references.

CheckTrue(LMyInterfaceReference {comparison here} TMyClass);

Any suggestions?

By the way, I use Delphi 2009, so I do not have access to the new RTTI support added in 2010.

+3
source share
3 answers

I wonder why you SHOULD experience this ... maybe you really don't need to.

But if knowledge of the underlying interface object is required, you have two options:

  • Add a method to the interface that returns the base object, only TObject, and implements it in each class, simply returning itself.
  • Hack a bit, for example, using the interface to the object .
+4
source

If you do not like hacks and do not want to switch to Delphi 2010+, you can use this interface:

IImplementingObjectInterface = interface
  function GetImplementingObject: TObject;
end;

, . , TInterfacedObject , , .

+4

( Embarcadero Delphi) .

.

, Hallvard Vassbotn 2004 .

Delphi 2010 is check as .

-

+1
source

Source: https://habr.com/ru/post/1796341/


All Articles