我如何将动态类型用作泛型?
这个
var x = something not strongly typed;
callFunction<x>();
并且这个
dynamic x = something not strongly typed;
callFunction<x>();
两者都会产生这个错误
Error 1 The type or namespace name 'x'
could not be found (are you missing a using directive or an assembly reference?)
我该怎么做才能使x
合法并可以在<x>
中使用?
System.Type
那样的类型,而是直接像string
或int
那样的类型。变量不能用作类型参数。 - M.Babcockstring x = "X";
。你不会调用CallFunction<x>()
或者CallFunction<typeof(string)>()
; 你会调用CallFunction<string>()
。这只有当CallFunction有一个字符串参数或返回值时才有意义;更合理的调用方式可能是string x = CallFunction<string>();
或者CallFunction<string>(x)
。你能给出一个你想调用的函数的例子吗?你想要实现什么目标? - phoog