Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cast with a Type variable

The below code won't work, I wanted to know how I can dynamically cast an instance to a type determined at runtime?

Convert.ChangeType() returns an Object that still needs to be cast. So does all attempts to Invoke() a GetConstructor() or Activator.CreateInstance(), see below. At some point I need to explicitly cast in code, I'm hoping to avoid it or push it out as far as possible.

Type type = Type.GetType ("RandomChildClass");
Object obj = Activator.CreateInstance (type, new Object[]{ "argument" });
var instance = (type)obj;

I know I can create a method to accept < T >, but I'd still have the same problem of not knowing how to call it with a dynamic random type Casting a variable using a Type variable

like image 274
user2994682 Avatar asked Jan 25 '14 02:01

user2994682


People also ask

Can you cast a variable?

We can cast any variable to Boolean using (bool) or (boolean) keyword. If we will convert any variable data type to Boolean then if the variable has value(and value is not 0) then it will return true, otherwise false.

How do you use type as variables?

For example: Type intType = typeof(Int32); object value1 = 1000.1; // Variable value2 is now an int with a value of 1000, the compiler // knows the exact type, it is safe to use and you will have autocomplete int value2 = Convert.

Which is used for casting of object to a type or a class?

Type Casting is a feature in Java using which the form or type of a variable or object is cast into some other kind of Object, and the process of conversion from one type to another is called Type Casting.


2 Answers

It is not possible to use a Type value to determine the type of an expression. (Generics type parameters are different than values as they are codified into the type-system.)

The value of the variable is from the run-time code execution, while the expression type is a compile-time construct. Needless to say, the compilation occurs before the code ever runs so using a variable for a cast is impossible.

Reflection (albiet unwieldy) or dynamic (which is basically easier-to-use-reflection) allow invoking arbitrary methods or accessing properties/fields against a generic object-typed expression - this is occasionally refered to as "late binding". However, the type of the expression upon which operations is invoked is still object.

Interfaces can be used to unify disparate class implementations for proper static typing. The newly created object can then cast to the applicable interface(s) are required. Just like with other expressions, the type is a compile-time construct (and as such the interface must be directly specified), but the code is now free from a particular class.

If creating a system such that these "dynamic classes" are to be used directly in statically typed (C#) code, and the interfaces can be guaranteed or are constrained to a small set, then using interfaces is likely the cleanest approach: e.g. var myAction = (IMyAction)obj. Otherwise, fall back to dynamic access - direct or behind a facade.

like image 109
user2864740 Avatar answered Sep 29 '22 05:09

user2864740


Define your variable as dynamic and it should work, I mean still you can't cast it but you can access your methods:

dynamic obj = Activator.CreateInstance (type, new Object[]{ "argument" });

For example:

enter image description here

like image 34
Selman Genç Avatar answered Sep 29 '22 04:09

Selman Genç