Skip to content

Fody/EmptyConstructor

Repository files navigation

EmptyConstructor.Fody

Chat on Gitter NuGet Status

Adds an empty constructor to classes even if you have a non-empty one defined.

See Milestones for release notes.

This is an add-in for Fody

It is expected that all developers using Fody become a Patron on OpenCollective. See Licensing/Patron FAQ for more information.

Usage

See also Fody usage.

NuGet installation

Install the EmptyConstructor.Fody NuGet package and update the Fody NuGet package:

PM> Install-Package Fody
PM> Install-Package EmptyConstructor.Fody

The Install-Package Fody is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.

Add to FodyWeavers.xml

Add <EmptyConstructor/> to FodyWeavers.xml

<Weavers>
  <EmptyConstructor/>
</Weavers>

Configuration Options

Exclude types with an Attribute

If for some reason you want to skip a specific class you can mark it with a DoNotVirtualizeAttribute.

Since no reference assembly is shipped with Virtuosity. Just add the below class to your assembly. Namespace does not matter.

public class DoNotVirtualizeAttribute : Attribute
{
}

So your class will look like this

[DoNotVirtualize]
public class ClassToSkip
{
    ...
}

Include or exclude namespaces

These config options are access by modifying the EmptyConstructor node in FodyWeavers.xml

Visibility

The visibility to use when injecting constructors.

Can not be defined with Visibility.

Allowed values: public or family (aka protected)

Defaults to public.

For example

<EmptyConstructor Visibility='family'/>

Making Existing Empty Constructors Visible

Optionally the visibility of already existing constructors can be increased. If this feature is enabled, the visibility of empty-constructors of non-abstract types will be increased to the same visibility as defined by the Visibility configuration (see above).

For example

<EmptyConstructor MakeExistingEmptyConstructorsVisible='True'/>

Will ensure all constructors on non-abstract types will be public.

ExcludeNamespaces

A list of namespaces to exclude.

Can not be defined with IncludeNamespaces.

Can take two forms.

As an element with items delimited by a newline.

<EmptyConstructor>
    <ExcludeNamespaces>
        Foo
        Bar
    </ExcludeNamespaces>
</EmptyConstructor>

Or as a attribute with items delimited by a pipe |.

<EmptyConstructor ExcludeNamespaces='Foo|Bar'/>

IncludeNamespaces

A list of namespaces to include.

Can not be defined with ExcludeNamespaces.

Can take two forms.

As an element with items delimited by a newline.

<EmptyConstructor>
    <IncludeNamespaces>
        Foo
        Bar
    </IncludeNamespaces>
</EmptyConstructor>

Or as a attribute with items delimited by a pipe |.

<EmptyConstructor IncludeNamespaces='Foo|Bar'/>

Initializers Preservation

By default, the generated constructors remain empty. If you would like field & property initialization to be copied from an existing constructor enable this via the PreserveInitializers attribute.

<EmptyConstructor PreserveInitializers='true'/>

Example, without initializers preservation:

public class Foo
{
    private int someValue;
    private int otherValue;
        
    public Foo(int someValue)
    {
        this.someValue = someValue;
        otherValue = 17;
    }
        
    // generated constructor
    public Foo() { }
}

Example, with initializers preservation:

public class Foo
{
    private int someValue;
    private int otherValue;
        
    public Foo(int someValue)
    {
        this.someValue = someValue;
        otherValue = 17;
    }
        
    // generated constructor
    public Foo()
    {
        // note: this.someValue isn't set
        otherValue = 17;
    }
}

Icon

Icon courtesy of The Noun Project

About

Adds an empty constructor to classes even if you have a non-empty one defined.

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages