Skip to content

Commit

Permalink
Adding conditional compiler symbol to support .NET 3.5 (protocolbuffe…
Browse files Browse the repository at this point in the history
…rs#1713)

* Adding condition compiler symbol to support .NET 3.5
  • Loading branch information
detlevschwabe authored and jskeet committed Jun 28, 2016
1 parent 7b5648c commit dc0aeaa
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void IsValueType(Type type, bool expected)
{
Assert.AreEqual(expected, TypeExtensions.IsValueType(type));
}

#if !DOTNET35
[Test]
[TestCase(typeof(object), typeof(string), true)]
[TestCase(typeof(object), typeof(int), true)]
Expand Down Expand Up @@ -129,5 +129,6 @@ public void GetMethod_Ambiguous(Type type, string name)
{
Assert.Throws<AmbiguousMatchException>(() => TypeExtensions.GetMethod(type, name));
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ internal static class PropertyInfoExtensions
/// </summary>
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;
}

Expand All @@ -57,7 +61,11 @@ internal static MethodInfo GetGetMethod(this PropertyInfo target)
/// </summary>
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;
}
}
Expand Down
6 changes: 6 additions & 0 deletions csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ internal static class TypeExtensions
/// 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).
/// </summary>
#if DOTNET35
internal static bool IsValueType(this Type target) {
return target.IsValueType;
}
#else
internal static bool IsValueType(this Type target)
{
return target.GetTypeInfo().IsValueType;
Expand Down Expand Up @@ -109,5 +114,6 @@ internal static MethodInfo GetMethod(this Type target, string name)
}
return null;
}
#endif
}
}
11 changes: 11 additions & 0 deletions csharp/src/Google.Protobuf/JsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,16 @@ internal static string GetOriginalName(object value)
return originalName;
}

#if DOTNET35
// TODO: Consider adding functionality to TypeExtensions to avoid this difference.
private static Dictionary<object, string> 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<object, string> GetNameMapping(System.Type enumType) =>
enumType.GetTypeInfo().DeclaredFields
.Where(f => f.IsStatic)
Expand All @@ -893,6 +903,7 @@ private static Dictionary<object, string> GetNameMapping(System.Type enumType) =
.FirstOrDefault()
// If the attribute hasn't been applied, fall back to the name of the field.
?.Name ?? f.Name);
#endif
}
}
}
4 changes: 4 additions & 0 deletions csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
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
{
Expand Down
5 changes: 5 additions & 0 deletions csharp/src/Google.Protobuf/WellKnownTypes/FieldMaskPartial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ internal static string ToJson(IList<string> paths, bool diagnosticOnly)
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
Expand Down
4 changes: 0 additions & 4 deletions csharp/src/Google.Protobuf/WellKnownTypes/TimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit dc0aeaa

Please sign in to comment.