Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 我应该如何公开在f#中声明的全局字典,该字典将包含从不同HttpModules添加的项?_Asp.net_Multithreading_F# - Fatal编程技术网

Asp.net 我应该如何公开在f#中声明的全局字典,该字典将包含从不同HttpModules添加的项?

Asp.net 我应该如何公开在f#中声明的全局字典,该字典将包含从不同HttpModules添加的项?,asp.net,multithreading,f#,Asp.net,Multithreading,F#,我在下面的代码中声明了一个字典(格式化程序),它将在多个HttpModules中添加项。一旦这些文件被加载,它就不会被再次写入。什么是最好的方式来公开它,以便可以从任何.NET语言访问它?我知道这看起来很蹩脚,看起来我应该让他们实现ToString(),但是应用程序的一部分要求字符串采用某种格式,我不希望客户端必须以特定于我的应用程序的方式实现ToString() module MappingFormatters open System open System.Collections.Gener

我在下面的代码中声明了一个字典(格式化程序),它将在多个HttpModules中添加项。一旦这些文件被加载,它就不会被再次写入。什么是最好的方式来公开它,以便可以从任何.NET语言访问它?我知道这看起来很蹩脚,看起来我应该让他们实现ToString(),但是应用程序的一部分要求字符串采用某种格式,我不希望客户端必须以特定于我的应用程序的方式实现ToString()

module MappingFormatters
open System
open System.Collections.Generic

let formatters = new Dictionary<Type, obj -> string>();

let format item =

    let toDateTime (d:DateTime) =
        let mutable date = d;
        if (date.Kind) <> System.DateTimeKind.Utc then
            date <- date.ToUniversalTime()
        date.ToString("yyyy-MM-ddTHH:mm:00Z")

    let stripControlCharacters (str:string) =
        let isControl c = not (Char.IsControl(c))
        System.String( isControl |> Array.filter <| str.ToCharArray())

    let defaultFormat (item:obj) =
        match item with
        | :? string as str-> stripControlCharacters(str)
        | :? DateTime as dte -> toDateTime(dte)
        | _ -> item.ToString()

    let key = item.GetType();
    if formatters.ContainsKey(key) then
        formatters.Item(key) item
    else
        defaultFormat item
模块映射格式化程序
开放系统
open System.Collections.Generic
让格式化程序=新字典字符串>();
让格式项=
let toDateTime(d:DateTime)=
设可变日期=d;
如果(date.Kind)System.DateTimeKind.Utc,则
日期数组.filter stripControlCharacters(str)
| :? 日期时间作为dte->toDateTime(dte)
|->item.ToString()
让key=item.GetType();
如果格式化程序.ContainsKey(键),则
格式化程序。项(键)项
其他的
默认格式项

如果问题只是关于语言互操作性的,那么我认为您应该将类型从

Dictionary<Type, obj -> string>
字典字符串>

字典

然后你应该状态良好。

如果问题只是关于语言互操作性的,那么我认为你应该将类型从

Dictionary<Type, obj -> string>
字典字符串>

字典

然后你的身体应该很好。

研究之后。我决定创建一个名为MappingFormatters的类型来保存添加格式化程序的方法。客户不需要调用它,但我的f#代码会调用它。我相信这将使我能够使用常见的f#约定,同时为其他.net语言提供一种以最少的混乱进行交互操作的方法

module File1
open System


let mutable formatters = Map.empty<string, obj -> string>

let format (item:obj) = 

    let dateToString (d:DateTime) =
        let mutable date = d;
        if (date.Kind) <> System.DateTimeKind.Utc then
            date <- date.ToUniversalTime()
        date.ToString("yyyy-MM-ddTHH:mm:00Z")

    let stripCtrlChars (str:string) =
        let isControl c = not (Char.IsControl(c))
        System.String( isControl |> Array.filter <| str.ToCharArray())


    let key = item.GetType().AssemblyQualifiedName
    if Map.containsKey key formatters then
        Map.find key formatters item
    else
        match item with
        | :? DateTime as d -> dateToString d
        | _ -> stripCtrlChars (item.ToString())

let add (typ:Type) (formatter:obj -> string) =
    let contains = Map.containsKey
    let key = typ.AssemblyQualifiedName

    if not (formatters |> contains key) then
        formatters <- Map.add key formatter formatters

type MappingFormatters() = class
    let addLock = new obj()
    member a.Add (``type``:Type, formatter:Func<obj,string>) =
        lock addLock (fun () ->
            add ``type`` (fun x -> formatter.Invoke(x))
        )
end
模块文件1
开放系统
让可变格式设置程序=Map.empty string>
let格式(项目:obj)=
让dateToString(d:DateTime)=
设可变日期=d;
如果(date.Kind)System.DateTimeKind.Utc,则
date Array.filter dateToString d
|->stripCtrlChars(item.ToString())
let add(类型)(格式化程序:obj->string)=
让contains=Map.containsKey
let key=typ.AssemblyQualifiedName
如果不是(格式化程序|>包含键),则
格式化程序
添加``type``(乐趣x->formatter.Invoke(x))
)
结束

研究后。我决定创建一个名为MappingFormatters的类型来保存添加格式化程序的方法。客户不需要调用它,但我的f#代码会调用它。我相信这将使我能够使用常见的f#约定,同时为其他.net语言提供一种以最少的混乱进行交互操作的方法

module File1
open System


let mutable formatters = Map.empty<string, obj -> string>

let format (item:obj) = 

    let dateToString (d:DateTime) =
        let mutable date = d;
        if (date.Kind) <> System.DateTimeKind.Utc then
            date <- date.ToUniversalTime()
        date.ToString("yyyy-MM-ddTHH:mm:00Z")

    let stripCtrlChars (str:string) =
        let isControl c = not (Char.IsControl(c))
        System.String( isControl |> Array.filter <| str.ToCharArray())


    let key = item.GetType().AssemblyQualifiedName
    if Map.containsKey key formatters then
        Map.find key formatters item
    else
        match item with
        | :? DateTime as d -> dateToString d
        | _ -> stripCtrlChars (item.ToString())

let add (typ:Type) (formatter:obj -> string) =
    let contains = Map.containsKey
    let key = typ.AssemblyQualifiedName

    if not (formatters |> contains key) then
        formatters <- Map.add key formatter formatters

type MappingFormatters() = class
    let addLock = new obj()
    member a.Add (``type``:Type, formatter:Func<obj,string>) =
        lock addLock (fun () ->
            add ``type`` (fun x -> formatter.Invoke(x))
        )
end
模块文件1
开放系统
让可变格式设置程序=Map.empty string>
let格式(项目:obj)=
让dateToString(d:DateTime)=
设可变日期=d;
如果(date.Kind)System.DateTimeKind.Utc,则
date Array.filter dateToString d
|->stripCtrlChars(item.ToString())
let add(类型)(格式化程序:obj->string)=
让contains=Map.containsKey
let key=typ.AssemblyQualifiedName
如果不是(格式化程序|>包含键),则
格式化程序
添加``type``(乐趣x->formatter.Invoke(x))
)
结束
可能看到也可能看到