Type casting object type to user defined class or interface

P

Programmer

I am making a project in which i have one interface ITest,
and a class which is implementing that interface.

I am making object of that class using

object obj=Activator.CreateInstance("TypeName");
ITest objITest =(ITest) obj;

It is giving error explicit typecasting not allowed

can any body help on this
 
J

Jon Skeet [C# MVP]

Programmer said:
I am making a project in which i have one interface ITest,
and a class which is implementing that interface.

I am making object of that class using

object obj=Activator.CreateInstance("TypeName");
ITest objITest =(ITest) obj;

It is giving error explicit typecasting not allowed

What is the *exact* error message, and when are you getting it? Do you
have more than one assembly involved? If so, you might be running into
the problem described at http://www.pobox.com/~skeet/csharp/plugin.html
 
R

Rashad Rivera

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

- Rashad Rivera
Department of State/NCC
 
J

Jon Skeet [C# MVP]

Rashad Rivera said:
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?
 
R

Rashad Rivera

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
 
J

Jon Skeet [C# MVP]

Rashad Rivera said:
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:

Yes, but you *do* need to directly cast.

My point was that the sample code you gave (in C#) was never going to
work, and your explanation was definitely wrong.

If you can give *any* code (which doesn't alias "object" to some other
type) where you can use your code:

object obj=Activator.CreateInstance("TypeName");
ITest objITest = obj; // Don't use a cast for like types

I'll be very surprised.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top