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
type.Assembly.FullName.StartsWith("DynamicProxyGenAssembly2")
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With