Export (0) Print
Expand All

ServiceKnownTypeAttribute Constructor (String, Type)

Initializes a new instance of the ServiceKnownTypeAttribute class with the name of a method that returns the known types and the type that contains the method (or methods) that returns the known types.

Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public ServiceKnownTypeAttribute(
	string methodName,
	Type declaringType


Type: System.String
The name of a method that returns the known types.
Type: System.Type
The type that can use the known types in its object graph.

The following example applies the ServiceKnownTypeAttribute attribute to an interface where the attribute specifies a method name and a declaring type.

    // Define a service contract and apply the ServiceKnownTypeAttribute
    // to specify types to include when generating client code. 
    // The types must have the DataContractAttribute and DataMemberAttribute
    // applied to be serialized and deserialized. The attribute specifies the 
    // name of a method (GetKnownTypes) in a class (Helper) defined below.
    [ServiceKnownType("GetKnownTypes", typeof(Helper))]
    public interface ICatalog
        // Any object type can be inserted into a Hashtable. The 
        // ServiceKnownTypeAttribute allows you to include those types
        // with the client code.
        Hashtable GetItems();

    // This class has the method named GetKnownTypes that returns a generic IEnumerable.
    static class Helper
        public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
            System.Collections.Generic.List<System.Type> knownTypes =
                new System.Collections.Generic.List<System.Type>();
            // Add any types to include here.
            return knownTypes;

    public class Widget
        public string Id;
        public string Catalog;

    public class Machine : Widget
        public string Maker;

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2014 Microsoft