How to get access to inherited object properties in WCF?

匿名 (未验证) 提交于 2019-12-03 00:54:02

问题:

The case is:

[ServiceContract] public interface IInfo {     [DataMember]     int Id{get;set;} }  [DataContract] [KnownType(typeof(Legal))] public class Info {     [DataMember]     public int Id { get; set; } }  [DataContract] public class Legal : Info {     [DataMember]     public string ManagerName { get; set; } }  [ServiceContract] [ServiceKnownType(typeof(Legal))] public interface IMyService {     [OperationContract]     int DoWork(Info dto); }  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class MyService : IMyService {      public int DoWork(Info dto)     {         string name;         if (dto is Legal)             name = (dto as Legal).ManagerName;         return dto.Id;     } } 

Is it possible to know the dto as Legal type and have access to the child properties?

I want to store the dto and I don't want to have many services for each child of info.

Passing Generics to service doesn't work, wsdl error, Interface such as IInfo as input parameter doesn't work, casting error, Base class like Info doesn't work, child props are not accessible, stack overflow doesn't work, this is my 2nd time i post this prob but no answer!

回答1:

I'm passing a json as dto to MyService. If i add "__type":"Legal:#Dto", MyService recognize the dto as Legal. then (dto as Legal).ManagerName has value

This solution is working, actually passing __type is not handy way. I'll appreciate your better suggestions.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!