diff options
author | csharptest <roger@csharptest.net> | 2010-11-07 18:49:42 -0600 |
---|---|---|
committer | csharptest <roger@csharptest.net> | 2010-11-07 18:49:42 -0600 |
commit | 487da48ad2c88d7c07451c546c3541d7db7ceae6 (patch) | |
tree | 98a502866fad09e1b775c69b0b58383cb69bd1b8 /src/ProtocolBuffers | |
parent | 80824a51c2730cad30395cf8c54b2a44546cd744 (diff) | |
download | protobuf-487da48ad2c88d7c07451c546c3541d7db7ceae6.tar.gz protobuf-487da48ad2c88d7c07451c546c3541d7db7ceae6.tar.bz2 protobuf-487da48ad2c88d7c07451c546c3541d7db7ceae6.zip |
Fix for repeated extensions.
Diffstat (limited to 'src/ProtocolBuffers')
-rw-r--r-- | src/ProtocolBuffers/EnumLite.cs | 4 | ||||
-rw-r--r-- | src/ProtocolBuffers/GeneratedExtensionLite.cs | 64 |
2 files changed, 33 insertions, 35 deletions
diff --git a/src/ProtocolBuffers/EnumLite.cs b/src/ProtocolBuffers/EnumLite.cs index a408fd43..0418d77e 100644 --- a/src/ProtocolBuffers/EnumLite.cs +++ b/src/ProtocolBuffers/EnumLite.cs @@ -66,7 +66,7 @@ namespace Google.ProtocolBuffers { } public class EnumLiteMap<TEnum> : IEnumLiteMap<IEnumLite> - where TEnum : struct, IComparable, IFormattable, IConvertible { + where TEnum : struct, IComparable, IFormattable { struct EnumValue : IEnumLite, IComparable<IEnumLite> { int _value; @@ -86,7 +86,7 @@ namespace Google.ProtocolBuffers { public EnumLiteMap() { items = new SortedList<int, IEnumLite>(); foreach (TEnum evalue in Enum.GetValues(typeof(TEnum))) - items.Add(evalue.ToInt32(CultureInfo.InvariantCulture), new EnumValue(evalue.ToInt32(CultureInfo.InvariantCulture))); + items.Add(Convert.ToInt32(evalue), new EnumValue(Convert.ToInt32(evalue))); } IEnumLite IEnumLiteMap.FindValueByNumber(int number) { diff --git a/src/ProtocolBuffers/GeneratedExtensionLite.cs b/src/ProtocolBuffers/GeneratedExtensionLite.cs index c2ba5627..87009cf8 100644 --- a/src/ProtocolBuffers/GeneratedExtensionLite.cs +++ b/src/ProtocolBuffers/GeneratedExtensionLite.cs @@ -126,7 +126,32 @@ namespace Google.ProtocolBuffers { return FieldNumber.CompareTo(other.FieldNumber); } } - + + public class GeneratedRepeatExtensionLite<TContainingType, TExtensionType> : GeneratedExtensionLite<TContainingType, IList<TExtensionType>> + where TContainingType : IMessageLite { + public GeneratedRepeatExtensionLite(TContainingType containingTypeDefaultInstance, + IMessageLite messageDefaultInstance, IEnumLiteMap enumTypeMap, int number, FieldType type, bool isPacked) : + base(containingTypeDefaultInstance, new List<TExtensionType>(), messageDefaultInstance, enumTypeMap, number, type, isPacked) { + } + + public override object ToReflectionType(object value) { + IList<TExtensionType> result = new List<TExtensionType>(); + foreach (object element in (IEnumerable)value) { + result.Add((TExtensionType)SingularToReflectionType(element)); + } + return result; + } + + public override object FromReflectionType(object value) { + // Must convert the whole list. + List<TExtensionType> result = new List<TExtensionType>(); + foreach (object element in (IEnumerable)value) { + result.Add((TExtensionType)SingularFromReflectionType(element)); + } + return result; + } + } + public class GeneratedExtensionLite<TContainingType, TExtensionType> : IGeneratedExtensionLite where TContainingType : IMessageLite { @@ -166,8 +191,8 @@ namespace Google.ProtocolBuffers { false /* isRepeated */, false /* isPacked */)) { } - /** For use by generated code only. */ - public GeneratedExtensionLite( + /** Repeating fields: For use by generated code only. */ + protected GeneratedExtensionLite( TContainingType containingTypeDefaultInstance, TExtensionType defaultValue, IMessageLite messageDefaultInstance, @@ -231,48 +256,21 @@ namespace Google.ProtocolBuffers { /// for enums use EnumValueDescriptors but the native accessors use /// the generated enum type. /// </summary> - public object ToReflectionType(object value) { - if (descriptor.IsRepeated) { - if (descriptor.MappedType == MappedType.Enum) { - // Must convert the whole list. - IList<object> result = new List<object>(); - foreach (object element in (IEnumerable)value) { - result.Add(SingularToReflectionType(element)); - } - return result; - } else { - return value; - } - } else { + public virtual object ToReflectionType(object value) { return SingularToReflectionType(value); - } } /// <summary> /// Like ToReflectionType(object) but for a single element. /// </summary> - internal Object SingularToReflectionType(object value) { + public object SingularToReflectionType(object value) { return descriptor.MappedType == MappedType.Enum ? descriptor.EnumType.FindValueByNumber((int)value) : value; } - public object FromReflectionType(object value) { - if (descriptor.IsRepeated) { - if (Descriptor.MappedType == MappedType.Message || - Descriptor.MappedType == MappedType.Enum) { - // Must convert the whole list. - List<TExtensionType> result = new List<TExtensionType>(); - foreach (object element in (IEnumerable)value) { - result.Add((TExtensionType)SingularFromReflectionType(element)); - } - return result; - } else { - return value; - } - } else { + public virtual object FromReflectionType(object value) { return SingularFromReflectionType(value); - } } public object SingularFromReflectionType(object value) { |