Code Monkey home page Code Monkey logo

publicizer's Introduction

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.

publicizer's People

Contributors

davidnemeti avatar

Watchers

 avatar  avatar

publicizer's Issues

Ability to publicize private types if they can be reached through a private property path of another public type

E.g. here we want to publicize Bar and Moo:

public class Foo
{
    private Bar Prop1 { get; set; }
    private Baz Prop2 { get; set; }
}

internal class Bar
{
    private int Prop3 { get; set; }
}

internal class Baz
{
    private Moo Prop4 { get; set; }
}

internal class Moo
{
    private int Prop5 { get; set; }
}

but they are inaccessible in another assembly, so we need to publicize them through Foo (note that Baz is also inaccessible):

[Publicize(typeof(Foo), Path: "Prop1")]
public partial class ProxyForFoo
{
}

[Publicize(typeof(Foo), Path: "Prop2.Prop4")]
public partial class ProxyForMoo
{
}

Proxy code generation improvements, bugfixes

  • should not generate forwarding for constructors
  • should not generate visibility modifier, so users can define it freely
  • should consider whether the proxy type is a class or struct

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.