Variables and types of type "Type" 
Author Message
 Variables and types of type "Type"

Hi
I would like to declare a varaible to store the type of a class. Then I want
to create an object of the type in that variable.
It works this way:

public class BaseItem : object
{...}
------
Type typeItem;
typeItem = typeof (BaseItem);
BaseItem objItem = (BaseItem) Activator.CreateInstance(typeItem);

My next goal is: In the example above, I would like the variable typeItem to
only accept the class type of BaseItem and class types of classes that have
been derived from class BaseItem.
How would I have to declare the variable?
Is it possible to declare a new (value) type for a class type? With
something like "public BaseItemType : typeof(BaseItem)"

Regards, Christian



Mon, 29 Dec 2003 19:21:15 GMT  
 Variables and types of type "Type"
Hi Christian,

Quote:
> My next goal is: In the example above, I would like the variable typeItem
to
> only accept the class type of BaseItem and class types of classes that
have
> been derived from class BaseItem.
> How would I have to declare the variable?
> Is it possible to declare a new (value) type for a class type? With
> something like "public BaseItemType : typeof(BaseItem)"

No, it isn't. You'll have to do the check yourself by navigating the
inheritance hierarchy via Type's accesors. Not pretty, but shouldn't be too
hard, either.

--
Tomas Restrepo



Mon, 29 Dec 2003 19:42:15 GMT  
 Variables and types of type "Type"
Thanks for your help!

Too bad that C# does not offer the possibility to declare types for types
over the build in type Type.

Christian



Mon, 29 Dec 2003 20:48:51 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. "Windows data types" vs "c data types"

2. Using type "string" as type "VerticalAlign"

3. Type Casting between Base and Derived Type (Serialization of objects with Type members)

4. "hooking" into a specific file type

5. Inconsistent type declaration: "iconv_t"

6. "*" Operator Result Type

7. "dereferencing pointer to incomplete type"

8. "Faking" null in value types

9. Function without a "return type"?

10. Parser: "C" types 2 ascii

11. getchar()"type mismatch"

12. Somewhat of a "Newbie" type question

 

 
Powered by phpBB® Forum Software