Skip to content

Latest commit

 

History

History
117 lines (88 loc) · 3.09 KB

HOW-TO attribs.md

File metadata and controls

117 lines (88 loc) · 3.09 KB

Filtering code generation sources using C# attributes

Copyright (C) 2026 The Qt Company Ltd. SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

It's possible to adjust which types and type members are included and excluded from code generation by using the attributes described below.

Ignore types

The simplest way of filtering out a type, regardless of where it is defined — either in the current module or in an external assembly — is by listing that type in a [Qt.IgnoreType] assembly-level attribute.

[assembly: Qt.IgnoreType(typeof(List<int>), typeof(List<string>))]

// This class will be included
public class Foo
{
    // These 2 methods will be excluded due to ignored parameter types
    public void Bar(List<int> arg) { }
    public void Bar(List<string> arg) { }

    // This method will be included
    public void Bar(List<double> arg) { }
}

Generic types

To ignore a generic type and all of its specializations, use either an open generic type definition or the string form of the generic type name.

[assembly: Qt.IgnoreType(typeof(List<>))]

If the type cannot be referenced directly in source code, use the string form instead.

[assembly: Qt.IgnoreType("System.Collections.Generic.List`1")]

// This class will be included
public class Foo
{
    // These 3 methods will be excluded due to ignored parameter types
    public void Bar(List<int> arg) { }
    public void Bar(List<string> arg) { }
    public void Bar(List<double> arg) { }
}

Ignore type definition

It's possible to exclude a type defined in the current project's code by adding a [Qt.Ignore] attribute to the type declaration.

// This class will be excluded
[Qt.Ignore]
public class Foo
{ }

Ignore type member definition

Similarly, a member of a type defined in code can also be excluded from code generation using a [Qt.Ignore] attribute.

// This class will be included
public class Foo
{
    // This property will be excluded
    [Qt.Ignore]
    public int Bar { get; set; }
}

Ignore type hierarchy

The [Qt.IgnoreType] attribute can also be used to filter out entire type hierarchies, i.e. any type that derives from the root type listed in the attribute. This is achieved by specifying Inherited = true in the attribute instantiation.

[assembly: Qt.IgnoreType("System.Collections.Generic.Dictionary`2", Inherited = true)]

// This class will be excluded as it derives from an ignored type hierarchy
public class Foo : Dictionary<int, string>
{
    ...
}

Include sub-type of ignored type hierarchy

It's still possible to include types deriving from an ignored type hierarchy. These must be opted-in to the code generation by using the [Qt.Include] attribute.

[assembly: Qt.IgnoreType(typeof(Foo), Inherited = true)]

// This class will be ignored (ignored base type)
public class Foo
{ }

// This class will be ignored (derived type of ignored base type)
public class Oof : Foo
{ }

// This class will be included (opted-in by attribute)
[Qt.Include]
public class Bar : Foo
{ }