CodeGrue
CodeGrue

Reputation: 5933

Why can't I use a "Type" to create a new variable in C#?

int number = 5;

Type dynamicType = number.GetType();    // dynamic type is "int"

dynamicType x = (number as dynamicType);

Upvotes: 1

Views: 261

Answers (7)

kurige
kurige

Reputation: 3779

SomeObject myObject = new SomeObject();
Type t = myObject.GetType();
SomeObject myOtherObject = (SomeObject)Activator.CreateInstance(t);

The ability to do this goes all the way back to .Net v1.0. No need for fancy dynamic typing or anything like that.

Constructing objects with more complex constructors takes a bit more work, but it's the same idea.

Upvotes: 3

Sanjay Manohar
Sanjay Manohar

Reputation: 7026

So, AFAIK you can't use the Type to instantiate 'basic' data types such as int. You could use it to create objects though:

Object x=Activator.CreateInstance(dynamicType)

The issue is, if you want to call methods or access fields on a dynamic type, you have to use reflection. See the Type class documentation for instructions.

Upvotes: 0

Frater
Frater

Reputation: 710

You can, just not directly. You can use reflection. Basically you get the fully qualified name of the type (from the Type object) and then call CreateInstance method from an assembly object that contains that type. It should be quite simple to get the assembly reference of the current instance...

I think the Assembly.GetExecutingAssembly() method will do it.

I've written a little about this quite some time ago in a post at: http://frater.wordpress.com/2007/06/24/instantiating-classes-through-reflection-using-c-dynamic-object-creation/

That might help you out some more, though the focus was also on compiling of c# code from within a c# program, to allow for the use of C# as a scripting language, so some of the details might not be completely relevant.

Hope that helps!

Upvotes: 1

this. __curious_geek
this. __curious_geek

Reputation: 43217

dynamicType is an object of type "Type" that holds the Type meta-data information of type "int" hence dynamicType is and instance or object and not a qualified type so you cannot perform instantiation on that.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1504132

How would you expect the compiler to treat the variable x later in the code? It wouldn't know anything about it... so it couldn't resolve any method calls etc.

Basically what you're after is dynamic typing which is supported in C# 4 with the "dynamic" pseudo-type:

int number = 5;
dynamic d = number;
// Calls to d are resolved at execution time, so this compiles:
d.ThisWillThrowAtExecutionTime();

Upvotes: 9

JustLoren
JustLoren

Reputation: 3234

Type is an object that represents information about a type. It's not a designator for a variable saying it is of that type.

Upvotes: 5

SLaks
SLaks

Reputation: 888293

Because it wouldn't make any sense.

What would you do next?

The purpose of a cast is to assign the casted object to a variable of the type that you casted it to. In this case, you wouldn't be able to do that, since a field must have a static type at compile time.

What are you trying to do?

Upvotes: 0

Related Questions