Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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
如何在C#中声明和初始化在F#中定义的有区别的并集值?_C#_F# - Fatal编程技术网

如何在C#中声明和初始化在F#中定义的有区别的并集值?

如何在C#中声明和初始化在F#中定义的有区别的并集值?,c#,f#,C#,F#,如何在C#中声明和初始化在F#中定义的有区别的并集值 F#代码: namespace Core [<AutoOpen>] module EventStore = type Events = | BuyRequested of RequestInfo | SellRequested of RequestInfo var myEvent = new Events.NewBuyRequested(requestInfo); // Doesn'

如何在C#中声明和初始化在F#中定义的有区别的并集值

F#代码:

namespace Core

[<AutoOpen>]
module EventStore =

    type Events =
        | BuyRequested  of RequestInfo
        | SellRequested of RequestInfo
var myEvent = new Events.NewBuyRequested(requestInfo); // Doesn't compile
我试图引用此示例,以便可以引用以下示例:

type Shape =
| Circle of float
| Rectangle of float * float
C#:


但是我没有看到为我的DU案例值(即BuyRequested、SellRequested)公开的任何方法。

正如您已经发现的那样,
新的…
方法不是构造函数,而是静态方法。为了完整起见,我想添加以下内容:

namespace Core

[<AutoOpen>]
module Entities =
    type RequestInfo = { 
        AccountId : string
        Symbol    : string
        Quantity  : int 
    }
  • 对于所有具有参数的联合案例,F#编译器生成一个名为
    “New”
    加上联合案例名称的静态方法
  • 对于所有没有参数的联合案例,将生成一个静态只读属性,该属性与联合案例具有相同的名称
比如说这个联盟,

type U = 
    | U1
    | U2 of int
你会得到:

> let members = typeof<U>.GetMembers();;
val members : System.Reflection.MemberInfo [] =
  [|U get_U1(); Boolean get_IsU1(); U NewU2(Int32); Boolean get_IsU2();
    Int32 get_Tag(); Int32 CompareTo(U); Int32 CompareTo(System.Object);
...
>让members=typeof.GetMembers();;
val成员:System.Reflection.MemberInfo[]=
[| U get_U1();Boolean get_IsU1();U NewU2(Int32);Boolean get_IsU2();
Int32 get_Tag();Int32 CompareTo(U);Int32 CompareTo(System.Object);
...
个别个案包括:

> typeof<U>.GetMember("get_U1");;
val it : System.Reflection.MemberInfo [] =
  [|U get_U1()
      {Attributes = PrivateScope, Public, Static;
       CallingConvention = Standard;
       ContainsGenericParameters = false;
       CustomAttributes = seq [[Microsoft.FSharp.Core.CompilationMappingAttribute((Microsoft.FSharp.Core.SourceConstructFlags)8, (Int32)0)]];
...
> typeof<U>.GetMember("NewU2");;
val it : System.Reflection.MemberInfo [] =
  [|U NewU2(Int32)
      {Attributes = PrivateScope, Public, Static;
       CallingConvention = Standard;
       ContainsGenericParameters = false;
       CustomAttributes = seq [[Microsoft.FSharp.Core.CompilationMappingAttribute((Microsoft.FSharp.Core.SourceConstructFlags)8, (Int32)1)]];
>typeof.GetMember(“get_U1”);;
val it:System.Reflection.MemberInfo[]=
[U get_U1()
{Attributes=PrivateScope,Public,Static;
呼叫约定=标准;
ContainsGenericParameters=false;
CustomAttributes=seq[[Microsoft.FSharp.Core.CompilationMappingAttribute((Microsoft.FSharp.Core.SourceConstructFlags)8,(Int32)0)];
...
>typeof.GetMember(“NewU2”);;
val it:System.Reflection.MemberInfo[]=
[| U NewU2(Int32)
{Attributes=PrivateScope,Public,Static;
呼叫约定=标准;
ContainsGenericParameters=false;
CustomAttributes=seq[[Microsoft.FSharp.Core.CompilationMappingAttribute((Microsoft.FSharp.Core.SourceConstructFlags)8,(Int32)1)];
CompilationMappingAttribute
是一个将它们标识为来自联合案例的属性,并包含它们的定义顺序。

从“new Events.NewBuyRequested”中删除了new。它现在可以工作了。
> typeof<U>.GetMember("get_U1");;
val it : System.Reflection.MemberInfo [] =
  [|U get_U1()
      {Attributes = PrivateScope, Public, Static;
       CallingConvention = Standard;
       ContainsGenericParameters = false;
       CustomAttributes = seq [[Microsoft.FSharp.Core.CompilationMappingAttribute((Microsoft.FSharp.Core.SourceConstructFlags)8, (Int32)0)]];
...
> typeof<U>.GetMember("NewU2");;
val it : System.Reflection.MemberInfo [] =
  [|U NewU2(Int32)
      {Attributes = PrivateScope, Public, Static;
       CallingConvention = Standard;
       ContainsGenericParameters = false;
       CustomAttributes = seq [[Microsoft.FSharp.Core.CompilationMappingAttribute((Microsoft.FSharp.Core.SourceConstructFlags)8, (Int32)1)]];