c# – .NET Native优化会破坏应用程序
发布时间:2020-12-15 22:24:48 所属栏目:百科 来源:网络整理
导读:在其中一个应用程序中,我遇到了与代码优化相关的错误. 我试图在测试应用程序中重复此行为.该应用程序可在GitHub上获得:https://github.com/altk/NullableError 只有在使用.NET Native进行编译时才会发生错误,但不是在任少数PC上出现错误. 应用程序代码非常
在其中一个应用程序中,我遇到了与代码优化相关的错误.
我试图在测试应用程序中重复此行为.该应用程序可在GitHub上获得:https://github.com/altk/NullableError 只有在使用.NET Native进行编译时才会发生错误,但不是在任少数PC上出现错误. 应用程序代码非常简单.它必须打印到控制台: ————– ELSE ————– 但由于优化它打印: ————– ELSE ————– 该应用程序的所有代码: #define DEBUG using System; using System.Collections.Generic; using System.Diagnostics; namespace NullableError { sealed partial class App { private readonly Dictionary<String,String> _dictionary = new Dictionary<String,String>(); public App() { InitializeComponent(); Execute(); Exit(); } private Int64? NullableInt64 { get { var resultString = this[nameof(NullableInt64)]; return String.IsNullOrEmpty(resultString) ? default(Int64?) : Int64.Parse(resultString); } //-----FIX VARIANT------ //UNCOMMENT NEXT LINE TO FIX //[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoOptimization)] set { //-----FIX VARIANT------ //REPLACE WITH //this[nameof(NullableInt64)] = value != null ? value.Value.ToString() : null; this[nameof(NullableInt64)] = value?.ToString(); } } private Int64? AnotherNullableInt64 { get { var resultString = this[nameof(AnotherNullableInt64)]; return String.IsNullOrEmpty(resultString) ? default(Int64?) : Int64.Parse(resultString); } set { this[nameof(AnotherNullableInt64)] = value?.ToString(); } } private void Execute() { //-----FIX VARIANT------ //REPLACE WITH IF-ELSE WITH //NullableInt64 = (DateTimeOffset.Now - DateTimeOffset.MinValue).TotalMilliseconds < 0 ? (Int64?) 125 : null; if ((DateTimeOffset.Now - DateTimeOffset.MinValue).TotalMilliseconds < 0) { Debug.WriteLine("-------------- IF --------------"); NullableInt64 = 125; } else { Debug.WriteLine("-------------- ELSE --------------"); NullableInt64 = null; } //-----FIX VARIANT------ //REPLACE WITH //AnotherNullableInt64 = 0; AnotherNullableInt64 = null; Debug.WriteLine(String.Format("-------------- {0} --------------",this[nameof(NullableInt64)] == "0" ? "FAIL" : "SUCCESS")); } private String this[String key] { get { String result; return _dictionary.TryGetValue(key,out result) ? result : null; } set { _dictionary[key] = value; } } } } 解决方法
微软证实了这个错误.
https://connect.microsoft.com/VisualStudio/feedback/details/2164313/net-native-optimization-breaks-an-application# 它将在即将推出的VS 2015 Update 2中修复 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |