ProxySourceGenerator 1.0.2
See the version list below for details.
dotnet add package ProxySourceGenerator --version 1.0.2
NuGet\Install-Package ProxySourceGenerator -Version 1.0.2
<PackageReference Include="ProxySourceGenerator" Version="1.0.2" />
paket add ProxySourceGenerator --version 1.0.2
#r "nuget: ProxySourceGenerator, 1.0.2"
// Install ProxySourceGenerator as a Cake Addin #addin nuget:?package=ProxySourceGenerator&version=1.0.2 // Install ProxySourceGenerator as a Cake Tool #tool nuget:?package=ProxySourceGenerator&version=1.0.2
ProxySourceGenerator
Generates proxy classes from classes and interfaces.
Usage
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 withUseInterface=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.
}
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.