WCF - 通过复制/粘贴原始代码或通过共享抽象类自动生成的代码来实现 DataContract?
[DataContract]
public abstract class FooBase
{
[DataMember]
public int Bar { get; set; }
}
这是一个基类,我将其用作其他也是 DataContract 的类的基类。但问题是......
在 Proj1 中,我选择添加服务引用... (MyService),它为我生成代码,包括 FooBase 代码。 在 Proj2 中,我选择添加服务引用... (OtherService),它执行相同的操作。
但是,我希望 Foo 基类位于它自己的程序集中,两个项目都可以引用...因此,最好将
FooBase 类按原样复制/粘贴到其他共享程序集中吗?
[DataContract]
public abstract class FooBase
{
[DataMember]
public int Bar { get; set; }
}
或者,将 FooBase 类生成的代码复制/粘贴到其他共享程序集中?
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="FooBase", Namespace="http://schemas.datacontract.org/2004/07/MyNamespace")]
[System.SerializableAttribute()]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(MyNamespace.Proj1.TypeA))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(MyNamespace.Proj1.TypeB))]
public partial class FooBase : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
private int BarField;
[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 Bar {
get {
return this.BarField;
}
set {
if ((this.BarField.Equals(value) != true)) {
this.BarField = value;
this.RaisePropertyChanged("Bar");
}
}
}
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));
}
}
}
[DataContract]
public abstract class FooBase
{
[DataMember]
public int Bar { get; set; }
}
That is a base class that I use as the base class for other classes that are also DataContracts. Here's the problem though...
In Proj1 I choose Add Service Reference... (MyService) and it generates the code for me, including the FooBase code.
In Proj2 I choose Add Service Reference... (OtherService) and it does the same.
But, I want the Foo base class to be in it's own assembly that both projects can reference... so, is it better to:
Copy/paste the FooBase class into the other shared assembly as it is?
[DataContract]
public abstract class FooBase
{
[DataMember]
public int Bar { get; set; }
}
Or, copy/paste the generated code for the FooBase class into the other shared assembly?
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="FooBase", Namespace="http://schemas.datacontract.org/2004/07/MyNamespace")]
[System.SerializableAttribute()]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(MyNamespace.Proj1.TypeA))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(MyNamespace.Proj1.TypeB))]
public partial class FooBase : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
private int BarField;
[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 Bar {
get {
return this.BarField;
}
set {
if ((this.BarField.Equals(value) != true)) {
this.BarField = value;
this.RaisePropertyChanged("Bar");
}
}
}
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));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将其放在共享库中,但在添加服务引用时,请确保引用共享库,并且在添加引用对话框的高级设置中,您已选择在所有引用的程序集中重用类型。
我的偏好是使用共享库中的非生成代码,即仅包含数据契约的代码。
You can have it in a shared library but when adding service reference make sure that shared library is referenced, and that in the Advanced settings of the add reference dialog, you have selected Reuse types in all referenced assemblies.
My preference would be using the non-generated code in the shared library, that is code with just data contracts.