使用它作为参数“T”的引用I型有一个类:Constants.csASP.NET C#错误:该类型必须是为了在通用类型或方法
代码:
namespace DataAccess.Utilities
{
public class Constants
{
public enum ReturnCode
{
Fail = -1,
Success = 0,
Warning = 1
}
}
}
这是我directcast类
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 VB的DirectCast,但让我尝试搜索DirectCast没有在C#中存在,我得到关于如何创建DirectCast具有相同的功能,vb的一些代码。
我不明白你问。错误消息很清晰,是因为您正在尝试使用需要引用类型的值类型。你的问题是什么_?为什么你有'where T:class'约束,如果你想用类以外的方法来使用该方法? –
'在哪里T:class'但是'ReturnCode'是**枚举不是类** –
我只是想将这段代码从vb转换为c#........昏暗的代码作为Constants.ReturnCode = DirectCast(整数。解析(db.GetParameterValue(命令,“RESULTCODE”)。ToString.Trim),Constants.ReturnCode) –