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.
See also Fody usage.
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 <EmptyConstructor/>
to FodyWeavers.xml
<Weavers>
<EmptyConstructor/>
</Weavers>
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
{
...
}
These config options are access by modifying the EmptyConstructor
node in FodyWeavers.xml
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'/>
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
.
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'/>
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'/>
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 courtesy of The Noun Project