2017-04-21 72 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())); 
相關問題