Giter Site home page Giter Site logo

proxysourcegenerator's Introduction

ProxySourceGenerator

Nuget

Generates proxy classes from classes and interfaces.

Usage

  • Add nuget package into your project Nuget
  • Decorate a class or interface with [GenerateProxy] attribute:

Autogenerating the proxy interface

[GenerateProxy]
public class TestClass: ITestClass //ITestClass is autogenerated with non-private members
{
    public string AProperty { get; set; }
    public int APropertyAsInt()
    {
        return Convert.ToInt32(AProperty);
    }
}
  • This generates proxy class and corresponding ITestClass interface:
//Autogenerated
public interface ITestClass
{
    public string AProperty { get; set; }
    public int APropertyAsInt();
}
  • Create your proxy:
//First create an object
var testObj = new TestClass();
//Directly call constructor of the proxy
var proxy = new TestClassProxy(testObj);
//Or call the generic accessor
var proxy = ProxyAccessor<ITestClass>.Create(testObj);
  • Intercept methods or properties:
proxy.InterceptMethod = (string methodName,
    InterceptMethodCallerHandler method,
    Dictionary<string, object> parameters) =>
{
    Console.WriteLine("method called: " + methodName + string.Join(",",parameters.Select(kv => (kv.Key, kv.Value).ToString())));
    return method(parameters);
};

proxy.InterceptPropertySetter = (propertyName, setter, value) =>
{
    Console.WriteLine($"property set: {propertyName} with value '{value}'");
    setter(value);
};

proxy.InterceptPropertyGetter = (propertyName, getter) =>
{
    Console.WriteLine($"property get: {propertyName}");
    return getter();
};
  • You can access methods/properties with explicit casting or Access property:
var intVal = proxy.Access.APropertyAsInt();
//or 
var testObjProxied = (ITestClass)proxy; //this also proxied.
  • You can access the underlying object too:
var testObjUnderlying = proxy.UnderlyingObject; //this is not proxied, returns original object.

Using the existing interface

If the interface already exists and does not want to be generated automatically, you can decorate the interface with [GenerateProxy] to proxy all calls to the interface.

[GenerateProxy]
public interface ITestClass
{
    public string AProperty { get; set; }
    public T AGenericMethod<T>();
}

Genereate from base class

You can generate interfaces and its proxy classes from all classes that derive from a base class:

[GenerateProxy(GenerateForDerived=true)]
public abstract ABaseClass //abstract is optional
{
    public void BaseMethod(){}
}

public class SuperClass: ABaseClass
{
}

This code generates an interface named ISuperClass and a proxy named SuperClassProxy which contains BaseMethod from ABaseClass.

Generate without interface

Normally the proxy pattern implementation in c# uses interfaces to intercept methods and properties. But there is another aproach uses overriding virtual members of the underlying class. To use this aproach, you can set UseInterface argument to false.

[GenerateProxy(UseInterface=false)]
public class AClass
{
    public virtual int AProperty { get; }
    public virtual string AMethod(int parameter){}
    
    public void NotProxiedMethod(){}// not proxied because it isn't virtual.
}

This code generates a proxy class derived from AClass, and it overrides all virtual members to intercept calls. It skips non-virtual members of the class. Also this method doesn't generates an interface form AClass. You can combine both UseInterface=false with GenerateForDerived=true for generating all subclasses without interface method.

Change accessibility of proxy class

The generated proxy classes and interfaces are internal and partial types, so you can change accessibility by defining another partial class:

public partial class TestClassProxy
{
}

public partial interface ITestClass {}

Notes

  • Can't generate proxy from sealed classes with UseInterface=false.
[GenerateProxy(UseInterface=false)]
public sealed class ASealedClass //NonSense, can't be derived from this clas because of sealed.
{
}
  • Can't generate proxy members/accessors from private members/accessors.
[GenerateProxy]
public class AClass
{
    private string PrivateProp { get;set; } //not generated
    public int PublicProp { get; private set; } //generates only for the getter.
}

proxysourcegenerator's People

Contributors

oruchreis avatar

Watchers

 avatar  avatar

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.