From dc0aeaa9030bdac264b44d56d07b6839a1ae94e9 Mon Sep 17 00:00:00 2001 From: detlevschwabe Date: Mon, 27 Jun 2016 22:31:42 -0700 Subject: Adding conditional compiler symbol to support .NET 3.5 (#1713) * Adding condition compiler symbol to support .NET 3.5 --- .../Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs | 3 ++- .../Google.Protobuf/Compatibility/PropertyInfoExtensions.cs | 8 ++++++++ csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs | 6 ++++++ csharp/src/Google.Protobuf/JsonFormatter.cs | 11 +++++++++++ csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs | 4 ++++ csharp/src/Google.Protobuf/WellKnownTypes/FieldMaskPartial.cs | 5 +++++ csharp/src/Google.Protobuf/WellKnownTypes/TimeExtensions.cs | 4 ---- 7 files changed, 36 insertions(+), 5 deletions(-) (limited to 'csharp') diff --git a/csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs b/csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs index f0c8d3bc..359c72c8 100644 --- a/csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs +++ b/csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs @@ -67,7 +67,7 @@ namespace Google.Protobuf.Compatibility { Assert.AreEqual(expected, TypeExtensions.IsValueType(type)); } - +#if !DOTNET35 [Test] [TestCase(typeof(object), typeof(string), true)] [TestCase(typeof(object), typeof(int), true)] @@ -129,5 +129,6 @@ namespace Google.Protobuf.Compatibility { Assert.Throws(() => TypeExtensions.GetMethod(type, name)); } +#endif } } diff --git a/csharp/src/Google.Protobuf/Compatibility/PropertyInfoExtensions.cs b/csharp/src/Google.Protobuf/Compatibility/PropertyInfoExtensions.cs index 8a6fefa7..e3914dd3 100644 --- a/csharp/src/Google.Protobuf/Compatibility/PropertyInfoExtensions.cs +++ b/csharp/src/Google.Protobuf/Compatibility/PropertyInfoExtensions.cs @@ -47,7 +47,11 @@ namespace Google.Protobuf.Compatibility /// internal static MethodInfo GetGetMethod(this PropertyInfo target) { +#if DOTNET35 + var method = target.GetGetMethod(); +#else var method = target.GetMethod; +#endif return method != null && method.IsPublic ? method : null; } @@ -57,7 +61,11 @@ namespace Google.Protobuf.Compatibility /// internal static MethodInfo GetSetMethod(this PropertyInfo target) { +#if DOTNET35 + var method = target.GetSetMethod(); +#else var method = target.SetMethod; +#endif return method != null && method.IsPublic ? method : null; } } diff --git a/csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs b/csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs index 762a29eb..fe9cda8d 100644 --- a/csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs +++ b/csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs @@ -49,6 +49,11 @@ namespace Google.Protobuf.Compatibility /// Returns true if the target type is a value type, including a nullable value type or an enum, or false /// if it's a reference type (class, delegate, interface - including System.ValueType and System.Enum). /// +#if DOTNET35 + internal static bool IsValueType(this Type target) { + return target.IsValueType; + } +#else internal static bool IsValueType(this Type target) { return target.GetTypeInfo().IsValueType; @@ -109,5 +114,6 @@ namespace Google.Protobuf.Compatibility } return null; } +#endif } } diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs index 2aa98cd1..a894ffa1 100644 --- a/csharp/src/Google.Protobuf/JsonFormatter.cs +++ b/csharp/src/Google.Protobuf/JsonFormatter.cs @@ -885,6 +885,16 @@ namespace Google.Protobuf return originalName; } +#if DOTNET35 + // TODO: Consider adding functionality to TypeExtensions to avoid this difference. + private static Dictionary GetNameMapping(System.Type enumType) => + enumType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static) + .ToDictionary(f => f.GetValue(null), + f => (f.GetCustomAttributes(typeof(OriginalNameAttribute), false) + .FirstOrDefault() as OriginalNameAttribute) + // If the attribute hasn't been applied, fall back to the name of the field. + ?.Name ?? f.Name); +#else private static Dictionary GetNameMapping(System.Type enumType) => enumType.GetTypeInfo().DeclaredFields .Where(f => f.IsStatic) @@ -893,6 +903,7 @@ namespace Google.Protobuf .FirstOrDefault() // If the attribute hasn't been applied, fall back to the name of the field. ?.Name ?? f.Name); +#endif } } } diff --git a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs index f5a835e5..7a1cb9d5 100644 --- a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs @@ -34,6 +34,10 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +#if DOTNET35 +// Needed for ReadOnlyDictionary, which does not exist in .NET 3.5 +using Google.Protobuf.Collections; +#endif namespace Google.Protobuf.Reflection { diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/FieldMaskPartial.cs b/csharp/src/Google.Protobuf/WellKnownTypes/FieldMaskPartial.cs index 4bd62cf3..f9cdb8af 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/FieldMaskPartial.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/FieldMaskPartial.cs @@ -59,7 +59,12 @@ namespace Google.Protobuf.WellKnownTypes if (firstInvalid == null) { var writer = new StringWriter(); +#if DOTNET35 + var query = paths.Select(JsonFormatter.ToCamelCase); + JsonFormatter.WriteString(writer, string.Join(",", query.ToArray())); +#else JsonFormatter.WriteString(writer, string.Join(",", paths.Select(JsonFormatter.ToCamelCase))); +#endif return writer.ToString(); } else diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/TimeExtensions.cs b/csharp/src/Google.Protobuf/WellKnownTypes/TimeExtensions.cs index dd485d32..8b63d630 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/TimeExtensions.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/TimeExtensions.cs @@ -31,10 +31,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Google.Protobuf.WellKnownTypes { -- cgit v1.2.3