动态生成代理对象(以编程方式生成从给定对象派生的类并覆盖单个方法)
本文关键字:对象 派生 单个 方法 覆盖 代理 编程 动态 方式生 | 更新日期: 2023-09-27 18:07:14
我想创建一个方法,做以下事情:
- 将任意实例作为参数
- 生成一个包装器实例,以与传递的实例相同的方式提供所有属性和方法
- 用不同的实现重写一个方法
- 返回生成的实例
这与orm创建的代理对象非常相似。它们通常不返回真正的模型类,而是返回行为方式相同的代理对象,除了延迟加载等。
有合适的吗?(我看到了CodeDom,但也看到了我需要为方法实现发出的操作码…)
感谢所有的提示和链接。Castle Project的DynamicProxy (http://www.castleproject.org/projects/dynamicproxy/)为我完成了这项工作。
代理生成器只覆盖单个方法(在本例中为GetHashCode()),这很容易完成:
/// <summary>
/// A class that is capable to wrap arbitrary objects and "override" method GetHashCode()
/// This is suitable for object that need to be passed to any WPF UI classes using them in
/// a hashed list, set of other collection using hash codes.
/// </summary>
public class CustomProxyFactory
{
/// <summary>
/// Interceptor class that stores a static hash code, "overrides" the
/// method GetHashCode() and returns this static hash code instead of the real one
/// </summary>
public class HashCodeInterceptor : IInterceptor
{
private readonly int frozenHashCode;
public HashCodeInterceptor( int frozenHashCode )
{
this.frozenHashCode = frozenHashCode;
}
public void Intercept( IInvocation invocation )
{
if (invocation.Method.Name.Equals( "GetHashCode" ) == true)
{
invocation.ReturnValue = this.frozenHashCode;
}
else
{
invocation.Proceed();
}
}
}
/// <summary>
/// Factory method
/// </summary>
/// <param name="instance">Instance to be wrapped by a proxy</param>
/// <returns></returns>
public static T Create<T>( T instance ) where T : class
{
try
{
IInterceptor hashCodeInterceptor = new HashCodeInterceptor( instance.GetHashCode() );
IInterceptor[] interceptors = new IInterceptor[] {hashCodeInterceptor};
ProxyGenerator proxyGenerator = new ProxyGenerator();
T proxy = proxyGenerator.CreateClassProxyWithTarget( instance, interceptors );
return proxy;
}
catch (Exception ex)
{
Console.WriteLine( typeof(CustomProxyFactory).Name + ": Exception during proxy generation: " + ex );
return default(T);
}
}
}