pizheng / protobuf-net

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

ParseableSerializer.GetCustomToString doesn't find DateTimeOffset's ToString() override #221

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Sample code:

{{{
public class Foo
{
  public DateTimeOffset Time { get; set; }
}

public void Test()
{
  var model = TypeModel.Create();
  model.Add(typeof(Foo), false).Add("Time");

  var foo = new Foo { Time = DateTimeOffset.Now };
  using (var ms = new MemoryStream())
     model.Serialize(ms, foo);
}
}}}

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

I would expect the {{{DateTimeOffset}}} property to make use of the 
{{{ParseableSerializer}}}, as it has both a static {{{Parse()}}} and a custom 
{{{ToString()}}} method.

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

v2.0.0.431, .NET 4.

Please provide any additional information below.

It looks to me like {{{ParseableSerializer.GetCustomToString}}} is not properly 
requesting DateTimeOffset's {{{ToString}}} method.  That is, when passed 
{{{typeof(DateTimeOffset)}}}, the following method returns {{{null}}}:

{{{
private static MethodInfo GetCustomToString(Type type)
{
  return type.GetMethod("ToString", BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public,
     (Binder) null, Helpers.EmptyTypes, (ParameterModifier[]) null);
}
}}}

This method seems to work better for me:

{{{
private static bool HasCustomToString(Type type)
{
   return type.GetMethod("ToString", new Type[0]).DeclaringType != typeof(object);
}
}}}

Original issue reported on code.google.com by ladene...@gmail.com on 12 Aug 2011 at 10:53

GoogleCodeExporter commented 9 years ago
Well, I obviously fail at markup, heh.  Hopefully you get the idea anyway!

Original comment by ladene...@gmail.com on 12 Aug 2011 at 10:54