rongfengliang / protobuf-net

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

Import statement not included for .proto files that reference bcl.proto #316

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Please include an e-mail address if this might need a dialogue!
==============

What steps will reproduce the problem?
1.
Use GetSchema(MyType) to generate a suggested .proto file for MyType, where a 
property in MyType is of DateTime.

What is the expected output? What do you see instead?
I expect to see:

package MyLib;
import "bcl.proto";

message Cat {
   optional int32 NumberOfLegs = 1 [default = 0];
   optional bcl.DateTime Dob = 2;
}

instead I see:

package MyLib;

message Cat {
   optional int32 NumberOfLegs = 1 [default = 0];
   optional bcl.DateTime Dob = 2;
}

Original issue reported on code.google.com by Franches...@gmail.com on 27 Aug 2012 at 2:16

GoogleCodeExporter commented 9 years ago
r585

Original comment by marc.gravell on 29 Aug 2012 at 7:36

GoogleCodeExporter commented 9 years ago
Hi,
The fix works now for my example, so thanks for that.

However, I am now getting a stackoverflow exception now when I call GetSchema 
on types that use serialization surrogates. The problem is in MetaType.cs in 
the following code, where GetSchemaTypeName keeps calling itself:

 internal string GetSchemaTypeName()
        {
            if (surrogate != null) return model[surrogate].GetSchemaTypeName();

            if (!Helpers.IsNullOrEmpty(name)) return name;
#if !NO_GENERICS
            if (type
#if WINRT
                .GetTypeInfo()
#endif       
                .IsGenericType)
            {
                StringBuilder sb = new StringBuilder(type.Name);
                int split = type.Name.IndexOf('`');
                if (split >= 0) sb.Length = split;
                foreach (Type arg in type
#if WINRT
                    .GetTypeInfo().GenericTypeArguments
#else               
                    .GetGenericArguments()
#endif
                    )
                {
                    sb.Append('_');
                    Type tmp = arg;
                    int key = model.GetKey(ref tmp);
                    MetaType mt;
                    if (key >= 0 && (mt = model[tmp]) != null)
                    {
                        sb.Append(mt.GetSchemaTypeName());
                    }
                    else
                    {
                        sb.Append(tmp.Name);
                    }
                }
                return sb.ToString();
            }
#endif
            return type.Name;
        }

This was not happening before revision 585

Original comment by Franches...@gmail.com on 29 Aug 2012 at 8:11

GoogleCodeExporter commented 9 years ago
oops! I'll check that; probably another commit in a few minutes

Original comment by marc.gravell on 29 Aug 2012 at 8:13

GoogleCodeExporter commented 9 years ago
I have been unable to reproduce this. Do you have an example model that shows 
this stackoverflow? (I have, however, tidied the general behaviour when 
handling surrogates)

Original comment by marc.gravell on 29 Aug 2012 at 8:42

GoogleCodeExporter commented 9 years ago
Yes, in my model I have System.Exception, and a few other exceptions that 
inherit from it. I am adding them all to the model with a surrogate as follows, 
so I can receive server exceptions and rethrow them on the client:

metaType = runtimeTypeModel.Add(myExceptionType, false);        
metaType.SetSurrogate(typeof(BinarySerializationSurrogate<>).MakeGenericType(myE
xceptionType));

This is my surrogate:

 /// <summary>
    /// Surrogate class to allow Protobuf-net to serialize any class that implements ISerializeable
    /// (e.g. Exceptions).
    /// </summary>
    /// <typeparam name="T">The type of an object that implements ISerializeable.</typeparam>
    [ProtoContract]
    internal class BinarySerializationSurrogate<T>
    {
        [ProtoMember(1)]
        private byte[] objectData = null;

        public static implicit operator T(BinarySerializationSurrogate<T> surrogate)
        {
            T returnValue = default(T);
            if (surrogate == null)
            {
                return returnValue;
            }

            var serializer = new BinaryFormatter();
            using (var serializedStream = new MemoryStream(surrogate.objectData))
            {
                returnValue = (T)serializer.Deserialize(serializedStream);
            }

            return returnValue;
        }

        public static implicit operator BinarySerializationSurrogate<T>(T objectToSerialize)
        {
            if (objectToSerialize == null)
            {
                return null;
            }

            var returnValue = new BinarySerializationSurrogate<T>();

            var serializer = new BinaryFormatter();
            using (var serializedStream = new MemoryStream())
            {
                serializer.Serialize(serializedStream, objectToSerialize);
                returnValue.objectData = serializedStream.ToArray();
            }

            return returnValue;
        }
    }

Original comment by Franches...@gmail.com on 29 Aug 2012 at 8:57

GoogleCodeExporter commented 9 years ago
k; try now

Original comment by marc.gravell on 29 Aug 2012 at 9:16

GoogleCodeExporter commented 9 years ago
Everything works fine now, thanks!!!!! :)

Original comment by Franches...@gmail.com on 29 Aug 2012 at 11:29

GoogleCodeExporter commented 9 years ago

Original comment by marc.gravell on 29 Aug 2012 at 12:35