diff --git a/.gitignore b/.gitignore index 76c414f..6ef6663 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ src/ProtocolBuffers.Test/bin/ src/ProtocolBuffers.Test/obj/ src/ProtocolBuffersLite.Test/bin/ src/ProtocolBuffersLite.Test/obj/ +src/ProtocolBuffers.Serialization/bin/ +src/ProtocolBuffers.Serialization/obj/ src/ProtoBench/bin/ src/ProtoBench/obj/ src/ProtoDump/bin/ diff --git a/src/ProtocolBuffers.Serialization/AbstractWriter.cs b/src/ProtocolBuffers.Serialization/AbstractWriter.cs index 2dc6b88..1176b38 100644 --- a/src/ProtocolBuffers.Serialization/AbstractWriter.cs +++ b/src/ProtocolBuffers.Serialization/AbstractWriter.cs @@ -155,10 +155,10 @@ protected virtual void WriteField(FieldType fieldType, string field, object valu { WriteEnum(field, ((IEnumLite) value).Number, ((IEnumLite) value).Name); } - else if (value is IConvertible) + else if (value is ValueType) { - WriteEnum(field, ((IConvertible)value).ToInt32(FrameworkPortability.InvariantCulture), - ((IConvertible)value).ToString(FrameworkPortability.InvariantCulture)); + int intValue = Convert.ToInt32(value); + WriteEnum(field, Convert.ToInt32(value), intValue.ToString(FrameworkPortability.InvariantCulture)); } else { diff --git a/src/ProtocolBuffers.Serialization/DictionaryReader.cs b/src/ProtocolBuffers.Serialization/DictionaryReader.cs index c460523..1f2a68c 100644 --- a/src/ProtocolBuffers.Serialization/DictionaryReader.cs +++ b/src/ProtocolBuffers.Serialization/DictionaryReader.cs @@ -80,14 +80,7 @@ private bool GetValue(ref T value) { try { - if (obj is IConvertible) - { - value = (T)Convert.ChangeType(obj, typeof(T), FrameworkPortability.InvariantCulture); - } - else - { - value = (T) obj; - } + value = (T) Convert.ChangeType(obj, typeof(T), FrameworkPortability.InvariantCulture); } catch { diff --git a/src/ProtocolBuffers.Serialization/ProtocolBuffers.Serialization.WinRT.PCL.csproj b/src/ProtocolBuffers.Serialization/ProtocolBuffers.Serialization.WinRT.PCL.csproj new file mode 100644 index 0000000..26090e0 --- /dev/null +++ b/src/ProtocolBuffers.Serialization/ProtocolBuffers.Serialization.WinRT.PCL.csproj @@ -0,0 +1,80 @@ + + + + + 12.0 + Debug + AnyCPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA} + Library + Properties + Google.ProtocolBuffers.Serialization + Google.ProtocolBuffers.Serialization + en-US + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Profile32 + v4.6 + + + true + full + false + bin\portable-win81+wpa81\Debug\ + TRACE;DEBUG;WINDOWS_RUNTIME + prompt + 4 + + + pdbonly + true + bin\portable-win81+wpa81\Release\ + TRACE;WINDOWS_RUNTIME + prompt + 4 + bin\portable-win81+wpa81\Release\Google.ProtocolBuffers.Serialization.xml + + + + + {5636c607-a1d0-4890-a810-041320618795} + ProtocolBuffers.WinRT.PCL + + + + + + + + + FrameworkPortability.cs + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ProtocolBuffers.Serialization/ProtocolBuffersLite.Serialization.WinRT.PCL.csproj b/src/ProtocolBuffers.Serialization/ProtocolBuffersLite.Serialization.WinRT.PCL.csproj new file mode 100644 index 0000000..bffa4a7 --- /dev/null +++ b/src/ProtocolBuffers.Serialization/ProtocolBuffersLite.Serialization.WinRT.PCL.csproj @@ -0,0 +1,80 @@ + + + + + 12.0 + Debug + AnyCPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9} + Library + Properties + Google.ProtocolBuffersLite.Serialization + Google.ProtocolBuffersLite.Serialization + en-US + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Profile32 + v4.6 + + + true + full + false + bin\portable-win81+wpa81\Debug\ + TRACE;DEBUG;WINDOWS_RUNTIME;LITE + prompt + 4 + + + pdbonly + true + bin\portable-win81+wpa81\Release\ + TRACE;WINDOWS_RUNTIME;LITE + prompt + 4 + bin\portable-win81+wpa81\Release\Google.ProtocolBuffersLite.Serialization.xml + + + + + {82181c67-7b33-4286-a575-42b9a2815979} + ProtocolBuffersLite.WinRT.PCL + + + + + + + + + FrameworkPortability.cs + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ProtocolBuffers.Universal.sln b/src/ProtocolBuffers.Universal.sln new file mode 100644 index 0000000..1638ec1 --- /dev/null +++ b/src/ProtocolBuffers.Universal.sln @@ -0,0 +1,94 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtocolBuffers.WinRT.PCL", "ProtocolBuffers\ProtocolBuffers.WinRT.PCL.csproj", "{5636C607-A1D0-4890-A810-041320618795}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtocolBuffers.Serialization.WinRT.PCL", "ProtocolBuffers.Serialization\ProtocolBuffers.Serialization.WinRT.PCL.csproj", "{B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtocolBuffersLite.WinRT.PCL", "ProtocolBuffers\ProtocolBuffersLite.WinRT.PCL.csproj", "{82181C67-7B33-4286-A575-42B9A2815979}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtocolBuffersLite.Serialization.WinRT.PCL", "ProtocolBuffers.Serialization\ProtocolBuffersLite.Serialization.WinRT.PCL.csproj", "{B492F9CC-F247-4F55-B91E-0359D07D21D9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|ARM = Debug|ARM + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|ARM = Release|ARM + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5636C607-A1D0-4890-A810-041320618795}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Debug|ARM.ActiveCfg = Debug|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Debug|ARM.Build.0 = Debug|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Debug|x64.ActiveCfg = Debug|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Debug|x64.Build.0 = Debug|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Debug|x86.ActiveCfg = Debug|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Debug|x86.Build.0 = Debug|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Release|Any CPU.Build.0 = Release|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Release|ARM.ActiveCfg = Release|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Release|ARM.Build.0 = Release|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Release|x64.ActiveCfg = Release|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Release|x64.Build.0 = Release|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Release|x86.ActiveCfg = Release|Any CPU + {5636C607-A1D0-4890-A810-041320618795}.Release|x86.Build.0 = Release|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Debug|ARM.ActiveCfg = Debug|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Debug|ARM.Build.0 = Debug|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Debug|x64.ActiveCfg = Debug|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Debug|x64.Build.0 = Debug|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Debug|x86.ActiveCfg = Debug|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Debug|x86.Build.0 = Debug|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Release|Any CPU.Build.0 = Release|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Release|ARM.ActiveCfg = Release|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Release|ARM.Build.0 = Release|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Release|x64.ActiveCfg = Release|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Release|x64.Build.0 = Release|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Release|x86.ActiveCfg = Release|Any CPU + {B7924AAF-514E-40C0-A5A6-521E1D4A8ACA}.Release|x86.Build.0 = Release|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Debug|Any CPU.Build.0 = Debug|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Debug|ARM.ActiveCfg = Debug|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Debug|ARM.Build.0 = Debug|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Debug|x64.ActiveCfg = Debug|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Debug|x64.Build.0 = Debug|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Debug|x86.ActiveCfg = Debug|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Debug|x86.Build.0 = Debug|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Release|Any CPU.ActiveCfg = Release|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Release|Any CPU.Build.0 = Release|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Release|ARM.ActiveCfg = Release|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Release|ARM.Build.0 = Release|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Release|x64.ActiveCfg = Release|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Release|x64.Build.0 = Release|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Release|x86.ActiveCfg = Release|Any CPU + {82181C67-7B33-4286-A575-42B9A2815979}.Release|x86.Build.0 = Release|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Debug|ARM.ActiveCfg = Debug|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Debug|ARM.Build.0 = Debug|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Debug|x64.ActiveCfg = Debug|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Debug|x64.Build.0 = Debug|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Debug|x86.ActiveCfg = Debug|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Debug|x86.Build.0 = Debug|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Release|Any CPU.Build.0 = Release|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Release|ARM.ActiveCfg = Release|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Release|ARM.Build.0 = Release|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Release|x64.ActiveCfg = Release|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Release|x64.Build.0 = Release|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Release|x86.ActiveCfg = Release|Any CPU + {B492F9CC-F247-4F55-B91E-0359D07D21D9}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs b/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs index 6d17ae2..383c3a2 100644 --- a/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs +++ b/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs @@ -31,6 +31,7 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using System; using System.Collections.Generic; +using System.Linq; using System.Reflection; using Google.ProtocolBuffers.Collections; using Google.ProtocolBuffers.DescriptorProtos; @@ -441,11 +442,17 @@ public MessageDescriptor MessageType private static IDictionary MapFieldTypes() { var map = new Dictionary(); - foreach (FieldInfo field in typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public)) +#if WINDOWS_RUNTIME + var publicFields = typeof(FieldType).GetRuntimeFields().Where(x => x.IsStatic && x.IsPublic); +#else + var publicFields = typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public); +#endif + + foreach (FieldInfo field in publicFields) { FieldType fieldType = (FieldType) field.GetValue(null); FieldMappingAttribute mapping = - (FieldMappingAttribute) field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0]; + (FieldMappingAttribute) field.GetCustomAttributes(typeof(FieldMappingAttribute), false).First(); map[fieldType] = mapping.MappedType; } return Dictionaries.AsReadOnly(map); diff --git a/src/ProtocolBuffers/Descriptors/FieldMappingAttribute.cs b/src/ProtocolBuffers/Descriptors/FieldMappingAttribute.cs index fc58d04..d7d4f95 100644 --- a/src/ProtocolBuffers/Descriptors/FieldMappingAttribute.cs +++ b/src/ProtocolBuffers/Descriptors/FieldMappingAttribute.cs @@ -31,6 +31,7 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using System; using System.Collections.Generic; +using System.Linq; using System.Reflection; using Google.ProtocolBuffers.Collections; @@ -63,11 +64,16 @@ public FieldMappingAttribute(MappedType mappedType, WireFormat.WireType wireType private static IDictionary MapFieldTypes() { var map = new Dictionary(); - foreach (FieldInfo field in typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public)) +#if WINDOWS_RUNTIME + var publicFields = typeof(FieldType).GetRuntimeFields().Where(x => x.IsStatic && x.IsPublic); +#else + var publicFields = typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public); +#endif + foreach (FieldInfo field in publicFields) { FieldType fieldType = (FieldType) field.GetValue(null); FieldMappingAttribute mapping = - (FieldMappingAttribute) field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0]; + (FieldMappingAttribute) field.GetCustomAttributes(typeof(FieldMappingAttribute), false).First(); map[fieldType] = mapping; } return Dictionaries.AsReadOnly(map); diff --git a/src/ProtocolBuffers/EnumLite.cs b/src/ProtocolBuffers/EnumLite.cs index 1301ec2..483b4a5 100644 --- a/src/ProtocolBuffers/EnumLite.cs +++ b/src/ProtocolBuffers/EnumLite.cs @@ -36,6 +36,8 @@ using System; using System.Collections.Generic; +using System.Linq; +using System.Reflection; namespace Google.ProtocolBuffers { @@ -134,8 +136,14 @@ static EnumParser() // It will actually be a T[], but the CLR will let us convert. array = (int[])Enum.GetValues(typeof(T)); #else + +# if WINDOWS_RUNTIME + var publicFields = typeof(T).GetRuntimeFields().Where(x => x.IsStatic && x.IsPublic); +# else + var publicFields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static) +# endif var temp = new List(); - foreach (var fld in typeof (T).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)) + foreach (var fld in publicFields) { if (fld.IsLiteral && fld.FieldType == typeof(T)) { diff --git a/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs b/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs index 798f0dd..916857a 100644 --- a/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs +++ b/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs @@ -56,7 +56,11 @@ internal static class ReflectionUtil public static Func CreateUpcastDelegate(MethodInfo method) { // The tricky bit is invoking CreateCreateUpcastDelegateImpl with the right type parameters +#if WINDOWS_RUNTIME + MethodInfo openImpl = typeof(ReflectionUtil).GetRuntimeMethod("CreateUpcastDelegateImpl", new[] { typeof(MethodInfo) }); +#else MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateUpcastDelegateImpl"); +#endif MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.ReturnType); return (Func) closedImpl.Invoke(null, new object[] {method}); } @@ -81,7 +85,11 @@ public static Func CreateUpcastDelegateImpl(M /// public static Action CreateDowncastDelegate(MethodInfo method) { +#if WINDOWS_RUNTIME + MethodInfo openImpl = typeof(ReflectionUtil).GetRuntimeMethod("CreateDowncastDelegateImpl", new[] { typeof(MethodInfo) }); +#else MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateImpl"); +#endif MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType); return (Action) closedImpl.Invoke(null, new object[] {method}); } @@ -101,7 +109,11 @@ public static Action CreateDowncastDelegateImpl public static Action CreateDowncastDelegateIgnoringReturn(MethodInfo method) { +#if WINDOWS_RUNTIME + MethodInfo openImpl = typeof(ReflectionUtil).GetRuntimeMethod("CreateDowncastDelegateIgnoringReturnImpl", new[] { typeof(MethodInfo) }); +#else MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateIgnoringReturnImpl"); +#endif MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType, method.ReturnType); return (Action) closedImpl.Invoke(null, new object[] {method}); @@ -122,7 +134,11 @@ public static Action CreateDowncastDelegateIgnoringReturnImpl public static Func CreateStaticUpcastDelegate(MethodInfo method) { +#if WINDOWS_RUNTIME + MethodInfo openImpl = typeof(ReflectionUtil).GetRuntimeMethod("CreateStaticUpcastDelegateImpl", new[] { typeof(MethodInfo) }); +#else MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateStaticUpcastDelegateImpl"); +#endif MethodInfo closedImpl = openImpl.MakeGenericMethod(method.ReturnType); return (Func) closedImpl.Invoke(null, new object[] {method}); } @@ -136,7 +152,9 @@ public static Func CreateStaticUpcastDelegateImpl(MethodInfo method internal static Func CreateDelegateFunc(MethodInfo method) { -#if !CF20 +#if WINDOWS_RUNTIME + return (Func) method.CreateDelegate(typeof(Func), null); +#elif !CF20 object tdelegate = Delegate.CreateDelegate(typeof(Func), null, method); return (Func)tdelegate; #else @@ -146,7 +164,9 @@ internal static Func CreateDelegateFunc(MethodInfo method) internal static Func CreateDelegateFunc(MethodInfo method) { -#if !CF20 +#if WINDOWS_RUNTIME + return (Func) method.CreateDelegate(typeof(Func), null); +#elif !CF20 object tdelegate = Delegate.CreateDelegate(typeof(Func), null, method); return (Func)tdelegate; #else @@ -160,7 +180,9 @@ internal static Func CreateDelegateFunc(MethodInfo metho internal static Func CreateDelegateFunc(MethodInfo method) { -#if !CF20 +#if WINDOWS_RUNTIME + return (Func) method.CreateDelegate(typeof(Func), null); +#elif !CF20 object tdelegate = Delegate.CreateDelegate(typeof(Func), null, method); return (Func)tdelegate; #else @@ -174,7 +196,9 @@ internal static Func CreateDelegateFunc(Method internal static Action CreateDelegateAction(MethodInfo method) { -#if !CF20 +#if WINDOWS_RUNTIME + return (Action) method.CreateDelegate(typeof(Action), null); +#elif !CF20 object tdelegate = Delegate.CreateDelegate(typeof(Action), null, method); return (Action)tdelegate; #else @@ -185,5 +209,33 @@ internal static Action CreateDelegateAction(MethodInfo method) return delegate(T1 arg1, T2 arg2) { method.Invoke(arg1, new object[] { arg2 }); }; #endif } + + internal static PropertyInfo GetProperty(string name) + { +#if WINDOWS_RUNTIME + return typeof(T).GetRuntimeProperty(name); +#else + return typeof(T).GetProperty(name); +#endif + } + + internal static MethodInfo GetMethod(string name, Type[] types) + { +#if WINDOWS_RUNTIME + return typeof(T).GetRuntimeMethod(name, types); +#else + return typeof(T).GetMethod(name, types); +#endif + } + + internal static MethodInfo GetMethod(string name) + { +#if WINDOWS_RUNTIME + return typeof(T).GetRuntimeMethod(name, EmptyTypes); +#else + return typeof(T).GetMethod(name, EmptyTypes); +#endif + } + } } \ No newline at end of file diff --git a/src/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs b/src/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs index fd18b90..be40feb 100644 --- a/src/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs +++ b/src/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs @@ -53,7 +53,12 @@ internal sealed class RepeatedMessageAccessor : RepeatedPrim internal RepeatedMessageAccessor(string name) : base(name) { +#if WINDOWS_RUNTIME + MethodInfo createBuilderMethod = ClrType.GetRuntimeMethod("CreateBuilder", EmptyTypes); +#else MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", EmptyTypes); +#endif + if (createBuilderMethod == null) { throw new ArgumentException("No public static CreateBuilder method declared in " + ClrType.Name); @@ -69,7 +74,11 @@ private object CoerceType(object value) { ThrowHelper.ThrowIfNull(value, "value"); // If it's already of the right type, we're done +#if WINDOWS_RUNTIME + if (ClrType.GetTypeInfo().IsSubclassOf(value.GetType())) +#else if (ClrType.IsInstanceOfType(value)) +#endif { return value; } diff --git a/src/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs b/src/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs index d3b926b..6783768 100644 --- a/src/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs +++ b/src/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs @@ -66,14 +66,14 @@ protected Type ClrType internal RepeatedPrimitiveAccessor(string name) { - PropertyInfo messageProperty = typeof(TMessage).GetProperty(name + "List"); - PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name + "List"); - PropertyInfo countProperty = typeof(TMessage).GetProperty(name + "Count"); - MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name, EmptyTypes); - getElementMethod = typeof(TMessage).GetMethod("Get" + name, new Type[] {typeof(int)}); + PropertyInfo messageProperty = ReflectionUtil.GetProperty(name + "List"); + PropertyInfo builderProperty = ReflectionUtil.GetProperty(name + "List"); + PropertyInfo countProperty = ReflectionUtil.GetProperty(name + "Count"); + MethodInfo clearMethod = ReflectionUtil.GetMethod("Clear" + name); + getElementMethod = ReflectionUtil.GetMethod("Get" + name, new Type[] {typeof(int)}); clrType = getElementMethod.ReturnType; - MethodInfo addMethod = typeof(TBuilder).GetMethod("Add" + name, new Type[] {ClrType}); - setElementMethod = typeof(TBuilder).GetMethod("Set" + name, new Type[] {typeof(int), ClrType}); + MethodInfo addMethod = ReflectionUtil.GetMethod("Add" + name, new Type[] {ClrType}); + setElementMethod = ReflectionUtil.GetMethod("Set" + name, new Type[] {typeof(int), ClrType}); if (messageProperty == null || builderProperty == null || countProperty == null @@ -85,10 +85,16 @@ internal RepeatedPrimitiveAccessor(string name) throw new ArgumentException("Not all required properties/methods available"); } clearDelegate = ReflectionUtil.CreateDelegateFunc(clearMethod); + addValueDelegate = ReflectionUtil.CreateDowncastDelegateIgnoringReturn(addMethod); +#if WINDOWS_RUNTIME + countDelegate = ReflectionUtil.CreateDelegateFunc(countProperty.GetMethod); + getValueDelegate = ReflectionUtil.CreateUpcastDelegate(messageProperty.GetMethod); + getRepeatedWrapperDelegate = ReflectionUtil.CreateUpcastDelegate(builderProperty.GetMethod); +#else countDelegate = ReflectionUtil.CreateDelegateFunc(countProperty.GetGetMethod()); getValueDelegate = ReflectionUtil.CreateUpcastDelegate(messageProperty.GetGetMethod()); - addValueDelegate = ReflectionUtil.CreateDowncastDelegateIgnoringReturn(addMethod); getRepeatedWrapperDelegate = ReflectionUtil.CreateUpcastDelegate(builderProperty.GetGetMethod()); +#endif } public bool Has(TMessage message) diff --git a/src/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs b/src/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs index 6bf48a0..6828b43 100644 --- a/src/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs +++ b/src/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs @@ -50,7 +50,11 @@ internal sealed class SingleMessageAccessor : SinglePrimitiv internal SingleMessageAccessor(string name) : base(name) { +#if WINDOWS_RUNTIME + MethodInfo createBuilderMethod = ClrType.GetRuntimeMethod("CreateBuilder", ReflectionUtil.EmptyTypes); +#else MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", ReflectionUtil.EmptyTypes); +#endif if (createBuilderMethod == null) { throw new ArgumentException("No public static CreateBuilder method declared in " + ClrType.Name); @@ -66,7 +70,11 @@ private object CoerceType(object value) { ThrowHelper.ThrowIfNull(value, "value"); // If it's already of the right type, we're done +#if WINDOWS_RUNTIME + if (ClrType.GetTypeInfo().IsSubclassOf(value.GetType())) +#else if (ClrType.IsInstanceOfType(value)) +#endif { return value; } diff --git a/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs b/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs index e5a0754..58cbd62 100644 --- a/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs +++ b/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs @@ -58,19 +58,25 @@ protected Type ClrType internal SinglePrimitiveAccessor(string name) { - PropertyInfo messageProperty = typeof(TMessage).GetProperty(name, null, ReflectionUtil.EmptyTypes); - PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name, null, ReflectionUtil.EmptyTypes); - PropertyInfo hasProperty = typeof(TMessage).GetProperty("Has" + name); - MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name); + PropertyInfo messageProperty = ReflectionUtil.GetProperty(name); + PropertyInfo builderProperty = ReflectionUtil.GetProperty(name); + PropertyInfo hasProperty = ReflectionUtil.GetProperty("Has" + name); + MethodInfo clearMethod = ReflectionUtil.GetMethod("Clear" + name, ReflectionUtil.EmptyTypes); if (messageProperty == null || builderProperty == null || hasProperty == null || clearMethod == null) { throw new ArgumentException("Not all required properties/methods available"); } clrType = messageProperty.PropertyType; - hasDelegate = ReflectionUtil.CreateDelegateFunc(hasProperty.GetGetMethod()); clearDelegate = ReflectionUtil.CreateDelegateFunc(clearMethod); +#if WINDOWS_RUNTIME + hasDelegate = ReflectionUtil.CreateDelegateFunc(hasProperty.GetMethod); + getValueDelegate = ReflectionUtil.CreateUpcastDelegate(messageProperty.GetMethod); + setValueDelegate = ReflectionUtil.CreateDowncastDelegate(builderProperty.SetMethod); +#else + hasDelegate = ReflectionUtil.CreateDelegateFunc(hasProperty.GetGetMethod()); getValueDelegate = ReflectionUtil.CreateUpcastDelegate(messageProperty.GetGetMethod()); setValueDelegate = ReflectionUtil.CreateDowncastDelegate(builderProperty.GetSetMethod()); +#endif } public bool Has(TMessage message) @@ -101,7 +107,7 @@ public virtual void SetValue(TBuilder builder, object value) setValueDelegate(builder, value); } - #region Methods only related to repeated values +#region Methods only related to repeated values public int GetRepeatedCount(TMessage message) { @@ -128,6 +134,6 @@ public object GetRepeatedWrapper(TBuilder builder) throw new InvalidOperationException(); } - #endregion +#endregion } } \ No newline at end of file diff --git a/src/ProtocolBuffers/GeneratedExtensionBase.cs b/src/ProtocolBuffers/GeneratedExtensionBase.cs index 424b981..879bd02 100644 --- a/src/ProtocolBuffers/GeneratedExtensionBase.cs +++ b/src/ProtocolBuffers/GeneratedExtensionBase.cs @@ -80,8 +80,12 @@ protected GeneratedExtensionBase(FieldDescriptor descriptor, Type singularExtens this.descriptor = descriptor; if (descriptor.MappedType == MappedType.Message) { +#if WINDOWS_RUNTIME + PropertyInfo defaultInstanceProperty = singularExtensionType.GetRuntimeProperty("DefaultInstance"); +#else PropertyInfo defaultInstanceProperty = singularExtensionType .GetProperty("DefaultInstance", BindingFlags.Static | BindingFlags.Public); +#endif if (defaultInstanceProperty == null) { throw new ArgumentException("No public static DefaultInstance property for type " + diff --git a/src/ProtocolBuffers/GeneratedMessageLite.cs b/src/ProtocolBuffers/GeneratedMessageLite.cs index 0dfc976..9c4d09b 100644 --- a/src/ProtocolBuffers/GeneratedMessageLite.cs +++ b/src/ProtocolBuffers/GeneratedMessageLite.cs @@ -111,7 +111,7 @@ protected static void PrintField(string name, bool hasValue, object value, TextW } else { - writer.WriteLine("{0}: {1}", name, ((IConvertible)value).ToString(FrameworkPortability.InvariantCulture)); + writer.WriteLine("{0}: {1}", name, ((IFormattable)value).ToString(null, FrameworkPortability.InvariantCulture)); } } diff --git a/src/ProtocolBuffers/MessageStreamIterator.cs b/src/ProtocolBuffers/MessageStreamIterator.cs index 32d697c..a3f034d 100644 --- a/src/ProtocolBuffers/MessageStreamIterator.cs +++ b/src/ProtocolBuffers/MessageStreamIterator.cs @@ -38,6 +38,8 @@ using System.Collections; using System.Collections.Generic; using System.IO; +using System.Reflection; +using Google.ProtocolBuffers.FieldAccess; namespace Google.ProtocolBuffers { @@ -76,9 +78,7 @@ private static TMessage CreateDefaultInstance() { try { - return (TMessage)typeof(TMessage) - .GetProperty("DefaultInstance", typeof(TMessage), new Type[0]) - .GetValue(null, null); + return (TMessage) ReflectionUtil.GetProperty("DefaultInstance").GetValue(null, null); } catch (Exception e) { diff --git a/src/ProtocolBuffers/MessageUtil.cs b/src/ProtocolBuffers/MessageUtil.cs index d120d20..6cc8533 100644 --- a/src/ProtocolBuffers/MessageUtil.cs +++ b/src/ProtocolBuffers/MessageUtil.cs @@ -53,25 +53,41 @@ public static class MessageUtil /// The type parameter is null. /// The type doesn't implement IMessage, or doesn't /// have a static DefaultMessage property of the same type, or is generic or abstract. - /// public static IMessage GetDefaultMessage(Type type) { if (type == null) { throw new ArgumentNullException("type", "No type specified."); } + +#if WINDOWS_RUNTIME + var typeInfo = type.GetTypeInfo(); + + if (typeInfo.IsAbstract || typeInfo.IsGenericTypeDefinition) + { + throw new ArgumentException("Unable to get a default message for an abstract or generic type (" + type.FullName + ")"); + } + if (!typeof(IMessage).GetTypeInfo().IsAssignableFrom(typeInfo)) + { + throw new ArgumentException("Unable to get a default message for non-message type (" + type.FullName + ")"); + } +#else if (type.IsAbstract || type.IsGenericTypeDefinition) { - throw new ArgumentException("Unable to get a default message for an abstract or generic type (" + - type.FullName + ")"); + throw new ArgumentException("Unable to get a default message for an abstract or generic type (" + type.FullName + ")"); } if (!typeof(IMessage).IsAssignableFrom(type)) { - throw new ArgumentException("Unable to get a default message for non-message type (" + type.FullName + - ")"); + throw new ArgumentException("Unable to get a default message for non-message type (" + type.FullName + ")"); } +#endif + +#if WINDOWS_RUNTIME + PropertyInfo property = type.GetRuntimeProperty("DefaultInstance"); +#else PropertyInfo property = type.GetProperty("DefaultInstance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); +#endif if (property == null) { throw new ArgumentException(type.FullName + " doesn't have a static DefaultInstance property"); diff --git a/src/ProtocolBuffers/ProtocolBuffers.WinRT.PCL.csproj b/src/ProtocolBuffers/ProtocolBuffers.WinRT.PCL.csproj new file mode 100644 index 0000000..2d7f339 --- /dev/null +++ b/src/ProtocolBuffers/ProtocolBuffers.WinRT.PCL.csproj @@ -0,0 +1,146 @@ + + + + + 12.0 + Debug + AnyCPU + {5636C607-A1D0-4890-A810-041320618795} + Library + Properties + Google.ProtocolBuffers + Google.ProtocolBuffers + en-US + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Profile32 + v4.6 + + + true + full + false + bin\portable-win81+wpa81\Debug\ + TRACE;DEBUG;PORTABLE_LIBRARY;WINDOWS_RUNTIME;NOSERIALIZABLE;NOSORTEDLIST + prompt + 4 + + + pdbonly + true + bin\portable-win81+wpa81\Release\ + TRACE;PORTABLE_LIBRARY;WINDOWS_RUNTIME;NOSERIALIZABLE;NOSORTEDLIST + prompt + 4 + bin\portable-win81+wpa81\Release\Google.ProtocolBuffers.xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ProtocolBuffers/ProtocolBuffersLite.WinRT.PCL.csproj b/src/ProtocolBuffers/ProtocolBuffersLite.WinRT.PCL.csproj new file mode 100644 index 0000000..eddda4a --- /dev/null +++ b/src/ProtocolBuffers/ProtocolBuffersLite.WinRT.PCL.csproj @@ -0,0 +1,92 @@ + + + + + 12.0 + Debug + AnyCPU + {82181C67-7B33-4286-A575-42B9A2815979} + Library + Properties + Google.ProtocolBuffersLite + Google.ProtocolBuffersLite + en-US + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Profile32 + v4.6 + + + true + full + false + bin\portable-win81+wpa81\Debug\ + TRACE;DEBUG;PORTABLE_LIBRARY;WINDOWS_RUNTIME;NOSERIALIZABLE;NOSORTEDLIST;LITE + prompt + 4 + + + pdbonly + true + bin\portable-win81+wpa81\Release\ + TRACE;PORTABLE_LIBRARY;WINDOWS_RUNTIME;NOSERIALIZABLE;NOSORTEDLIST;LITE + prompt + 4 + false + bin\portable-win81+wpa81\Release\Google.ProtocolBuffersLite.xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ProtocolBuffers/TextFormat.cs b/src/ProtocolBuffers/TextFormat.cs index 747dce4..fbc0529 100644 --- a/src/ProtocolBuffers/TextFormat.cs +++ b/src/ProtocolBuffers/TextFormat.cs @@ -214,7 +214,7 @@ private static void PrintFieldValue(FieldDescriptor field, object value, TextGen case FieldType.Fixed64: // The simple Object.ToString converts using the current culture. // We want to always use the invariant culture so it's predictable. - generator.Print(((IConvertible)value).ToString(FrameworkPortability.InvariantCulture)); + generator.Print(((IFormattable)value).ToString(null, FrameworkPortability.InvariantCulture)); break; case FieldType.Bool: // Explicitly use the Java true/false