【发布时间】:2017-04-21 17:37:16
【问题描述】:
我有一个类:Constants.cs
代码:
namespace DataAccess.Utilities
{
public class Constants
{
public enum ReturnCode
{
Fail = -1,
Success = 0,
Warning = 1
}
}
}
这是我的直接广播类代码
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()));
错误信息:
错误 2 类型“DataAccess.Utilities.Constants.ReturnCode”必须是引用类型才能在泛型类型或方法“DataAccess.DataManager.QueueingManager.DirectCast(object)”中用作参数“T”
在我使用 .net vb 的 DirectCast 之前,但 DirectCast 在 c# 中不存在,所以我尝试搜索并获得了一些关于如何创建与 vb 具有相同功能的 DirectCast 的代码。
【问题讨论】:
-
我不明白你在问什么。错误消息非常清楚,是因为您尝试使用需要引用类型的值类型。您的问题是什么?如果您想将该方法用于类以外的事物,为什么要使用
where T : class约束? -
where T : class但ReturnCode是枚举而不是类 -
我只想将此代码从 vb 转换为 c#........ 暗码 As Constants.ReturnCode = DirectCast(Integer.Parse(db.GetParameterValue(Command, "RESULTCODE") .ToString.Trim), 常量.ReturnCode)
标签: c# asp.net parameters reference directcast