rscg_Interface_to_null_object by Andrei Ignat
NuGet / site data
Details
Info
Name: rscg_Interface_to_null_object
Interface to null object - common
Author: Andrei Ignat
NuGet: https://www.nuget.org/packages/rscg_Interface_to_null_object/
https://www.nuget.org/packages/rscg_Interface_to_null_object_common
You can find more details at https://github.com/ignatandrei/rscg_Interface_to_null_object/
Source: https://github.com/ignatandrei/rscg_Interface_to_null_object/
Original Readme
Interface to Null Object Pattern
Implementation of https://en.wikipedia.org/wiki/Null_object_pattern from interface
Installation
Add to your csproj file:
<ItemGroup>
<PackageReference Include="rscg_Interface_to_null_object" Version="2025.120.1832" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="rscg_Interface_to_null_object_common" Version="2025.120.1832" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
Or add the nuget packages rscg_Interface_to_null_object and rscg_Interface_to_null_object_common
Usage
[InterfaceToNullObject.ToNullObject]
public interface IEmployee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public IDepartment Department { get; set; }
public string GetFullName();
}
And then a C# class that implements the interface will be generated
public partial class Employee_null : global::IntegrationConsole.IEmployee
{
public virtual string FirstName { get; set; } = default(string);
public virtual string LastName { get; set; } = default(string);
public virtual IntegrationConsole.IDepartment Department { get; set; } = default(IntegrationConsole.IDepartment);
public virtual string GetFullName() { return default(string); }
}
About
Generate null objects for interfaces
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
- IEmployee.cs
- IDepartment.cs
This is the CSharp Project that references rscg_Interface_to_null_object
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="rscg_Interface_to_null_object" Version="2025.120.1832" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="rscg_Interface_to_null_object_common" Version="2025.120.1832" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of rscg_Interface_to_null_object in Program.cs
using NullInterface;
Console.WriteLine("Hello, World!");
Console.WriteLine("Hello, World!");
IDepartment department = new Department_null();
department.Name = "IT";
IEmployee employee = new Employee_null();
employee.FirstName = "Andrei";
employee.Department = department;
Console.WriteLine(employee.FirstName);
Console.WriteLine(employee.Department.Name);
This is the use of rscg_Interface_to_null_object in IEmployee.cs
using InterfaceToNullObject;
namespace NullInterface;
[ToNullObject]
public interface IEmployee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public IDepartment Department { get; set; }
public string GetFullName();
public string GetFullNameAndDepartment(string separator);
public bool MoveEmployeeToDepartment(IDepartment department);
}
This is the use of rscg_Interface_to_null_object in IDepartment.cs
namespace NullInterface;
using InterfaceToNullObject;
[ToNullObject]
public interface IDepartment
{
public string Name { get; set; }
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- AutoInterfaceAttribute.g.cs
- AutoInterfaceVisibilityInternalAttribute.g.cs
- AutoInterfaceVisibilityPrivateProtectedAttribute.g.cs
- AutoInterfaceVisibilityProtectedAttribute.g.cs
- AutoInterfaceVisibilityProtectedInternalAttribute.g.cs
- AutoInterfaceVisibilityPublicAttribute.g.cs
- IDepartment_NullInterface.Department_IDepartment.cs.g.cs
- IEmployee_NullInterface.Employee_Employee.cs.g.cs
- IgnoreAutoInterfaceAttribute.g.cs
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
#if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES
using System;
namespace AutoInterfaceAttributes;
/// <summary>
/// Generates an interface for the decorated class/struct.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
[System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")]
internal sealed class AutoInterfaceAttribute : Attribute {
/// <summary>
/// <para>The name of the generated interface.</para>
/// <para>Default is "I{ClassName}"</para>
/// </summary>
public string Name { get; init; }
/// <summary>
/// <para>The modifier(s) for the interface.</para>
/// <para>Deault is "public partial"</para>
/// </summary>
public string Modifier { get; init; }
/// <summary>
/// <para>The namespace declaration for the interface.</para>
/// <para>If empty string, no namespace directive will be used (global namespace).<br />
/// Default (if not present) it will be mapped to the same namespace as the namespace of the class/struct.</para>
/// </summary>
public string Namespace { get; init; }
/// <summary>
/// <para>interface inheritance: Name(s) of interfaces this interface will inherit.</para>
/// <para>Default is Array.Empty</para>
/// </summary>
public Type[] Inheritance { get; init; }
/// <summary>
/// <para>
/// The Classes, structs or interfaces containing the generated interface.<br />
/// e.g. ["public sealed partial class Example"] will wrap the interface with that expression.
/// </para>
/// <para>Default is Array.Empty</para>
/// </summary>
public string[] Nested { get; init; }
/// <summary>
/// <para>If enabled, static members get accepted and are generating "static abstract" members.</para>
/// <para>Default is false</para>
/// </summary>
public bool StaticMembers { get; init; }
}
#endif
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
#if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES
using System;
namespace AutoInterfaceAttributes;
/// <summary>
/// Adds a "internal" access modifier to the interface member.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)]
[System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")]
internal sealed class AutoInterfaceVisibilityInternal : Attribute { }
#endif
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
#if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES
using System;
namespace AutoInterfaceAttributes;
/// <summary>
/// Adds a "private protected" access modifier to the interface member.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)]
[System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")]
internal sealed class AutoInterfaceVisibilityPrivateProtected : Attribute { }
#endif
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
#if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES
using System;
namespace AutoInterfaceAttributes;
/// <summary>
/// Adds a "protected" access modifier to the interface member.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)]
[System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")]
internal sealed class AutoInterfaceVisibilityProtected : Attribute { }
#endif
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
#if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES
using System;
namespace AutoInterfaceAttributes;
/// <summary>
/// Adds a "protected internal" access modifier to the interface member.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)]
[System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")]
internal sealed class AutoInterfaceVisibilityProtectedInternal : Attribute { }
#endif
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
#if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES
using System;
namespace AutoInterfaceAttributes;
/// <summary>
/// Adds a "public" access modifier to the interface member.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)]
[System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")]
internal sealed class AutoInterfaceVisibilityPublic : Attribute { }
#endif
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
namespace NullInterface;
public partial interface IDepartment {
string Name { get; set; }
}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
using AutoInterfaceAttributes;
namespace NullInterface;
public partial interface IEmployee {
string FirstName { get; set; }
string LastName { get; set; }
IDepartment Department { get; set; }
string GetFullName();
string GetFullNameAndDepartment(string separator);
}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
#if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES
using System;
namespace AutoInterfaceAttributes;
/// <summary>
/// The decorated member will be Ignored by the generator.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)]
[System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")]
internal sealed class IgnoreAutoInterfaceAttribute : Attribute { }
#endif
Useful
Download Example (.NET C#)
Share rscg_Interface_to_null_object
https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg_Interface_to_null_object
aaa
Category "Interface" has the following generators:
3 CopyCat
9 ProxyGen
12 rscg_Interface_to_null_object
13 RSCG_Static