如何获得访问WCF中继承的对象的属性?如何获得、属性、对象、WCF

2023-09-04 11:42:50 作者:Chihiro(千寻)

目前,此案:

[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;
    }
}

是否有可能知道DTO的法律类型,有机会获得孩子的属性?

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

我要存储的DTO,我不希望有很多服务的信息每个孩子。

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

传递泛型服务无法正常工作,WSDL错误, 接口如IInfo作为输入参数不起作用,铸造错误, 基类像信息不起作用,孩子的道具都不能访问, 堆栈溢出是不行的,这是我第二次我张贴这种概率,但没有答案!

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!

推荐答案

我传递一个JSON作为DTO为了MyService。 如果我添加__type:法律:#Dto,为MyService承认DTO的法律。 然后(DTO法律).ManagerName具有价值

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

此解决方案的工作,实际上是通过__type没有简便的方法。我将AP preciate你更好的建议。

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