Generates proxy classes from classes and interfaces.
[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.
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>();
}
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
.
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.
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 {}
- 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.
}