App.config and F# Interactive not working

前端 未结 6 1299
陌清茗
陌清茗 2020-12-17 10:53

As I\'m polishing my little pet project, I\'m trying to store all the constant strings in my app.config file (Keys, XpathExpressions etc). When I run the compiled exe this w

6条回答
  •  一整个雨季
    2020-12-17 11:37

    Setting APP_CONFIG_FILE does the job "if it's early enough". It doesn't appear to be able to do it early enough in the .fsx file. So it needs a reset, as per this post: Change default app.config at runtime

    In F#, it looks like this:

    open System
    open System.Configuration
    open System.IO
    open System.Reflection
    
    let appConfigPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "App.config")
    AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", appConfigPath)
    typeof
        .GetField("s_initState", BindingFlags.NonPublic ||| BindingFlags.Static).SetValue(null, 0)
    typeof
        .GetField("s_configSystem", BindingFlags.NonPublic ||| BindingFlags.Static).SetValue(null, null)
    (typeof.Assembly.GetTypes()
        |> Array.find (fun x -> x.FullName = "System.Configuration.ClientConfigPaths"))
        .GetField("s_current", BindingFlags.NonPublic ||| BindingFlags.Static).SetValue(null, null);;
    

提交回复
热议问题