ProxySourceGenerator 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ProxySourceGenerator --version 1.0.0                
NuGet\Install-Package ProxySourceGenerator -Version 1.0.0                
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="ProxySourceGenerator" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ProxySourceGenerator --version 1.0.0                
#r "nuget: ProxySourceGenerator, 1.0.0"                
#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 ProxySourceGenerator as a Cake Addin
#addin nuget:?package=ProxySourceGenerator&version=1.0.0

// Install ProxySourceGenerator as a Cake Tool
#tool nuget:?package=ProxySourceGenerator&version=1.0.0                

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();
  • Intercept methods or properties:
proxy.InterceptMethod = (string methodName,
    InterceptMethodCallerHandler method,
    Dictionary<string, object> parameters) =>
{
    Console.WriteLine("method: " + methodName + " parameters: " + string.Join(",", parameters.Select(kv => (kv.Key, kv.Value).ToString())));
    return method(parameters);
};
  • 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 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; } //doesn't generates
    public int PublicProp { get; private set; } //generates only for the getter.
}
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.

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
1.0.9 382 1/19/2024
1.0.8 988 12/19/2023
1.0.7 115 12/19/2023
1.0.6 119 12/19/2023
1.0.5 126 12/19/2023
1.0.4 118 12/19/2023
1.0.3 130 12/19/2023
1.0.2 123 12/19/2023
1.0.1 125 12/18/2023
1.0.0 123 12/18/2023