There is no need. In the case you described above, you got an
instance to the very interface you are attempting to cast too. But to
answer your question, yes! But it depends on what language and what
you are doing. In C++ you use the IUnknown::QuerryInterface(), in C#
you just directly cast with the interface alone:
namespace MyTest {
public interface ITest {
bool Print();
}
public class Test : ITest {
public bool Print() {
// do something
}
}
}
Test t = new Test();
ITest i = t;
i.Print(); // result is printed
....
but in JScript 5.5 and less, you never have access to an interface
directly:
// assuming you registered with string named
var t = new ActiveXObject('MyTest.Test'); // OK
var t2 = new ActiveXObject('MyTest.ITest'); // ERROR, ITEST is not in
REGISTRY
Does this answer your question?
- Rashad Rivera
Department of State/NCC
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Rashad Rivera <rashadrivera@NO_____SPAM__.hotmail.com> wrote: In this case you need to look at the work "explicit" and see that it is
telling you not to cast it with the "(ITest)" because there is no need. Try
this and it should work
object obj=Activator.CreateInstance("TypeName");
ITest objITest = obj; // Don't use a cast for like types
No it shouldn't. There's no way an implicit cast is going to exist from
object to a particular interface, is there?