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

5

7

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

Nolte answered 28/7, 2009 at 11:1 Comment(0)
N
0

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;
        }
    }
Nolte answered 28/7, 2009 at 13:43 Comment(1)
As Ayende pointed out groups.google.com/group/castle-project-users/browse_thread/… Location throwing an exception is a side effect. the same would happen if you use Assembly.Load(File.ReadAllBytes("Nhibernate.dll"));Nolte
T
10
type.Assembly.FullName.StartsWith("DynamicProxyGenAssembly2")
Tradition answered 29/7, 2009 at 0:25 Comment(5)
need something not specific to castleNolte
You want to detect if type is a DynamicProxy proxy. How is that NOT specific to Castle?Ollie
I am using DynamicProxy as an example so people can easily know what i am talking about. But i am looking for code that will tell me if it is a runtime generated type.Nolte
there is no single solution for that. I'm afraid. You can generate type, save the assembly you generated it to to file. How would you know if it was generated, or was there from the beginning? Why do you need that at all?Ollie
@JeffN825: that's what I used... :)Timms
R
3

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);
Regenaregency answered 31/8, 2009 at 20:49 Comment(0)
N
0

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;
        }
    }
Nolte answered 28/7, 2009 at 13:43 Comment(1)
As Ayende pointed out groups.google.com/group/castle-project-users/browse_thread/… Location throwing an exception is a side effect. the same would happen if you use Assembly.Load(File.ReadAllBytes("Nhibernate.dll"));Nolte
A
0

This seems to be working for Castle:

private static bool IsDynamic(Type type)
{
    return type.Namespace == null;
}
Adjoin answered 28/7, 2009 at 23:36 Comment(2)
But that is specific to the castle implementation. Might not be true for other proxy generators :(Nolte
This won't work with the trunk and the upcoming v2.2 because proxies now Do have a namespaceOllie
D
0

A decade and a half later... You can test if the assembly is dynamic like the example below. Apparently this has been in the framework since .Net Framework 4.0

if (type.Assembly.IsDynamic)
{
    ...
}
Decrial answered 5/10, 2024 at 6:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.