在Visual Studio中,Intellisense显示List.empty和List.Empty.有什么不同?他们为什么都在那里?
我负责F#WPF应用程序。当发生未处理的异常时,它将通过电子邮件发送给我。大多数时候,我能够从错误消息和堆栈跟踪中识别异常的来源。但是,我偶尔会收到类似以下的消息,该消息在堆栈跟踪中不包含我的任何代码。
Summary:
Object reference not set to an instance of an object.
--------------
Details:
System.NullReferenceException:
Object reference not set to an instance of an object.
at Microsoft.FSharp.Control.CancellationTokenOps.Start@1234-1.Invoke(Exception e)
at <StartupCode$FSharp-Core>.$Control.loop@435-40(Trampoline this, FSharpFunc`2 action)
at Microsoft.FSharp.Control.Trampoline.ExecuteAction(FSharpFunc`2 firstAction)
at Microsoft.FSharp.Control.TrampolineHolder.Protect(FSharpFunc`2 firstAction)
at <StartupCode$FSharp-Core>.$Control.-ctor@520-1.Invoke(Object state)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
Run Code Online (Sandbox Code Playgroud)
此堆栈跟踪中有哪些线索可以帮助我识别此异常的来源?
例如,我假设原始异常正在async计算中抛出。(我的大部分异步代码都是基于F#异步块的。)我NullReferenceException可能发生在async块中的任何地方吗?我注意到该异常发生在Start() …
是否有以下简单的解决方法?还是这是VSCode和/或特定于语言的扩展中的错误?
我创建了两个项目和一个类似的解决方案:
dotnet new library -lang F# -o .\ClassLibrary
dotnet new console -lang C# -o .\MainProgram
dotnet new sln
dotnet sln add .\ClassLibrary\ClassLibrary.fsproj
dotnet sln add .\MainProgram\MainProgram.csproj
Run Code Online (Sandbox Code Playgroud)
我从MainProgram中添加了对ClassLibrary的项目引用。
dotnet add reference ..\ClassLibrary\ClassLibrary.fsproj
Run Code Online (Sandbox Code Playgroud)
我更新了Program.cs以调用ClassLibrary。
static void Main(string[] args)
{
ClassLibrary.Say.hello("world.");
}
Run Code Online (Sandbox Code Playgroud)
我可以成功还原,构建和运行该程序。
dotnet restore
dotnet run -p .\MainProgram\MainProgram.csproj
Run Code Online (Sandbox Code Playgroud)
问题是当我在Visual Studio Code中打开解决方案文件夹时。
我也尝试过单独打开项目文件夹。
更新资料
我有VS Code版本1.15.1,安装了五个扩展。
假设我有一个业务对象(没有AllowNullLiteralAttribute).
type Person(name: string) =
member val Name = name
override x.ToString() = name
Run Code Online (Sandbox Code Playgroud)
和视图模型,可选择设置所选人员.
type MainWindowModel() =
let mutable selectedPerson: Person option = None
:
member val People = ObservableCollection<Person>()
member x.SelectedPerson
with get() = selectedPerson
and set(v) =
if selectedPerson <> v then
selectedPerson <- v
x.RaisePropertyChanged("SelectedPerson")
Run Code Online (Sandbox Code Playgroud)
将WPF控件的SelectedItem属性绑定到F#选项属性(不使用AllowNullLiteralAttribute)的最佳方法是什么?
如果我这样做......
<StackPanel>
<ListBox ItemsSource="{Binding People}"
SelectedItem="{Binding SelectedPerson}"
DisplayMemberPath="Name" />
<TextBlock Text="{Binding SelectedPerson}" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
...导致错误,无法将'George'从类型'Person'转换为'Microsoft.FSharp.Core.FSharpOption`1 [Person]'
当我运行ClickOnce mage.exe实用程序,使用-MinVersion和-Install选项创建新的部署清单时,我收到错误.
> mage.exe -New Deployment -Install true -Version 1.0.0.0 -MinVersion 1.0.0.0
The minimum version specified is not valid.
Run Code Online (Sandbox Code Playgroud)
如何避免此错误消息?
当我在 Visual Studio Code 中打开一个 Fake 构建脚本时,如下所示:
> dotnet new -i fake-template
> dotnet new fake
> dotnet tool update fake-cli
> code build.fsx
Run Code Online (Sandbox Code Playgroud)
我在编辑器中看到此错误消息,并且没有定义任何假命名空间、模块或类型:
Package manager key 'paket' was not registered in
c:\Users\wallace.kelly\.vscode\extensions\ionide.ionide-fsharp-5.4.0\bin\
Currently registered: nuget
Run Code Online (Sandbox Code Playgroud)
我该如何纠正这个错误?
跑步dotnet fake build效果很好。该错误仅出现在编辑器中。
我有"FSharp.dotNetRoot": "C:\\Program Files\\dotnet\\sdk",我的设置文件。该文件夹包括文件夹 2.1.701 2.2.401 3.1.407 5.0.103 和 5.0.104。
当我将其包含NLog.LogManager.GetCurrentClassLogger()在 F# 模块的顶部时,记录器的名称类似于<StartupCode$NLogTest>.$Program. 我本来希望有类似的事情NLogTest.TestModule。
如何为我的 F# 模块获得更好的记录器名称?
这是一个完整的例子:
Program.fs
namespace NLogTest
type TestClass() =
let logger = NLog.LogManager.GetCurrentClassLogger()
member _.Run() =
logger.Info("Inside my class")
module TestModule =
let logger = NLog.LogManager.GetCurrentClassLogger()
let Run() =
logger.Info("Inside my module")
module Program =
[<EntryPoint>]
let Main _ =
TestClass().Run()
TestModule.Run()
0
Run Code Online (Sandbox Code Playgroud)
NLogTest.fsproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Include="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="4.7.13" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
NLog.config
<?xml version="1.0" …Run Code Online (Sandbox Code Playgroud) 我有一个用VS Code打开的控制台应用程序。当我按Ctrl-F5时,程序的输出将与其他文本一起显示在DEBUG CONSOLE窗口中。
如何获取Visual Studio代码以在新的控制台窗口中启动程序?
运行时,dotnet build我会多次收到此警告:
C:\Program Files\dotnet\sdk\1.0.4\Microsoft.Common.CrossTargeting.targets(145,3):
warning MSB4011: "*.fsproj.proj-info.targets" cannot be imported again.
It was already imported at "Microsoft.Common.targets (127,3)".
This is most likely a build authoring error.
This subsequent import will be ignored.
[*.fsproj]
Run Code Online (Sandbox Code Playgroud)
如何防止显示这些警告?
在F#中,我们可以方便地使用failwith和failwithf抛出异常实例.因此,您可能偶尔需要使用"守卫时"来区分不同的异常情况.
这是一个说明性的例子:
try
let isWednesday = DateTime.Now.DayOfWeek = DayOfWeek.Wednesday
if isWednesday then failwith "Do not run this on Wednesdays!"
with
| :? DivideByZeroException -> printfn "You divided by zero."
| :? Exception as ex when ex.Message.Contains("Wednesdays") -> printfn "I said, %s" ex.Message
| ex -> printfn "%s" ex.Message
Run Code Online (Sandbox Code Playgroud)
但是,上面的代码会产生两个警告:
Program.fs(14,7): warning FS0067: This type test or downcast will always hold
Program.fs(14,7): warning FS0067: This type test or downcast will always hold
Run Code Online (Sandbox Code Playgroud)
我该如何避免这种警告?
f# ×8
.net-core ×2
asynchronous ×1
c# ×1
clickonce ×1
data-binding ×1
dotnet-cli ×1
f#-fake ×1
ionide ×1
nlog ×1
wpf ×1