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

TypeLoadException when attempting serialization of value type defined in referenced .NET Framework class library #2981

Closed
zspitz opened this issue Jul 4, 2019 · 5 comments

Comments

@zspitz
Copy link

zspitz commented Jul 4, 2019

I have the following project dependency structure:

  • Entry (.NET Core 2.2)
    • Fx (.NET Framework 4.7.2 class library)

Fx contains two types:

  • struct EndNodeData
  • class TestClass

Serializing an instance of TestClass succeeds:

var stream = File.Create(Path.GetTempFileName());
var formatter = new BinaryFormatter();

var testClass = new TestClass() {
    Foo = "a",
    Bar = "b"
};
formatter.Serialize(stream, testClass);

but serializing a value of EndNodeType fails:

var endnodeData = new EndNodeData {
    Closure = null,
    Name = null,
    Type = "int",
    Value = "5"
};
formatter.Serialize(stream, endnodeData);

with the following exception:

System.TypeLoadException: 'Could not load type 'System.Runtime.CompilerServices.IsReadOnlyAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.'

Commenting out the string properties doesn't help.

Sample code

Related StackOverflow question


Running Windows 10 Home 1903 64-bit.
dotnet --version prints 2.2.300

Using these functions I get the following output:

.NET Framework Version: 4.8 or later

v2.0.50727  2.0.50727.4927  SP2
v3.0  3.0.30729.4926  SP2
v3.5  3.5.30729.4926  SP1
v4.0  
  Client  4.0.0.0
@zspitz zspitz changed the title TypeLoadException when attempting serialization of value type defined in referenced ,NET Framework class library TypeLoadException when attempting serialization of value type defined in referenced .NET Framework class library Jul 4, 2019
@0xd4d
Copy link

0xd4d commented Jul 4, 2019

System.Runtime.CompilerServices.IsReadOnlyAttribute doesn't exist in .NET Core 2.2's mscorlib.dll (as a type forwarder), but it does exist in .NET Framework's mscorlib (net48, don't know about earlier versions)

workarounds:

  • Don't use auto implemented properties: add your own backing fields or don't use properties, just public fields. The compiler adds [IsReadOnly] to the getters.
  • Define your own System.Runtime.CompilerServices.IsReadOnlyAttribute in the same assembly or some common assembly
	namespace System.Runtime.CompilerServices {
		[AttributeUsage(AttributeTargets.All, Inherited = false)]
		sealed class IsReadOnlyAttribute : Attribute {
			public IsReadOnlyAttribute() { }
		}
	}
  • Upgrade your project from netcoreapp2.2 to netcoreapp3.0

@zspitz
Copy link
Author

zspitz commented Jul 4, 2019

@0xd4d

Closing, as the workarounds are sufficient.

Why does this happen only on struct and not on a class with auto-implemented properties?

@zspitz zspitz closed this as completed Jul 4, 2019
@0xd4d
Copy link

0xd4d commented Jul 4, 2019

I think it's the new 'readonly member' feature in C#8, and only structs can have readonly members. Seems like you're compiling your code with the latest Roslyn compiler.

@zspitz
Copy link
Author

zspitz commented Jul 4, 2019

@0xd4d Even when I explicitly set the compiler version to 7.3, and to 7.0, in both the .NET Framework and .NET Core projects, I still get the same error, and not while serializing the class, only when serializing the struct.

@0xd4d
Copy link

0xd4d commented Jul 4, 2019

Yes, you could report it to roslyn, I'm not sure if it's a feature or a bug to add [IsReadOnly] to the getter. https://github.com/dotnet/roslyn/issues

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

1 participant