Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a Type is a generated DynamicProxy without referencing Castle DynamicProxy?

I am using castle DynamicProxy and was wondering if there is a way of detecting if a Type is a proxy without referencing Castle DynamicProxy?

So while I am using Castle DynamicProxy as an example I would like code that would work for any in memory generated type.

var generator = new ProxyGenerator();

var classProxy = generator.CreateClassProxy<Hashtable>();
Debug.WriteLine(classProxy.GetType().Is....);

var interfaceProxy = generator.CreateInterfaceProxyWithoutTarget<ICollection>();
Debug.WriteLine(interfaceProxy.GetType().Is....);

Thanks

like image 794
Simon Avatar asked Jul 28 '09 11:07

Simon


2 Answers

type.Assembly.FullName.StartsWith("DynamicProxyGenAssembly2")
like image 110
Ayende Rahien Avatar answered Nov 19 '22 04:11

Ayende Rahien


You could make your dynamic type implements a specific interface:

public interface IDynamicProxy { }

...

ProxyGenerator generator = new ProxyGenerator();

var classProxy = generator.CreateClassProxy(typeof(Hashtable), new[] {typeof(IDynamicProxy)});
Debug.WriteLine(classProxy is IDynamicProxy);


var interfaceProxy = generator.CreateInterfaceProxyWithoutTarget(typeof(ICollection), new[] { typeof(IDynamicProxy) });
Debug.WriteLine(interfaceProxy is IDynamicProxy);
like image 43
Jeff Cyr Avatar answered Nov 19 '22 04:11

Jeff Cyr