Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialising substitutes #48

Open
dtchepak opened this issue May 7, 2011 · 2 comments
Open

Serialising substitutes #48

dtchepak opened this issue May 7, 2011 · 2 comments

Comments

@dtchepak
Copy link
Member

dtchepak commented May 7, 2011

Reported on the mailing list.

using NSubstitute;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace SerializeNSubstitute
{
       class Program
       {
               static void Main(string[] args)
               {
                       var root = Substitute.For<Root>();
                       var formatter = new BinaryFormatter();
                       Root newRoot = null;

                       using(var stream = new MemoryStream())
                       {
                               formatter.Serialize(stream, root);
                               stream.Position = 0;
                               newRoot = formatter.Deserialize(stream) as Root;
                       }
               }
       }

       [Serializable]
       public abstract class Root
       {
               public abstract string Data { get; set; }
       }
}

When I run this, I get the following error (happens at the Serialize()
call):

System.MethodAccessException was unhandled
Message=Attempt by method
'Castle.Proxies.RootProxy.GetObjectData(System.Runtime.Serialization.SerializationInfo,
System.Runtime.Serialization.StreamingContext)' to access method
'Castle.DynamicProxy.Generators.Emitters.TypeUtil.Sort(System.Reflection.MemberInfo[])'
failed.
Source=DynamicProxyGenAssembly2
StackTrace:
at Castle.Proxies.RootProxy.GetObjectData(SerializationInfo ,
StreamingContext )
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object
obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter,
ObjectWriter objectWriter, SerializationBinder binder)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object
obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter,
ObjectWriter objectWriter, SerializationBinder binder)
at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object
graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream
serializationStream, Object graph, Header[] headers, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream
serializationStream, Object graph)
at SerializeNSubstitute.Program.Main(String[] args) in G:
\JasonBock\Personal.NET Projects\SerializeNSubstitute\Program.cs:line
18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

I looked at the embedded Castle.DynamicProxy stuff in the NSubstitute
DLL and the Sort() method exists, but this is occuring in the proxy
itself, so I'm not sure what's going on there.

@kkozmic
Copy link

kkozmic commented May 8, 2011

can that be reproduced using just DP?

@dtchepak
Copy link
Member Author

dtchepak commented May 8, 2011

Confirmed this is not a DP problem. Following code runs fine using DynamicProxy directly:

    public class Class1 {
        [Test]
        public void TestName() {
            var gen = new ProxyGenerator();
            var root = gen.CreateClassProxy<Root>(new DoNothingInterceptor());

            var formatter = new BinaryFormatter();
            Root newRoot = null;

            using (var stream = new MemoryStream()) {
                formatter.Serialize(stream, root);
                stream.Position = 0;
                newRoot = formatter.Deserialize(stream) as Root;
            }
        }
    }

    [Serializable]
    public class DoNothingInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { } }

    [Serializable]
    public abstract class Root { public abstract string Data { get; set; } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants