gpt4 book ai didi

wcf - F#,序列化具有缺少数据的值的可区分联合

转载 作者:行者123 更新时间:2023-12-02 01:35:04 24 4
gpt4 key购买 nike

首先,我不得不说,我知道在 .NET 中与其他语言集成时使用 F# 特定的东西通常不是一个好主意。

我的问题是我不明白如何创建对包含公开可区分联合的方法的服务的服务引用。

我得到的基础知识有点像这样:

  type TelephonyProductActivationData =
| MobileUseNextIcc
| Mobile of decimal
| MobileBroadbandUseNextIcc
| MobileBroadband of decimal
| Fixed
| Voip of int16 * int16
static member KnownTypes() =
typeof<TelephonyProductActivationData>.GetNestedTypes(BindingFlags.Public ||| BindingFlags.NonPublic) |> Array.filter FSharpType.IsUnion

如果您使用 F# 交互方式首先创建类型:

type TelephonyProductActivationData =
| MobileUseNextIcc of unit
| Mobile of decimal<Icc>
| MobileBroadbandUseNextIcc of unit
| MobileBroadband of decimal<Icc>
| Fixed of unit
| Voip of BoxNr * int16<BoxPort>;;

然后执行已知类型代码部分(稍作修改):

(typeof<TelephonyProductActivationData>.GetNestedTypes(System.Reflection.BindingFlags.Public ||| System.Reflection.BindingFlags.NonPublic) |> Array.filter Microsoft.FSharp.Reflection.FSharpType.IsUnion) |> Array.map (fun x -> x.FullName);;

您将看到以下输出:

val it : string [] =
[|"FSI_0047+TelephonyProductActivationData+Mobile";
"FSI_0047+TelephonyProductActivationData+MobileBroadband";
"FSI_0047+TelephonyProductActivationData+Voip"|]

请注意,没有关联数据的值已消失。这意味着编译此可区分联合时不会创建任何类型。通过在 F# 交互中执行此语句:

typeof<TelephonyProductActivationData>.GetProperties() |> Array.map (fun x -> (x.Name));;

我们将看看它们变成了什么:

val it : string [] =
[|"Tag"; "IsVoip"; "Fixed"; "IsFixed"; "IsMobileBroadband";
"MobileBroadbandUseNextIcc"; "IsMobileBroadbandUseNextIcc"; "IsMobile";
"MobileUseNextIcc"; "IsMobileUseNextIcc"|]

正如您所看到的,没有与之关联的数据的值已成为属性。现在我可以告诉你真正的问题。当使用此方法创建对服务的服务引用时,我得到的是:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="ActivationModel.TelephonyProductActivationData", Namespace="http://schemas.datacontract.org/2004/07/Svea.Inri.Data")]
[System.SerializableAttribute()]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(ConsoleApplication1.ServiceReference1.ActivationModelTelephonyProductActivationData.Mobile))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(ConsoleApplication1.ServiceReference1.ActivationModelTelephonyProductActivationData.MobileBroadband))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(ConsoleApplication1.ServiceReference1.ActivationModelTelephonyProductActivationData.Voip))]
public partial class ActivationModelTelephonyProductActivationData : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

private int _tagField;

[global::System.ComponentModel.BrowsableAttribute(false)]
public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
get {
return this.extensionDataField;
}
set {
this.extensionDataField = value;
}
}

[System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
public int _tag {
get {
return this._tagField;
}
set {
if ((this._tagField.Equals(value) != true)) {
this._tagField = value;
this.RaisePropertyChanged("_tag");
}
}
}

public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="ActivationModel.TelephonyProductActivationData.Mobile", Namespace="http://schemas.datacontract.org/2004/07/Svea.Inri.Data")]
[System.SerializableAttribute()]
public partial class Mobile : ConsoleApplication1.ServiceReference1.ActivationModelTelephonyProductActivationData {

private decimal itemField;

[System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
public decimal item {
get {
return this.itemField;
}
set {
if ((this.itemField.Equals(value) != true)) {
this.itemField = value;
this.RaisePropertyChanged("item");
}
}
}
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="ActivationModel.TelephonyProductActivationData.MobileBroadband", Namespace="http://schemas.datacontract.org/2004/07/Svea.Inri.Data")]
[System.SerializableAttribute()]
public partial class MobileBroadband : ConsoleApplication1.ServiceReference1.ActivationModelTelephonyProductActivationData {

private decimal itemField;

[System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
public decimal item {
get {
return this.itemField;
}
set {
if ((this.itemField.Equals(value) != true)) {
this.itemField = value;
this.RaisePropertyChanged("item");
}
}
}
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="ActivationModel.TelephonyProductActivationData.Voip", Namespace="http://schemas.datacontract.org/2004/07/Svea.Inri.Data")]
[System.SerializableAttribute()]
public partial class Voip : ConsoleApplication1.ServiceReference1.ActivationModelTelephonyProductActivationData {

private string item1Field;

private short item2Field;

[System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
public string item1 {
get {
return this.item1Field;
}
set {
if ((object.ReferenceEquals(this.item1Field, value) != true)) {
this.item1Field = value;
this.RaisePropertyChanged("item1");
}
}
}

[System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
public short item2 {
get {
return this.item2Field;
}
set {
if ((this.item2Field.Equals(value) != true)) {
this.item2Field = value;
this.RaisePropertyChanged("item2");
}
}
}
}
}

ActivationModelTelephonyProductActivationData 没有子类(ActivationModel 部分是命名空间)来表示不包含任何数据的值,并且基类中不存在可以设置不包含任何数据的值的属性。

我的问题最后是,应该如何做到这一点。我是否必须将“单位”添加到所有没有数据的受歧视联合值中。

最佳答案

如果您像下面这样定义 DU 类型,它将起作用。

[<KnownType("KnownTypes")>]
//[<DataContract>] // note: keep KnownTypes, but avoid DataContract
// so that DataContractSerializer uses .NET 'Serializable' instead
type TelephonyProductActivationData =
| MobileUseNextIcc
| Mobile of decimal
| MobileBroadbandUseNextIcc
| MobileBroadband of decimal
| Fixed
| Voip of int16 * int16
static member KnownTypes() =
typeof<TelephonyProductActivationData>.GetNestedTypes(BindingFlags.Public |||
BindingFlags.NonPublic)
|> Array.filter FSharpType.IsUnion

关于wcf - F#,序列化具有缺少数据的值的可区分联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8563664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com