Publicizer 2.0.1

dotnet add package Publicizer --version 2.0.1
NuGet\Install-Package Publicizer -Version 2.0.1
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="Publicizer" Version="2.0.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Publicizer --version 2.0.1
#r "nuget: Publicizer, 2.0.1"
#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 Publicizer as a Cake Addin
#addin nuget:?package=Publicizer&version=2.0.1

// Install Publicizer as a Cake Tool
#tool nuget:?package=Publicizer&version=2.0.1

Publicizer

NuGet NuGet Build License

Summary

What does Publicizer do?

Publicizer is a source generator that let you access the private members (fields, properties and methods) of a type from outside with compile-time safety in a typesafe manner.

What happens during compile time?

The private members of the type is accessed through a proxy type containing generated public members which forward to the private members of the original type. Thus, it provides a typesafe way to access the private members. If the name or type of a private member of the original type changes, so does the name or type of the generated public member in the proxy type. Therefore, any name or type mismatches emerge during compile time. No more NullReferenceExceptions or InvalidCastExceptions during runtime!

What happens during runtime?

During runtime the actual implementation of forwarding uses compiled expression trees, thus providing very fast forwarding performance which is almost as fast as accessing directly the original members. Alternatively, any other custom mechanism (including a slower, reflection based mechanism) can be used to access the private members.

Extra features

By changing the default AccessorHandling, even readonly fields and readonly auto-implemented properties (having only a getter) can be writable through the proxy type.

Similarly, by changing the default AccessorHandling, writeonly auto-implemented properties (having only a setter) can be readable through the proxy.

Development dependency

Publicizer nuget package is a development dependency, which means that your project only uses it during compile time, but it does not get deployed with your application, i.e. it is not used during runtime.

Rather, Publicizer's two components: annotation (containing attributes and other primitive types) and runtime (which is being used by the code generated by the source generator) are being included into your project as source files.

Example

The original type

Let assume the following type with private members:

public class TypeWithPrivateMembers
{
    private static int StaticField = 3;
    private static int StaticProperty { get; set; } = 8;

    private int _field; = 30;
    private int _property { get; set; } = 80;

    private static void StaticProcedure(int a)
    {
        Console.WriteLine($"{nameof(StaticProcedure)}(int a)");
    }

    private static string StaticFunction(int a)
    {
        Console.WriteLine($"{nameof(StaticFunction)}(int a)");
        return "hello";
    }

    private string Function(int a)
    {
        Console.WriteLine($"{nameof(Function)}(int a)");
        return a.ToString();
    }

    private void Procedure(int a)
    {
        Console.WriteLine($"{nameof(Procedure)}(int a)");
    }
}

The proxy type

In order to access the private members, you need to create a partial proxy type (a class, struct or record, with an arbitrary name), decorate it with the Publicize attribute and refer to the original type:

[Publicize(typeof(TypeWithPrivateMembers))]
public partial class Proxy
{
}

Usage

Accessing instance members

To access the instance members of the original type through the proxy type, the original type needs to be instantiated, as well as the proxy type needs to be instantiated with that instance of the original type:

var instance = new TypeWithPrivateMembers();

var proxy = new Proxy(instance);

proxy._field = 38;
proxy._field++;
Console.WriteLine($"_field = {proxy._field}");

proxy._property = 42;
proxy._property++;
Console.WriteLine($"_property = {proxy._property}");

Console.WriteLine(proxy.Function(15));

proxy.Procedure(18);
Accessing static members

To access the static members of the original type through the proxy type, the static members of the proxy type can be used:

Proxy.StaticField = 38;
Proxy.StaticField++;
Console.WriteLine($"StaticField = {Proxy.StaticField}");

Proxy.StaticProperty = 42;
Proxy.StaticProperty++;
Console.WriteLine($"StaticProperty = {Proxy.StaticProperty}");

Console.WriteLine(Proxy.StaticFunction(15));

Proxy.StaticProcedure(18);

The generated code

Behind the scenes, Publicizer's source generator will generate the corresponding public members into the proxy type with the proper forwarding code as implementation.

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.
  • .NETStandard 2.0

    • No dependencies.

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
2.0.1 360 11/24/2022
2.0.0 333 11/23/2022
1.0.3 371 10/5/2022
1.0.2 381 10/5/2022
1.0.1 404 10/1/2022
1.0.0 398 10/1/2022