【问题标题】:adding KnownType for class in another Assembly在另一个程序集中为类添加 KnownType
【发布时间】:2018-06-22 16:24:04
【问题描述】:

我有扩展A 的类B,因为它位于另一个程序集中,我不能简单地将[KnownType(typeof(B))] 添加到A 以使其序列化,因为这需要循环依赖。我尝试在配置中执行此操作,但由于某种原因它不起作用。

现在我正在尝试这里描述的方法

Generally accepted way to avoid KnownType attribute for every derived class

(我加载 B 的程序集而不是 GetExecutingAssembly)当我尝试更新服务引用时,我得到 The URI prefix is not recognized. Metadata contains a reference that cannot be resolved..." 这可能是什么问题?

【问题讨论】:

  • 提供试用过的代码。

标签: c# wcf


【解决方案1】:

这可能会对您有所帮助:

假设ClassA在程序集A中,ClassB在程序集B中,A 在 B 中被引用。考虑以下几点:

[KnownType("GetDerivedTypes")]
[DataContract]
public abstract class ClassA         \\This is referenced in B
{
    static Type[] GetDerivedTypes()
    { 
        return DerivedTypes.ToArray();  
    }
    public static List<Type> DerivedTypes = new List<Type>();
}

您不能在前面添加 typeof(ClassB)。然而,

[DataContract]
public class ClassB : ClassA
{
    static ClassB()
    {
        ClassA.DerivedTypes.Add(typeof(ClassB));
    }

    public ClassB()
    {
    }
}

【讨论】:

    猜你喜欢
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多