ASP.NET C# Error: The Type must be a reference type in order to use it as parameter 'T' in the generic type or method
ASP.NET C# Error: The Type must be a reference type in order to use it as parameter 'T' in the generic type or method
我有一个 class: Constants.cs
代码:
namespace DataAccess.Utilities
{
public class Constants
{
public enum ReturnCode
{
Fail = -1,
Success = 0,
Warning = 1
}
}
}
这是我的直播代码class
public static T DirectCast<T>(object o) where T : class
{
T value = o as T;
if (value == null && o != null)
{
throw new InvalidCastException();
}
return value;
}
这是我的错误代码
code = DirectCast<Constants.ReturnCode>(int.Parse(db.GetParameterValue(command, "RESULTCODE").ToString().Trim()));
错误信息:
Error 2 The type 'DataAccess.Utilities.Constants.ReturnCode' must be a reference type in order to use it as parameter 'T' in the generic type or method 'DataAccess.DataManager.QueueingManager.DirectCast(object)'
在我使用 .net 中的 DirectCast 之前 vb,但是 C# 中不存在 DirectCast,所以我尝试搜索并获得了一些关于如何创建与 vb 具有相同功能的 DirectCast 的代码.
code = DirectCast<Constants.ReturnCode>(int.Parse(db.GetParameterValue(command, "RESULTCODE").ToString().Trim()));
应改为:
code = DirectCast<Constants>(int.Parse(db.GetParameterValue(command, "RESULTCODE").ToString().Trim()));
或者,这样做:参见工作示例:
class Program
{
static void Main(string[] args)
{
var fail = Constants.DirectCast<Constants.ReturnCode>(-1);
var success = Constants.DirectCast<Constants.ReturnCode>(0);
var warning = Constants.DirectCast<Constants.ReturnCode>(1);
Console.WriteLine(fail);
Console.WriteLine(success);
Console.WriteLine(warning);
Console.ReadLine();
}
}
public class Constants
{
public enum ReturnCode
{
Fail = -1,
Success = 0,
Warning = 1
}
public static T DirectCast<T>(object o)
{
T value = (T)o;
if (value == null && o != null)
{
throw new InvalidCastException();
}
return value;
}
}
在.NET中,有值类型和引用类型。例如,类 是引用类型,它们就像 C++ 中的指针。
int 是值类型,枚举也是。
在泛型中,只有引用类型可以用作类型参数。
您可以删除方法的通用性,但您将无法知道 return 类型。此外,"as" 不适用于枚举,因为它是一种值类型。
我不明白你为什么要转换一个枚举,你可以解析它:
Enum.Parse(typeof(Constants.ReturnCode), db.GetParameterValue(command, "RESULTCODE").ToString().Trim()));
感谢您的评论。我的代码终于可以用了。
public class Constants
{
public enum ReturnCode
{
Fail = -1,
Success = 0,
Warning = 1
}
}
public static T DirectCast<T>(object o)
{
T value= (T)o;
if (value== null && o != null)
{
throw new InvalidCastException();
}
return value;
}
code = Constants.DirectCast<Constants.ReturnCode>(int.Parse(db.GetParameterValue(command, "RESULTCODE").ToString().Trim()));
我有一个 class: Constants.cs
代码:
namespace DataAccess.Utilities
{
public class Constants
{
public enum ReturnCode
{
Fail = -1,
Success = 0,
Warning = 1
}
}
}
这是我的直播代码class
public static T DirectCast<T>(object o) where T : class
{
T value = o as T;
if (value == null && o != null)
{
throw new InvalidCastException();
}
return value;
}
这是我的错误代码
code = DirectCast<Constants.ReturnCode>(int.Parse(db.GetParameterValue(command, "RESULTCODE").ToString().Trim()));
错误信息:
Error 2 The type 'DataAccess.Utilities.Constants.ReturnCode' must be a reference type in order to use it as parameter 'T' in the generic type or method 'DataAccess.DataManager.QueueingManager.DirectCast(object)'
在我使用 .net 中的 DirectCast 之前 vb,但是 C# 中不存在 DirectCast,所以我尝试搜索并获得了一些关于如何创建与 vb 具有相同功能的 DirectCast 的代码.
code = DirectCast<Constants.ReturnCode>(int.Parse(db.GetParameterValue(command, "RESULTCODE").ToString().Trim()));
应改为:
code = DirectCast<Constants>(int.Parse(db.GetParameterValue(command, "RESULTCODE").ToString().Trim()));
或者,这样做:参见工作示例:
class Program
{
static void Main(string[] args)
{
var fail = Constants.DirectCast<Constants.ReturnCode>(-1);
var success = Constants.DirectCast<Constants.ReturnCode>(0);
var warning = Constants.DirectCast<Constants.ReturnCode>(1);
Console.WriteLine(fail);
Console.WriteLine(success);
Console.WriteLine(warning);
Console.ReadLine();
}
}
public class Constants
{
public enum ReturnCode
{
Fail = -1,
Success = 0,
Warning = 1
}
public static T DirectCast<T>(object o)
{
T value = (T)o;
if (value == null && o != null)
{
throw new InvalidCastException();
}
return value;
}
}
在.NET中,有值类型和引用类型。例如,类 是引用类型,它们就像 C++ 中的指针。 int 是值类型,枚举也是。 在泛型中,只有引用类型可以用作类型参数。
您可以删除方法的通用性,但您将无法知道 return 类型。此外,"as" 不适用于枚举,因为它是一种值类型。
我不明白你为什么要转换一个枚举,你可以解析它:
Enum.Parse(typeof(Constants.ReturnCode), db.GetParameterValue(command, "RESULTCODE").ToString().Trim()));
感谢您的评论。我的代码终于可以用了。
public class Constants
{
public enum ReturnCode
{
Fail = -1,
Success = 0,
Warning = 1
}
}
public static T DirectCast<T>(object o)
{
T value= (T)o;
if (value== null && o != null)
{
throw new InvalidCastException();
}
return value;
}
code = Constants.DirectCast<Constants.ReturnCode>(int.Parse(db.GetParameterValue(command, "RESULTCODE").ToString().Trim()));