1

Closed

ArgumentNullException when serializing a dynamic proxy without a default constructor

description

I have a problem serializing a proxy of type Castle DynamicProxy when the object doesn't have a default constructor.

The exception is ArgumentNullException "Value cannot be null"

Here a code to simulate the problem:
// nuget packages: 
// install-package Castle.Core
// install-package Newtonsoft.Json

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var proxyGenerator = new Castle.DynamicProxy.ProxyGenerator();
            var entity = proxyGenerator.CreateClassProxy(typeof(Article), new object[]{0}, new TestInterceptor()) as Article; ;
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(entity);
        }
    }

    public class Article
    {
        public Article(int id)
        {
            Id = id;
        }

        public int Id { get; set; }
    }

    class TestInterceptor : Castle.DynamicProxy.IInterceptor
    {
        public void Intercept(Castle.DynamicProxy.IInvocation invocation)
        {
        }
    }   
}
The strange thing is that if the object has a default constructor or if I don't use DynamicProxy the serialization is correct.

It is a bug?

thanks
Closed Oct 3, 2013 at 9:58 AM by JamesNK
Fixed. Note that you'll still get proxy properties being serialized, this isn't a bug.

comments