1
votes

I'm using Microsoft.CodeAnalysis.CSharp.dll in my project. I have a class, the property of this class is an object that had been described in Microsoft.CodeAnalysis.CSharp.dll and connected to my project.

[ProtoContract]
public class FunctionAnnotation : 
{
     [ProtoMember(1)] public IMethodSymbol Symbol { get; set; }
}

IMethodSymbol described in Microsoft.CodeAnalysis.CSharp.dll.

I use protobuf-net. When you try to serialize an object it throws an exception:

No serializer defined for type: Microsoft.CodeAnalysis.IMetnodSymbol

It is clear that Microsoft.CodeAnalysis.Method.Symbol is not marked as [ProtoContract]

  private void Serialization()
    {
         string folderName = @"f:/serialization/";
         Directory.CreateDirectory(folderName);
         int name = this.Symbol.ToString().GetHashCode();
         using (FileStream fs = new FileStream("f:/serialization/" + name + ".dat", FileMode.OpenOrCreate))
         {
              Serializer.Serialize(fs, this);
         }
    }

What are the options for marking of existing classes? How to solve this problem?

1

1 Answers

1
votes

In theory you can manually configure external types using the RuntimeTypeModel API, i.e.

RuntimeTypeModel.Default.Add(typeof(SomeTYpe), false).Add("Member", 1); // etc

But: this will work best if the external type is a DTO-style object. If it is a complex domain object, you would be better served by introducing a DTO map separately, i.e. a set of objects you control that represents the data you need, rather than the implementation.