2017-04-21 73 views
-3

使用它作为参数“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的一些代码。

+1

我不明白你问。错误消息很清晰,是因为您正在尝试使用需要引用类型的值类型。你的问题是什么_?为什么你有'where T:class'约束,如果你想用类以外的方法来使用该方法? –

+0

'在哪里T:class'但是'ReturnCode'是**枚举不是类** –

+0

我只是想将这段代码从vb转换为c#........昏暗的代码作为Constants.ReturnCode = DirectCast(整数。解析(db.GetParameterValue(命令,“RESULTCODE”)。ToString.Trim),Constants.ReturnCode) –

回答

0
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; 
    } 
} 
+0

不能隐式地将类型'DataAccess.Utilities.Constants'转换为'DataAccess.Utilities.Constants.ReturnCode' –

+0

谢谢...我使用code = Constants.DirectCast (int.Parse(db.GetParameterValue(command,“RESULTCODE”)。ToString()。Trim())); –

1

在.NET中,有值类型和引用类型。例如,类是引用类型,它们就像C++中的指针。 int是值类型,enums也是。 在泛型中,只有引用类型可以用作类型参数。

您可以删除方法的通用性,但无法知道返回类型。而且,“as”不能用于枚举,因为它是一种值类型。

我不明白你为什么要投一个枚举,你可以分析它:

Enum.Parse(typeof(Constants.ReturnCode), db.GetParameterValue(command, "RESULTCODE").ToString().Trim())); 
+0

感谢您的回答。 –

0

感谢您的意见。最后我的代码工作。

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())); 
相关问题