简体   繁体   中英

How to get access to inherited object properties in WCF?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM