Note that there are some explanatory texts on larger screens.

plurals
  1. POSame KnownType Attribute For Multiple Abstract Classes
    primarykey
    data
    text
    <p>I'd like to know if I can add the same class as a knowntype to multiple abstract classes.</p> <p>My problem at the moment is that I have two abstract classes in the same namespace:</p> <p>Let's call this namespace "Thing"</p> <p>I have two abstract classes in Thing, Fruit and Vegetable.</p> <p>Both of these abstract classes are inherited by the two different classes in the same namespace, but have the same name. "Tomato"</p> <p>So I have two declarations:</p> <pre><code>namespace Thing [KnownType(typeof(Tomato))] public abstract class Fruit {} public sealed class Tomato: Fruit{} </code></pre> <p>as well as</p> <pre><code>namespace Thing [KnownType(typeof(Tomato))] public abstract class Vegetable {} public sealed class Tomato: Vegetable{} </code></pre> <p>Type 'Thing.Tomato' cannot be added to list of known types since another type 'Thing.Tomato' with the same data contract name "{datacontractname: Tomato}" is already present.</p> <p>I've refactored the two declarations of Tomato into it's own file and now one single declaration. Now everything will serialize, but when I look at the custom attributes for both abstract classes only one of them will get the knowntype attribute. That class will also be the only one that knows that it has a subclass of Tomato.</p> <p>Edit: I was a bit too hasty with my refactor as I need Tomato to have multiple inheritance. At the moment I've changed namespace Thing to Thing.Fruit and Thing.Vegetable, keeping the multiple declarations. The duplication of code makes me dubious that this is a good solution though.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload