C# WCF和各种返回分类

C# WCF和各种返回分类,c#,wcf,inheritance,C#,Wcf,Inheritance,我遇到了一个我找不到好的解决方案的问题——我有一个WCF服务,我想返回一个从父类继承的ChildClass对象 大多数情况下,我会返回ChildClass,但在某些情况下,我只想返回FatherClass(它只包含1个字段“error”) 这能做到吗 我的代码: [WebGet(UriTemplate = "SomeQueryString", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json)] public Child

我遇到了一个我找不到好的解决方案的问题——我有一个WCF服务,我想返回一个从父类继承的ChildClass对象

大多数情况下,我会返回ChildClass,但在某些情况下,我只想返回FatherClass(它只包含1个字段“error”)

这能做到吗

我的代码:

[WebGet(UriTemplate = "SomeQueryString", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json)]
public ChildClass GetCollection(parameter)
{

    if (err)
    {
        return new FatherClass();
    }
    else
    {
        return new ChildClass();
    }
}
其中as ChildClass继承自父类(字段较少)

我的目标是只返回“文本”的一小部分,而不是返回整个ChildClass对象时将返回的文本

想法?:)


谢谢

只有在重新定义操作和约定的情况下,这才是可能的-您必须返回父级,并且序列化程序必须知道可用于替代父级的所有子级:

[KnownType(typeof(ChildClass)]
[DataContract]
public class ParentClass 
{
    // DataMembers
}

[DataContract]
public class ChildClass : ParentClass 
{
    // DataMembers
}
您的操作将如下所示:

[WebGet(UriTemplate = "SomeQueryString", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json)]
public ParentClass GetCollection(parameter)
{
    ...
}

这只有在重新定义操作和协定时才有可能-您必须返回父项,并且序列化程序必须知道所有可用于代替父项的子项:

[KnownType(typeof(ChildClass)]
[DataContract]
public class ParentClass 
{
    // DataMembers
}

[DataContract]
public class ChildClass : ParentClass 
{
    // DataMembers
}
您的操作将如下所示:

[WebGet(UriTemplate = "SomeQueryString", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json)]
public ParentClass GetCollection(parameter)
{
    ...
}

我认为这是一个关于C#和类型铸造的问题。正如你所拥有的,它不会起作用,因为孩子阶级:父亲阶级。见下文:

    class FatherClass
    {
        public int x { get; set; }
    }

    class ChildClass : FatherClass
    {
        public int y { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            FatherClass a = new FatherClass();
            ChildClass b = new ChildClass();


            FatherClass c = (FatherClass)b;
            ChildClass d = (ChildClass)a;

            Console.ReadLine();
        }
    }
演员ChildClass d=(ChildClass)a;将失败。因此,您可以尝试将您的签名修改为

public FatherClass GetCollection(parameter)

并使用适当的类型转换。

我认为这是一个关于C#和类型转换的问题。正如你所拥有的,它不会起作用,因为孩子阶级:父亲阶级。见下文:

    class FatherClass
    {
        public int x { get; set; }
    }

    class ChildClass : FatherClass
    {
        public int y { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            FatherClass a = new FatherClass();
            ChildClass b = new ChildClass();


            FatherClass c = (FatherClass)b;
            ChildClass d = (ChildClass)a;

            Console.ReadLine();
        }
    }
演员ChildClass d=(ChildClass)a;将失败。因此,您可以尝试将您的签名修改为

public FatherClass GetCollection(parameter)

并使用合适的类型铸造。

谢谢您的详细回答。我不相信它会起作用,因为如果我通过ChildClass,WCF不会将其序列化到ChildClass。我在那个问题上有个错误。谢谢你详细的回答。我不相信它会起作用,因为如果我通过ChildClass,WCF不会将其序列化到ChildClass。我在这个问题上有个错误,谢谢。似乎是正确的方向。。。尽管我得到了错误,其中包括“使用ServiceKnownTypeAttribute将类型添加到操作的已知类型集合”。我也应该添加一个接口吗?仅供将来参考-没有将[ServiceKnownType(typeof(ChildClass))]添加到服务本身。@roman:如果您不使用
KnownType
标记基类型,这是另一种方法,谢谢。似乎是正确的方向。。。尽管我得到了错误,其中包括“使用ServiceKnownTypeAttribute将类型添加到操作的已知类型集合”。我也应该添加一个接口吗?仅供将来参考-没有将[ServiceKnownType(typeof(ChildClass))]添加到服务本身。@roman:如果您不使用
KnownType