Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

protobuf-net mscorlib references cause warnings #2

Closed
ctaggart opened this issue Jun 7, 2014 · 3 comments
Closed

protobuf-net mscorlib references cause warnings #2

ctaggart opened this issue Jun 7, 2014 · 3 comments

Comments

@ctaggart
Copy link
Owner

ctaggart commented Jun 7, 2014

Froto.Roslyn is a .NET 4.5 project. Using the latest protobuf-net, version 2.0.0.668, for nuget, it adds the dll from net40. Unfortunately, it looks to be compiled for .NET 3.5.

image

Roslyn emit then complains about mscorlib 3.5 missing from my system.

If I switch to use the dll from the net20, emit is successful, but I get a bunch of warnings.

image

(85,20): warning CS1701: Assuming assembly reference 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' used by 'protobuf-net' matches identity 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' of 'mscorlib', you may need to supply runtime policy

How do I get rid of these warnings?

@ctaggart ctaggart changed the title protobuf-net mscorlib references cause warnings or errors protobuf-net mscorlib references cause warnings Jun 7, 2014
@ctaggart
Copy link
Owner Author

ctaggart commented Jun 7, 2014

protobuf-net 447: protobuf-net.2.0.0.668\lib\net40\protobuf-net.dll should target v4.0.30319, not v2.0.50727

@m0sa
Copy link

m0sa commented Nov 21, 2014

@ctaggart you can get rid of the warnings by using the DesktopAssemblyIdentityComparer.Default (an example of how to use it is in this LinqPad script which was used to get all the stuff below). The generated IL is different though:

public static class Program 
{
  public static void Main()
  {     
    using (var ms = new System.IO.MemoryStream())
    {
      ProtoBuf.Serializer.Serialize(ms, new {a = 1});
    }
  }
}

The csc.exe generates the following IL (irrelevant stuff omitted)

// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly extern 'protobuf-net'
{
  .publickeytoken = (25 7B 51 D8 7D 2E 4D 67 )                         // %{Q.}.Mg
  .ver 2:0:0:668
}
.assembly extern mscorlib as mscorlib_2
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 2:0:0:0
}
...
// =============== CLASS MEMBERS DECLARATION ===================
.class public abstract auto ansi sealed beforefieldinit Program
       extends [mscorlib]System.Object
{
  .method public hidebysig static void  Main() cil managed
  {
    // Code size       31 (0x1f)
    .maxstack  2
    .locals init (class [mscorlib]System.IO.MemoryStream V_0)
    IL_0000:  newobj     instance void [mscorlib]System.IO.MemoryStream::.ctor()
    IL_0005:  stloc.0
    .try
    {
      IL_0006:  ldloc.0
      IL_0007:  ldc.i4.1
      IL_0008:  newobj     instance void class '<>f__AnonymousType0`1'<int32>::.ctor(!0)
      IL_000d:  call       void ['protobuf-net']ProtoBuf.Serializer::Serialize<class '<>f__AnonymousType0`1'<int32>>(class [mscorlib_2]System.IO.Stream,
                                                                                                                     !!0)
      IL_0012:  leave.s    IL_001e

    }  // end .try
    finally
    {
      IL_0014:  ldloc.0
      IL_0015:  brfalse.s  IL_001d

      IL_0017:  ldloc.0
      IL_0018:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()
      IL_001d:  endfinally
    }  // end handler
    IL_001e:  ret
  } // end of method Program::Main

} // end of class Program

Roslyn generates the following IL, without the v2 mscorlib reference:

// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly extern 'protobuf-net'
{
  .publickeytoken = (25 7B 51 D8 7D 2E 4D 67 )                         // %{Q.}.Mg
  .ver 2:0:0:668
}
...
// =============== CLASS MEMBERS DECLARATION ===================
.class public abstract auto ansi sealed beforefieldinit Program
       extends [mscorlib]System.Object
{
  .method public hidebysig static void  Main() cil managed
  {
    // Code size       36 (0x24)
    .maxstack  2
    .locals init (class [mscorlib]System.IO.MemoryStream V_0)
    IL_0000:  nop
    IL_0001:  newobj     instance void [mscorlib]System.IO.MemoryStream::.ctor()
    IL_0006:  stloc.0
    .try
    {
      IL_0007:  nop
      IL_0008:  ldloc.0
      IL_0009:  ldc.i4.1
      IL_000a:  newobj     instance void class '<>f__AnonymousType0`1'<int32>::.ctor(!0)
      IL_000f:  call       void ['protobuf-net']ProtoBuf.Serializer::Serialize<class '<>f__AnonymousType0`1'<int32>>(class [mscorlib]System.IO.Stream,
                                                                                                                     !!0)
      IL_0014:  nop
      IL_0015:  nop
      IL_0016:  leave.s    IL_0023

    }  // end .try
    finally
    {
      IL_0018:  ldloc.0
      IL_0019:  brfalse.s  IL_0022

      IL_001b:  ldloc.0
      IL_001c:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()
      IL_0021:  nop
      IL_0022:  endfinally
    }  // end handler
    IL_0023:  ret
  } // end of method Program::Main

} // end of class Program

I haven't found a way to make it reference both mscorlib versions at once. And it gets weirder when PCLs are in play.

@ctaggart
Copy link
Owner Author

This Froto project is going to take a new direction. Protocol Buffers v3 support C# code generation out of the box and it is supported via a Google.Protobuf library. I like their approach for the most part. The only thing I don't like is how they handle optional primitives. See #6. I don't plan to use protobuf-net.

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

No branches or pull requests

2 participants