10

How can I use a dynamic as a generic?

This

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

and this

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

both produce 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 to x to make it legitimate enough to be used in <x>?

4

6 回答 6

19

You could use type inference to sort of trampoline the 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 execution time based on the execution-time type of the value of x, using the same kind of type inference it would use if x had that as its compile-time type. The parameter is only present to make type inference work.

Note that unlike Darin, I believe this is a useful technique - in exactly the same situations where pre-dynamic you'd end up calling the generic method with reflection. You can make this one part of the code dynamic, but keep the rest of the code (from the generic type on downwards) type-safe. It allows one step to be dynamic - just the single bit where you don't know the type.

于 2012-04-12T22:33:49.367 回答
4

It's hard to tell what exactly are you trying to do. But if you want to call a generic method with a type parameter that is the same as some object, you can't do that directly. But you can write another method that takes your object as a parameter, let the dynamic infer the type and then call the method you want:

void HelperMethod<T>(T obj)
{
    CallFunction<T>();
}

…

dynamic x = …;
HelperMethod(x);
于 2012-04-12T22:33:22.657 回答
2

You can't. The whole point of generics is compile-time safety which means that they must be known at compile-time. And the whole point of dynamics is that you don't need to know the exact type at compile time and use runtime dispatching => it's the absolutely exact opposite of generics. So don't waste your time => once you get the dynamic/reflection path you can forget about generics and compile-time safety. You will have to walk that path till the end.

So to answer your question:

What can I do to x to make it legitimate enough to be used in ?

The only thing you could do is to use a type that is known at compile-time, otherwise you cannot use generics.

于 2012-04-12T22:25:16.653 回答
1

You get that error because x is not a type. You need to specify a 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.

于 2012-04-12T22:27:51.730 回答
0

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

So instead of

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);
于 2012-04-12T22:31:47.747 回答
0

You should be able to call the function like this

callFunction<dynamic>();

If your function is defined as

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

You can simply call it with

callFunction(x);

C# is able to infer generic type parameters in many situations.

于 2012-04-12T22:46:28.640 回答