Publicizer 1.0.1
See the version list below for details.
dotnet add package Publicizer --version 1.0.1
NuGet\Install-Package Publicizer -Version 1.0.1
<PackageReference Include="Publicizer" Version="1.0.1" />
paket add Publicizer --version 1.0.1
#r "nuget: Publicizer, 1.0.1"
// Install Publicizer as a Cake Addin #addin nuget:?package=Publicizer&version=1.0.1 // Install Publicizer as a Cake Tool #tool nuget:?package=Publicizer&version=1.0.1
Publicizer
Summary
Publicizer is a source generator that let you access the private members of a class from outside in a typesafe manner.
The private members of the class is accessed through a proxy class which contains the generated public members which forward to the private members of the original class. Thus, it provides a typesafe way to access the private members. If the name or type of a private member of the original class changes, so does the name or type of the generated public member in the proxy class. Therefore, any name or type mismatches emerge during compile time. No more NullReferenceException
s or InvalidCastException
s!
During runtime the actual implementation of forwarding uses reflection (or any other custom mechanism) to access the private members.
Example
Let assume the following class with private members:
public class TypeWithPrivateMembers
{
#pragma warning disable CS0414 // The field 'TypeWithPrivateMembers.StaticField' is assigned but its value is never used
private static int StaticField = 3;
#pragma warning restore CS0414 // The field 'TypeWithPrivateMembers.StaticField' is assigned but its value is never used
private static int StaticReadonlyProperty { get; } = 5;
private static int StaticProperty { get; set; } = 8;
private OtherType _field = new OtherType(30);
private int _readonlyProperty { get; } = 50;
private int _property { get; set; } = 80;
private static void StaticProcedure()
{
Console.WriteLine($"{nameof(StaticProcedure)}()");
}
private static string StaticFunction()
{
Console.WriteLine($"{nameof(StaticFunction)}()");
return "hello";
}
private void Procedure()
{
Console.WriteLine($"{nameof(Procedure)}()");
}
private string Function()
{
Console.WriteLine($"{nameof(Function)}()");
return "hello";
}
private void Procedure(int a)
{
Console.WriteLine($"{nameof(Procedure)}(int a)");
}
private string Function(int a)
{
Console.WriteLine($"{nameof(Function)}(int a)");
return a.ToString();
}
private void Procedure(int a, OtherType otherType)
{
Console.WriteLine($"{nameof(Procedure)}(int a, OtherType otherType)");
}
private string Function(int a, OtherType otherType)
{
Console.WriteLine($"{nameof(Function)}(int a, OtherType otherType)");
return a.ToString();
}
}
In order to access the private members, you need to create a proxy class (with an arbitrary name), decorate it with the Publicize
attribute and assign the class with the private members:
[Publicize(typeof(TypeWithPrivateMembers))]
public partial class Proxy
{
}
Publicizer's source generator will generate the corresponding public members into the proxy class with the proper forwarding code as implementation:
public partial class Proxy
{
private static readonly global::Publicizer.IMemberAccessor<global::NamespaceForTypeWithPrivateMembers.TypeWithPrivateMembers> Proxy_IMemberAccessor = new global::Publicizer.ReflectionMemberAccessor<global::NamespaceForTypeWithPrivateMembers.TypeWithPrivateMembers>();
private readonly global::NamespaceForTypeWithPrivateMembers.TypeWithPrivateMembers Proxy_TypeWithPrivateMembers;
public Proxy(global::NamespaceForTypeWithPrivateMembers.TypeWithPrivateMembers instanceToPublicize)
{
this.Proxy_TypeWithPrivateMembers = instanceToPublicize;
}
public static int StaticField
{
get => Proxy_IMemberAccessor.GetFieldValue<int>(null, "StaticField", global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
set => Proxy_IMemberAccessor.SetFieldValue<int>(null, "StaticField", value, global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
}
public static int StaticReadonlyProperty
{
get => Proxy_IMemberAccessor.GetPropertyValue<int>(null, "StaticReadonlyProperty", global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
}
public static int StaticProperty
{
get => Proxy_IMemberAccessor.GetPropertyValue<int>(null, "StaticProperty", global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
set => Proxy_IMemberAccessor.SetPropertyValue<int>(null, "StaticProperty", value, global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
}
public global::NamespaceForOtherTypes.OtherType _field
{
get => Proxy_IMemberAccessor.GetFieldValue<global::NamespaceForOtherTypes.OtherType>(this.Proxy_TypeWithPrivateMembers, "_field", global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
set => Proxy_IMemberAccessor.SetFieldValue<global::NamespaceForOtherTypes.OtherType>(this.Proxy_TypeWithPrivateMembers, "_field", value, global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
}
public int _readonlyProperty
{
get => Proxy_IMemberAccessor.GetPropertyValue<int>(this.Proxy_TypeWithPrivateMembers, "_readonlyProperty", global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
}
public int _property
{
get => Proxy_IMemberAccessor.GetPropertyValue<int>(this.Proxy_TypeWithPrivateMembers, "_property", global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
set => Proxy_IMemberAccessor.SetPropertyValue<int>(this.Proxy_TypeWithPrivateMembers, "_property", value, global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
}
public static void StaticProcedure() =>
Proxy_IMemberAccessor.InvokeMethod(null, "StaticProcedure", new Type[] { }, new object[] { }, global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
public static string StaticFunction() =>
(string) Proxy_IMemberAccessor.InvokeMethod(null, "StaticFunction", new Type[] { }, new object[] { }, global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
public void Procedure() =>
Proxy_IMemberAccessor.InvokeMethod(this.Proxy_TypeWithPrivateMembers, "Procedure", new Type[] { }, new object[] { }, global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
public string Function() =>
(string) Proxy_IMemberAccessor.InvokeMethod(this.Proxy_TypeWithPrivateMembers, "Function", new Type[] { }, new object[] { }, global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
public void Procedure(int a) =>
Proxy_IMemberAccessor.InvokeMethod(this.Proxy_TypeWithPrivateMembers, "Procedure", new Type[] { typeof(int) }, new object[] { a }, global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
public string Function(int a) =>
(string) Proxy_IMemberAccessor.InvokeMethod(this.Proxy_TypeWithPrivateMembers, "Function", new Type[] { typeof(int) }, new object[] { a }, global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
public void Procedure(int a, global::NamespaceForOtherTypes.OtherType otherType) =>
Proxy_IMemberAccessor.InvokeMethod(this.Proxy_TypeWithPrivateMembers, "Procedure", new Type[] { typeof(int), typeof(global::NamespaceForOtherTypes.OtherType) }, new object[] { a, otherType }, global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
public string Function(int a, global::NamespaceForOtherTypes.OtherType otherType) =>
(string) Proxy_IMemberAccessor.InvokeMethod(this.Proxy_TypeWithPrivateMembers, "Function", new Type[] { typeof(int), typeof(global::NamespaceForOtherTypes.OtherType) }, new object[] { a, otherType }, global::Publicizer.MemberLifetime.All, global::Publicizer.MemberVisibility.All);
}
Product | Versions 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. |
-
.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.