The mixture of projects with different Postsharp version caused InvalidCastException: Object cannot be stored in an array of this type.

michael.freidgeim's Avatar

michael.freidgeim

23 Apr, 2016 01:40 AM

I want to inform you about the following scenario. Could you please avoid the error or at least provide better error messages?

When running unit tests, I've got for most tests the same error:
System.InvalidCastException: Object cannot be stored in an array of this type
Result Message:
Test method UnitTests.API.Helpers.Add_Remove_Add_SeatAsTaxItem_SeatAdded threw exception:
System.TypeInitializationException: The type initializer for 'myClass' threw an exception. ---> System.TypeInitializationException: The type initializer for 'PostSharp.ImplementationDetails_58cc4e55.<>z__a_a' threw an exception. ---> System.TypeInitializationException: The type initializer for 'PostSharp.ImplementationDetails_58cc4e55.<>z__a_18' threw an exception. ---> System.InvalidCastException: Object cannot be stored in an array of this type.

Result StackTrace:
at System.Array.InternalSetValue(Void* target, Object value)
   at System.Array.SetValue(Object value, Int32[] indices)
   at System.Runtime.Serialization.ObjectManager.CompleteObject(ObjectHolder holder, Boolean bObjectFullyComplete)
   at System.Runtime.Serialization.ObjectManager.DoNewlyRegisteredObjectFixups(ObjectHolder holder)
   at System.Runtime.Serialization.ObjectManager.RegisterObject(Object obj, Int64 objectID, SerializationInfo info, Int64 idOfContainingObj, MemberInfo member, Int32[] arrayIndex)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.RegisterObject(Object obj, ParseRecord pr, ParseRecord objectPr, Boolean bIsString)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseObjectEnd(ParseRecord pr)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at PostSharp.Aspects.Serialization.BinaryAspectSerializer.Deserialize(Stream stream, IMetadataDispenser metadataDispenser)
   at PostSharp.Aspects.Serialization.AspectSerializer.Deserialize(Assembly assembly, String resourceName, IMetadataDispenser metadataDispenser)
   at PostSharp.ImplementationDetails_58cc4e55.<>z__a_18..cctor() in :line 0
 --- End of inner exception stack trace ---
    at PostSharp.ImplementationDetails_58cc4e55.<>z__a_a..cctor() in :line 0
 --- End of inner exception stack trace ---
    at PostSharp.ImplementationDetails_58cc4e55.<>z__a_a.Initialize()
   at .myclass..cctor() in :line 0
 --- End of inner exception stack trace ---

Note, that the main application worked fine.
Thanks to http://support.sharpcrafters.com/discussions/problems/1844-unit-tests-failing-when-using-postsharp, I realized, that the problem could be caused by the mixture of projects with postsharp 4.2.21 and 4.2.22 included.
I've tried to set in unit tests project app.config
      <dependentAssembly>
        <assemblyIdentity name="PostSharp" publicKeyToken="b13fd38b8f9c99d7" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.22.0" newVersion="4.2.22.0" />
      </dependentAssembly>
But it didn't help.
However proper upgrade of nuget packages to the latest postsharp 4.2.22 fixed the problem.
Note that compiler showed the warning
C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1819,5): warning MSB3277: Found conflicts between different versions of the same dependent assembly that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed.
But I didn't pay attention to it, in particular because the name of assembly wasn't shown.

  1. Support Staff 1 Posted by PostSharp Techn... on 26 Apr, 2016 09:45 AM

    PostSharp Technologies's Avatar

    Hello,

    is it possible to provide us with assemblies (and better their source) on which this did happen? If not, could you at least point us on the structure of aspects you are using (aspect types, advices, their relations, etc.).

    Thanks a lot for further information.

    Best regards,
    Daniel

  2. Support Staff 2 Posted by PostSharp Techn... on 10 May, 2016 07:16 AM

    PostSharp Technologies's Avatar

    We're closing this request because you have not replied for a few days. Feel free to reopen the discussion if you need more help.

  3. PostSharp Technologies closed this discussion on 10 May, 2016 07:16 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac