Aresak.Interfacify 1.1.2

dotnet add package Aresak.Interfacify --version 1.1.2
NuGet\Install-Package Aresak.Interfacify -Version 1.1.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Aresak.Interfacify" Version="1.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Aresak.Interfacify --version 1.1.2
#r "nuget: Aresak.Interfacify, 1.1.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Aresak.Interfacify as a Cake Addin
#addin nuget:?package=Aresak.Interfacify&version=1.1.2

// Install Aresak.Interfacify as a Cake Tool
#tool nuget:?package=Aresak.Interfacify&version=1.1.2

Interfacify

Automates the generation of interface-compliant classes or missing properties in a class.

NuGet

Installation

Install the package from NuGet:

Install-Package Aresak.Interfacify

Usage

Add using statement to the top of the file:

using Aresak.Interfacify;

Design the interface, for our examples it looks like this:

public interface IMyInterface
{
	string MyProperty { get; set; }

	string MyOtherProperty { get; set; }
}

Auto-generated class from an interface

If you wish to automatically generate a class that implements the interface, you can use the InterfacifyAttribute:

[Interfacify]
public interface IMyInterface
{
	string MyProperty { get; set; }

	string MyOtherProperty { get; set; }
}

The following code will be generated:

public partial class MyInterface : IMyInterface
{
	public string MyProperty { get; set; }

	public string MyOtherProperty { get; set; }
}

It will create a new class with the same name as the interface, but without the I prefix. In case, your interface doesn't begin with an I, the class will have a suffix Class.

Examples:

  • The interface IMyInterface will generate a class MyInterface.
  • The interface MyInterface will generate a class MyInterfaceClass.

Generating missing properties to a class that implements the interface

If you wish to only auto-generate missing interface properties on a class, you can use the InterfacifyAttribute. That way if you want to define custom property definitions, you can do so.

Create a class that implements the interface and add [Interfacify] attribute:

[Interfacify)]
public partial class MyClass : IMyInterface
{
	public string MyProperty { get; set; }
}

The following code will be generated:


public partial class MyClass : IMyInterface
{
	public string MyProperty { get; set; }

	public string MyOtherProperty { get; set; }
}

Specifying a generator template

In case there is needed to have a custom template for the generated code, it can be specified by using the Template parameter of the InterfacifyAttribute:

[Interfacify(Template.NotifyPropertyChanged)]
public partial class MyClass : IMyInterface
{
// ...

The following templates are available:

The code snippets below are generated examples.

0. Basic

Default template. Creates a basic implementation of the interface.

With the specified rules:

  • For property with only getter: string MyProperty { get; }
  • For property with getter and setter: string MyProperty { get; set; }
  • For property with neither getter or setter: string MyProperty;
  • Access modifiers can be used
[Interfacify(Template.Basic)]
public partial class MyClass : IMyInterface
{
	public string MyProperty { get; set; }

	public string MyOtherProperty { get; set; }
}
1. NotifyPropertyChanged

Creates a basic implementation of the interface, but also implements INotifyPropertyChanged and raises the PropertyChanged event when a property is changed.

With the specified rules:

  • For property with only getter: string MyProperty { get; }
  • For other properties it has { get; set; } and also raises the PropertyChanged event
  • Access modifiers can be used
[Interfacify(Template.NotifyPropertyChanged)]
public partial class MyClass : IMyInterface
{
	// Generated required code
	public event PropertyChangedEventHandler PropertyChanged;

	private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged == null)
        {
            return;
        }

        PropertyChangedEventArgs arguments = new PropertyChangedEventArgs(propertyName);
        PropertyChanged.Invoke(this, arguments);
    }

	// Generated properties
	private string _generated_MyProperty;
	public string MyProperty { 
		get => _generated_MyProperty;
		set {
			_generated_MyProperty = value;
			NotifyPropertyChanged();
		}
	}

	private string _generated_MyOtherProperty;
	public string MyOtherProperty { 
		get => _generated_MyOtherProperty;
		set {
			_generated_MyOtherProperty = value;
			NotifyPropertyChanged();
		}
	}
}

Common issues

My IDE is giving me errors that my class does not implement the interface

If you are sure that you have assigned the attribute to the class correctly, clean and rebuild the solution. If that does not help, try to restart the IDE. In worst scenarios, the IDE must be restarted multiple times.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.2 415 5/21/2024
1.1.1 79 5/21/2024
1.1.0 77 5/21/2024
1.0.2 739 3/1/2024
1.0.1 1,456 1/5/2024