How can dynamic be used as generic?

How to use dynamic as shared?

it

var x = something not strongly typed; callFunction<x>(); 

and this one

 dynamic x = something not strongly typed; callFunction<x>(); 

both create this error

 Error 1 The type or namespace name 'x' could not be found (are you missing a using directive or an assembly reference?) 

What can I do for x to make it legitimate enough for use in <x> ?

+7
source share
6 answers

You can use type inference to sort the trampoline by call:

 dynamic x = something not strongly typed; CallFunctionWithInference(x); ... static void CallFunctionWithInference<T>(T ignored) { CallFunction<T>(); } static void CallFunction<T>() { // This is the method we really wanted to call } 

This will determine the type argument at runtime based on the runtime type of the value x , using the same type inference type that it would use if x had it as its compile time type. The parameter is present only for performing type inference operations.

Please note that unlike Darin, I believe that this is a useful method - in the same situations when pre-dynamics you end up calling a general method with reflection. You can do this part of the dynamic code, but keep the rest of the code (from the generic type down) by type. It allows you to make one step dynamic - only one bit where you do not know the type.

+11
source

It's hard to say exactly what you are trying to do. But if you want to call a generic method with a type parameter that matches an object, you cannot do it directly. But you can write another method that takes your object as a parameter, let dynamic infer the type, and then call the method you want:

 void HelperMethod<T>(T obj) { CallFunction<T>(); } … dynamic x = …; HelperMethod(x); 
+4
source

You can not. The whole set of generics is safety at compile time, which means that they must be known at compile time. And the whole point of the dynamics is that you do not need to know the exact type at compile time and use dispatch at run time => this is the exact opposite of generics. Therefore, do not waste your time => after you get a dynamic / reflex path that you can forget about generics and security during compilation. You will have to go this way to the end.

So, to answer your question:

What can I do for x to make it legitimate enough to use?

The only thing you can do is use the type that is known at compile time, otherwise you cannot use generics.

+2
source

You get this error because x not a type. You need to specify the type as a type parameter.

In fact, you can use dynamic as a type parameter if you use it correctly:

 var dict = new Dictionary<string, dynamic>(); dict.Add("Item1", 123); dict.Add("Item2", "Blah"); 

This compiles and runs just fine.

+1
source

The quickest way to make this work is to make your anonymous type real.

So instead

 var x = new { Value = "somevalue", Text = "sometext" }; 

You need to do

 class MyClass { string Text, Value; } .... var x = new MyClass() { Value = "somevalue", Text = "sometext" }; //this should work now callFunction<MyClass>(x); 
0
source

You should be able to call a function like this

 callFunction<dynamic>(); 

If your function is defined as

 public void callFunction<T>(T arg) { ... } 

You can just call him

 callFunction(x); 

C # can invoke type type parameters in many situations.

0
source

All Articles