[英]How to Pass class name Dynamically to List at Run Time C#
I need to pass the class name as Dynamically to List in Function but I am getting the error as below.. 我需要将类名称作为动态传递给Function中的List,但是出现以下错误。
public void updateList <T>( T clazz, string classData)
{
if (string.IsNullOrEmpty(json_response) == false)
{
List<clazz> list_of_measurements_Original = JsonConvert.DeserializeObject<List<clazz>>(json_response);
} // Getting error for ClassType in List
}
error CS0246: The type or namespace name 'clazz' could not be found (are you missing a using directive or an assembly reference?)
错误CS0246:找不到类型或名称空间名称“ clazz”(您是否缺少using指令或程序集引用?)
I have a number of classes and I want to update dynamically by the same Generic function by passing the Class name or by enum. 我有许多类,我想通过传递类名或枚举,通过相同的泛型函数动态更新。 I have also used the Dynamic Keyword as a function parameter but no result.
我还使用了动态关键字作为函数参数,但没有结果。 Please help me out.
请帮帮我。
'clazz' is a parameter while the compiler searches for a type. “ clazz”是编译器搜索类型时的参数。 You don't need to pass the type as a parameter, this is what the generics are for.
您不需要将类型作为参数传递,这就是泛型的用途。 Try this:
尝试这个:
public void updateList <T>(string classData)
{
List<T> objlist = new List<T>();
}
Change 更改
List<clazz> objlist = new List<clazz>();
To 至
List<T> objlist = new List<T>();
Here T
is a type parameter which is a placeholder for a specific type. 这里
T
是类型参数,它是特定类型的占位符。 The compiler changes the T
to whatever type you pass. 编译器将
T
更改为您传递的任何类型。 You can't pass a variable (clazz) as a type argument of a generic class. 您不能将变量(clazz)作为泛型类的类型参数传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.