leejw51 / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
0 stars 0 forks source link

ProtoBuf unable to operate on classes that expose a static Parse method #238

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

What steps will reproduce the problem?

1. Given the following contract class:

[Serializable]
[ProtoContract(Name="Version")]
public class VersionInfo
{
    [ProtoMember( 1 )]
    public int Major { get; set; }
    [ProtoMember( 2 )]
    public int Minor { get; set; }
    [ProtoMember( 3 )]
    public int Build { get; set; }
    [ProtoMember( 4 )]
    public int Revision { get; set; }

    public override string ToString()
    {
        return string.Format( "{0}.{1}.{2}.{3}", Major, Minor, Build, Revision );
    }

    public static VersionInfo Parse( string version )
    {
        return new VersionInfo();
    }
}

2. Then executing this code:

using( var stream = new MemoryStream() )
{
    Serializer.Serialize( stream, new VersionInfo() );
}

3. Causes ProtoBuf to throw an ArgumentException with the message "Data of this 
type has inbuilt behaviour, and cannot be added to a model in this way: 
VersionInfo".

What is the expected output? What do you see instead?

Expected: serialization should work, no exceptions.
Actual: exception thrown.
Workaround: avoid static method named Parse that takes string and returns 
instance of class.

What version of the product are you using? On what operating system?

Windows 7, .NET 4, ProtoBuf 2.0.0.431

Please provide any additional information below.

The error appears to be thrown in the MetaType constructor, because 
coreSerializer has been assigned a value. And this is the case because 
ValueMember.TryGetCoreSerializer detects the static Parse method on the class 
and returns a serializer based on that.

Something in this behavior of detecting Parse methods is broken, as I certainly 
do not want it to happen for this class.

Original issue reported on code.google.com by mor...@mertner.com on 2 Oct 2011 at 2:15