今天開始了一項將控制臺應用程式遷移到 VS2022 和 C#10 的新任務,但遇到了一個我找不到解決方法的顯示停止程式,除了使用編譯器指令來抑制警告。我希望避免這種情況,因為這個應用程式有數百甚至數千個使用類似語法的 LINQ 查詢。以下是我遇到的基本情況。
public static class ResponseCodes
{
public static string MEI0066 => "ME-I0066";
public static string MEI0069 => "ME-I0069";
public static string MEI0070 => "ME-I0070";
}
public class DebugLog
{
public DebugLog()
{
this.DebugLogId = null;
this.LogType = string.Empty;
this.ResponseCode = string.Empty;
this.Explanation = string.Empty;
}
public DebugLog(int? p_id, string p_logType, string p_responseCode, string p_explaination)
{
this.DebugLogId = p_id;
this.LogType = p_logType;
this.ResponseCode = p_responseCode;
this.Explanation = p_explaination;
}
public int? LogId { get; set; }
public string ResponseCode { get; set; }
public string Explanation { get; set; }
public string LogType { get; set; }
}
public enum LogEntryTypes
{
BlackListedIP,
DNSQuerySuccess,
DNSQueryFailed,
WhiteListedIP
}
public class QueryLogs
{
public QueryLogs()
{
this.DebugLogQueue = new List<DebugLog>();
}
public List<DebugLog> DebugLogQueue {get;set;}
public DebugLog? Query()
{
DebugLog _debugLog = new DebugLog()
{
LogId = 0,
LogType = LogEntryTypes.DNSQuerySuccess.ToString(),
Explanation = "IP was valid",
ResponseCode = ResponseCodes.MEI0070
};
DebugLogQueue.Add(_debugLog);
_debugLog = new DebugLog(1, LogEntryTypes.BlackListedIP.ToString(), "This a test", ResponseCodes.MEI0066);
DebugLogQueue.Add(_debugLog);
_debugLog = new DebugLog(2, LogEntryTypes.DNSQueryFailed.ToString(), "This string is not null", ResponseCodes.MEI0069);
DebugLogQueue.Add(_debugLog);
// This LINQ query gives a CS8600 warning "Converting null literal or possible
// null value to non-nullable type.
DebugLog returnValue = DebugLogQueue
.FirstOrDefault(x =>
(x.ResponseCode == ResponseCodes.MEI0069)
&& (x.LogType == LogEntryTypes.DNSQueryFailed.ToString()));
// This form uses the null-coalescing operator but gives CS1662 warning
// "Cannot convert lambda expression to intended delegate type because some of
// the return types in the block are not implicitly convertible to to the
// delegate return type."
returnValue = DebugLogQueue
.FirstOrDefault(x => (x.ResponseCode ?? ResponseCodes.MEI0069));
return returnValue;
}
}
嘗試過的內容:
因為類 ResponseCodes 是靜態的,所以它的值永遠不會為空。
因為列舉 LogEntryTypes 在使用時會轉換為字串,所以它們永遠不會回傳空值。
正如其他帖子所建議的那樣,我嘗試了以下方法:
- 通過使用“靜態字串?MEI0066 =>“ME-I0066”;將靜態類 ResponseCodes 更改為具有可為空的值
- 將 DebugLog 類的屬性 getter/setter 更改為使用“public string?ResponseCode { get; set; }”
- 將 LogEntryTypes 更改為字串 const,例如 dNSQueryFailed = LogEntryTypes.DNSQueryFailed,但這與使用類 ResponseCodes 中的靜態值沒有什么不同。
- QueryLogs 類中的 Query 方法允許回傳可為空的 DebugLog,以便它與 .FirstOrDefault 查詢運算子一起使用,該運算子不適用于 DebugLog 屬性值。
- 將編譯器指令設定為:“#pragma warning disable CS8600 // 將空文字或可能的空值轉換為不可為空的型別。” 消除了警告并且代碼運行,但是對數百個查詢執行此操作肯定會在以后回來咬我。
因此,當發出并回傳屬性值時,CS8600 和 CS1662 警告必須來自 LINQ 端,但對于我來說,我一直無法想出正確的語法來撰寫查詢,以便警告消失。我什至不確定在哪里可以進一步了解這個問題。
那么我在這里遺漏了什么,或者這是 C#10 中 LINQ 查詢的非入門方法嗎?
uj5u.com熱心網友回復:
使用 net6 時出現的大多數問題是由 c# 的這個新的愚蠢的可空特性引起的。最糟糕的是,它默認進入新專案,大多數人花費大量時間試圖找出它突然停止作業或發出奇怪警告的原因。只需在您的 net 6 個專案檔案中進行這個小改動
<TargetFramework>net6.0</TargetFramework>
<!--<Nullable>enable</Nullable>-->
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/428903.html
上一篇:在C#中使用Linq更新現有串列
下一篇:使用LINQ對字串陣列進行分組
