Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
WCF上的DataContract和层次结构存在问题_Wcf_C# 4.0_Datacontract - Fatal编程技术网

WCF上的DataContract和层次结构存在问题

WCF上的DataContract和层次结构存在问题,wcf,c#-4.0,datacontract,Wcf,C# 4.0,Datacontract,我的wcf项目中的对象有问题。 我已经说过这个物体: [DataContract(Name="ClassA")] public class Person{ //---attributes--- } [DataContract(Name="ClassB")] public class Men : Person{ //---attributes--- } 其中ClassB是另一侧ClassA的子级。 然后我有一个方法是post: [OperationContract] [WebInvo

我的wcf项目中的对象有问题。 我已经说过这个物体:

[DataContract(Name="ClassA")]
public class Person{
   //---attributes---
}

[DataContract(Name="ClassB")]
public class Men : Person{
  //---attributes---
}
其中ClassB是另一侧ClassA的子级。 然后我有一个方法是post:

[OperationContract]
[WebInvoke(UriTemplate= "Person", ResponseFormat = WebMessageFormat.Json, Method= "POST")]
public string PostPerson(Person person) {
    if(person is Men){
       //code...
    }
}

问题是我收到了那个人(在另一边,他们把我作为B类发送给我),但那个人是男人,返回错误。。为什么?

您需要将
[ServiceKnownType(typeof(Men))]
属性添加到PostPerson方法中

正如Ryan Gross提到的,你需要男人成为一个知名的类型。链接文章中未提及的一个选项是。下面是我过去使用过的一个代码示例。前提是此类是所有数据协定的基类,并且所有数据协定都在同一程序集中:

/// <summary>
///   Base class for all data contracts.
/// </summary>
[DataContract(Name = "Base", Namespace = "your namespace")]
[KnownType("GetKnownTypes")]
public class BaseDC : IExtensibleDataObject
{
  #region Constants and Fields

  /// <summary>
  ///   Instance used to control access to the known types list.
  /// </summary>
  private static readonly object _knownTypesLock = new object();

  /// <summary>
  ///   Classes derived from this class.  Needed to ensure proper functioning of the WCF data
  ///   constract serializer.
  /// </summary>
  private static List<Type> _knownTypes;

  #endregion

  #region Properties

  /// <summary>
  ///   Gets or sets an <c>ExtensionDataObject</c> that contains data that is not recognized as belonging to the
  ///   data contract.
  /// </summary>
  public ExtensionDataObject ExtensionData { get; set; }

  #endregion

  #region Public Methods

  /// <summary>
  ///   Enumerates the types in the assembly containing <c>BaseDC</c> that derive from <c>BaseDC</c>.
  /// </summary>
  /// <returns>List of <c>BaseDC</c>-derived types.</returns>
  [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate",
    Justification = "Not appropriate for a property.")]
  public static IEnumerable<Type> GetKnownTypes()
  {
    lock (_knownTypesLock)
    {
      if (_knownTypes == null)
      {
        _knownTypes = new List<Type>();
        Assembly contractsAssembly = Assembly.GetAssembly(typeof(BaseDC));
        Type[] assemblyTypes = contractsAssembly.GetTypes();
        foreach (Type assemblyType in assemblyTypes)
        {
          if (assemblyType.IsClass && !assemblyType.IsGenericType)
          {
            if (assemblyType.IsSubclassOf(typeof(BaseDC)))
            {
              _knownTypes.Add(assemblyType);
            }
          }
        }

        _knownTypes.Add(typeof(BaseDC));
      }

      return _knownTypes;
    }
  }

  #endregion
}
//
///所有数据协定的基类。
/// 
[DataContract(Name=“Base”,Namespace=“您的命名空间”)]
[KnownType(“GetKnownType”)]
公共类BaseDC:IEExtensibleDataObject
{
#区域常数和字段
/// 
///实例,用于控制对已知类型列表的访问。
/// 
私有静态只读对象_knownTypesLock=新对象();
/// 
///从此类派生的类。需要确保WCF数据正常运行
///构造序列化程序。
/// 
私有静态列表_knownTypes;
#端区
#区域属性
/// 
///获取或设置一个ExtensionDataObject,该对象包含无法识别为属于
///数据合同。
/// 
公共扩展数据对象扩展数据{get;set;}
#端区
#区域公共方法
/// 
///枚举包含从BaseDC派生的BaseDC的程序集中的类型。
/// 
///BaseDC派生类型的列表。
[SuppressMessage(“Microsoft.Design”、“CA1024:UseProperties whereproperties”,
Justification=“不适用于属性。”)]
公共静态IEnumerable GetKnownTypes()
{
锁(_knownTypesLock)
{
如果(_knownTypes==null)
{
_knownTypes=新列表();
装配合同装配=Assembly.GetAssembly(typeof(BaseDC));
类型[]assemblyTypes=contractsAssembly.GetTypes();
foreach(assemblyTypes中的assemblyType类型)
{
if(assemblyType.IsClass&!assemblyType.IsGenericType)
{
if(assemblyType.IsSubclassOf(typeof(BaseDC)))
{
_添加(assemblyType);
}
}
}
_添加(类型(BaseDC));
}
返回_knownTypes;
}
}
#端区
}