在另一个Assembly中为类添加KnownType

时间:2018-06-22 16:24:05

标签: c# wcf

我有扩展ListView.builder( itemBuilder: (context, index) { if (index == 0) // only the first item will be a "margin widget", with some width and height return Container(width: _placeholderWidth, height: _placeholderHeight); return NormalItem(index - 1); // return your "normal" items, substract the "margin item" index ... ) 的类B,并且因为它位于另一个程序集中,所以我不能简单地将A添加到[KnownType(typeof(B))]使其序列化,因为这需要循环依赖。我尝试在配置中执行此操作,但是由于某种原因它不起作用。

现在我正在尝试此处描述的方法

Generally accepted way to avoid KnownType attribute for every derived class

(我加载B的程序集而不是GetExecutingAssembly)当我尝试更新服务引用时,我得到A可能是什么问题?

1 个答案:

答案 0 :(得分:0)

这可能会帮助您:

假定 ClassA 在程序集 A 中,而 ClassB 在程序集 B A < / strong>在 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()
    {
    }
}