.netreflectioncastlecastle-dynamicproxy

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


Solution

  • so far i have this fugly code

        private static bool IsDynamic(Type type)
        {
            try
            {
                var location = type.Assembly.Location;
                return false;
            }
            catch (NotSupportedException)
            {
                return true;
            }
        }