1

Here's a scenario:

[ProtoContract]
class A{}

A while later another dev adds:

class B : A {
  int m;
}

I have a fallback to XML if a class doesn't have ProtoContractAttribute or had errors during Google Protocol Buffers serialization. The problem with the code above is that B inherent ProtoContractAttribute from A (note Inherited = true below), but member 'm' will not be serialized since it does not have ProtoMemberAttribute.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum 
| AttributeTargets.Interface, AllowMultiple = false, Inherited = true)]
public sealed class ProtoContractAttribute : Attribute
{...}

It would be great to be able to add:

[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public class ProtoContractWithoutInheritance : ProtoBuf.ProtoContractAttribute
{
}

But ProtoContractAttribute is sealed, and even if it hadn't been sealed, ProtoBuf-Net is looking for the explicit type:

if (item.AttributeType.FullName == "ProtoBuf.ProtoContractAttribute")
{...}

Any solution / workaround?

Thanks!

Oron Nadiv
  • 225
  • 2
  • 11

1 Answers1

1

Well, the simplest workaround I can think of is:

bool isProto = Attribute.IsDefined(
    yourType, typeof(ProtoContractAttribute), false);

However, I am also investigating what breaks if we make that non-inherited. Something looks a bit wrong with that being inherited!

Edit: this should also be fixed from r571 onwards; the attribute is not marked as inherited now.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900