gpt4 book ai didi

c# - C# 中的泛型 - 如何创建带有参数的变量类型的实例?

转载 作者:IT王子 更新时间:2023-10-29 03:40:48 28 4
gpt4 key购买 nike

<分区>

我有一个泛型类,我想在其中实例化一个具有泛型类型的对象。我想为该类型的构造函数使用一个参数。

我的代码:

public class GenericClass<T> where T : Some_Base_Class, new()
{
public static T SomeFunction(string s)
{
if (String.IsNullOrEmpty(s))
return new T(some_param);
}
}

我得到一个错误

new T(some_param)

'T': cannot provide arguments when creating an instance of a variable type

我该怎么做?

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com