abstract classes and System.Type

M

Michael Soza

I have a little confusion about abstract classes..it supposed that this
classes can't be instantiated, then when I read the help about the class
System.Type says that we can use typeof or the GetType() method to get and
object Type that represent de type of the argument....so why? can or can't
be instantiated....
Playing with the powershell y figure out that in reality the object obtained
are from the type System.RuntimeType....so apparently the rules aren't
broken but why the Intellisense tellme lies, jejeje is confusing when hover
the cursor over the methods and tellme that the return type is System.Type.
 
J

Jeff Johnson

I have a little confusion about abstract classes..it supposed that this
classes can't be instantiated, then when I read the help about the class
System.Type says that we can use typeof or the GetType() method to get and
object Type that represent de type of the argument....so why? can or can't
be instantiated....

A Type object that represents a class is not the same as an instance of a
class.
 
F

Family Tree Mike

can be a little more expressive?
my doubt is Why in some place like Intellisense from VS appear that
GetType and typeof return a System.Type object and that class is
abstract so is theoretically impossible to have an instance of that type.
I don't understand what you mean with "a Type object that represents a
class is not the same as an instance of a class"..because basically a
object is an instantiation.

GetType and TypeOf both return an instance of System.Type. If you have
an abstract class called MyAbstractClass, then this creates an instance
of the System.Type class which describes the MyAbstract class. This is
not an instance of a MyAbastract object. To do that, you would need to
use "x = new MyAbstract();", which as you said is not valid.
 
M

Michael Soza

can be a little more expressive?
my doubt is Why in some place like Intellisense from VS appear that GetType
and typeof return a System.Type object and that class is abstract so is
theoretically impossible to have an instance of that type.
I don't understand what you mean with "a Type object that represents a class
is not the same as an instance of a class"..because basically a object is an
instantiation.
 
F

Family Tree Mike

The problem continues..........is not the class from where I apply
typeof or gettype() the important, the thing is that
System.Type is abstract!!!

Ah, now I see your point.

Run this code:

System.Type t = typeof(System.Random);
Console.WriteLine(t.GetType());
Console.ReadKey();

You will see the actual type of t is System.RuntimeType. You are
actually getting a subclass of System.Type.
 
A

Arne Vajhøj

Ah, now I see your point.

Run this code:

System.Type t = typeof(System.Random);
Console.WriteLine(t.GetType());
Console.ReadKey();

You will see the actual type of t is System.RuntimeType. You are
actually getting a subclass of System.Type.

Which is documented.

http://msdn.microsoft.com/en-us/library/system.type.aspx

says:

<quote>
Type is an abstract base class that allows multiple implementations. The
system will always provide the derived class RuntimeType. In reflection,
all classes beginning with the word Runtime are created only once per
object in the system and support comparison operations.
</quote>

Arne
 
M

Michael Soza

The problem continues..........is not the class from where I apply typeof or
gettype() the important, the thing is that
System.Type is abstract!!!
 

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