---
title: "What is the use of [KnownType] attribute in WCF?"  
description: "What is the use of [KnownType] attribute in WCF?"  
author: "ICSM Computer"  
published: 2025-05-26  
updated: 2025-05-26  
canonical: https://www.mindstick.com/interview/34166/what-is-the-use-of-knowntype-attribute-in-wcf  
category: "c#"  
tags: ["c#", "wcf"]  
reading_time: 3 minutes  

---

# What is the use of [KnownType] attribute in WCF?

The `[KnownType]` attribute in WCF is used to **inform the DataContractSerializer about derived or related types** that might be encountered during serialization or deserialization of a base type.

### Why `[KnownType]` is Needed

By default, WCF only serializes types it knows about — those marked with `[DataContract]` and explicitly referenced. If a service operation uses a **base class or interface** as a parameter or return type, WCF needs to know in advance about all **possible derived types**.

Without `[KnownType]`, WCF will throw a serialization error when it encounters an unknown derived type at runtime.

### Example Scenario

Suppose you have a base class `Shape` and two derived classes `Circle` and `Square`.

```cs
[DataContract]
[KnownType(typeof(Circle))]
[KnownType(typeof(Square))]
public abstract class Shape
{
    [DataMember]
    public string Color { get; set; }
}

[DataContract]
public class Circle : Shape
{
    [DataMember]
    public double Radius { get; set; }
}

[DataContract]
public class Square : Shape
{
    [DataMember]
    public double SideLength { get; set; }
}
```

Now you can safely use `Shape` as a parameter or return type in your service contract:

```cs
[ServiceContract]
public interface IShapeService
{
    [OperationContract]
    void SaveShape(Shape shape);
}
```

WCF will correctly serialize and deserialize `Circle` or `Square` when passed as a `Shape`.

### Alternate Form of KnownType

You can also declare known types through a method:

```cs
[KnownType("GetKnownTypes")]
public abstract class Shape
{
    public static Type[] GetKnownTypes()
    {
        return new Type[] { typeof(Circle), typeof(Square) };
    }
}
```

This is useful if known types are determined dynamically.

### Summary

1. `[KnownType]` helps WCF handle polymorphism during serialization.
2. Required when base types may be substituted with derived types at runtime.
3. Prevents runtime errors during service communication involving inheritance.

## Answers

### Answer by ICSM Computer

The `[KnownType]` attribute in WCF is used to **inform the DataContractSerializer about derived or related types** that might be encountered during serialization or deserialization of a base type.

### Why `[KnownType]` is Needed

By default, WCF only serializes types it knows about — those marked with `[DataContract]` and explicitly referenced. If a service operation uses a **base class or interface** as a parameter or return type, WCF needs to know in advance about all **possible derived types**.

Without `[KnownType]`, WCF will throw a serialization error when it encounters an unknown derived type at runtime.

### Example Scenario

Suppose you have a base class `Shape` and two derived classes `Circle` and `Square`.

```cs
[DataContract]
[KnownType(typeof(Circle))]
[KnownType(typeof(Square))]
public abstract class Shape
{
    [DataMember]
    public string Color { get; set; }
}

[DataContract]
public class Circle : Shape
{
    [DataMember]
    public double Radius { get; set; }
}

[DataContract]
public class Square : Shape
{
    [DataMember]
    public double SideLength { get; set; }
}
```

Now you can safely use `Shape` as a parameter or return type in your service contract:

```cs
[ServiceContract]
public interface IShapeService
{
    [OperationContract]
    void SaveShape(Shape shape);
}
```

WCF will correctly serialize and deserialize `Circle` or `Square` when passed as a `Shape`.

### Alternate Form of KnownType

You can also declare known types through a method:

```cs
[KnownType("GetKnownTypes")]
public abstract class Shape
{
    public static Type[] GetKnownTypes()
    {
        return new Type[] { typeof(Circle), typeof(Square) };
    }
}
```

This is useful if known types are determined dynamically.

### Summary

1. `[KnownType]` helps WCF handle polymorphism during serialization.
2. Required when base types may be substituted with derived types at runtime.
3. Prevents runtime errors during service communication involving inheritance.


---

Original Source: https://www.mindstick.com/interview/34166/what-is-the-use-of-knowntype-attribute-in-wcf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
