How to get class name?

B

Brett

I'd like to get the name of a class I reference via an interface.

public Class1(Imyinterface _Classnamestoredhere)
{
string ClassName = _Classnamestoredhere.GetType().FullName.ToString();

This is given in the watches window:
_Classnamestoredhere.GetType().Name.ToString() error:
'_Classnamestoredhere.GetType' does not exist

Intellisense gives a GetType().

ClassName is out of scope because it is never assigned. How to I get the
class name of the value in Classnamestoredhere?

Thanks,
Brett
 
S

Scott Maskiel

Hi Brett,

Cast your interface to "object" first, i.e.

((object) _Classnamestoredhere).GetType().Name

Regards,
Scott
 
J

Jon Skeet [C# MVP]

Brett said:
I'd like to get the name of a class I reference via an interface.

public Class1(Imyinterface _Classnamestoredhere)
{
string ClassName = _Classnamestoredhere.GetType().FullName.ToString();

This is given in the watches window:
_Classnamestoredhere.GetType().Name.ToString() error:
'_Classnamestoredhere.GetType' does not exist

Intellisense gives a GetType().

ClassName is out of scope because it is never assigned. How to I get the
class name of the value in Classnamestoredhere?

That looks okay to me.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
B

Brett

Thanks. That works but I can't assign it to a variable of type object. I
get "out of scope" on that var. Why is that?

Brett
 
B

Brett

Jon Skeet said:
That looks okay to me.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Thanks Jon. Although Scott has posted a solution to my original problem, I
will make use of your suggestion. I particularly like the Pastebin service.

While we're on the subject, what is considered best in this group - top or
bottom post and why?

Brett
 
J

Jon Skeet [C# MVP]

Brett said:
Thanks Jon. Although Scott has posted a solution to my original problem, I
will make use of your suggestion. I particularly like the Pastebin service.

Right - although in most cases, it's reasonable to include it in the
post - that way it'll go into group archives etc permanently.
While we're on the subject, what is considered best in this group - top or
bottom post and why?

Personally, I vastly prefer bottom-posting, and I think it's probably
more common than top-posting here, but few people get riled up about it
either way.
 

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