aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2016-07-09 07:16:06 +0100
committerGitHub <noreply@github.com>2016-07-09 07:16:06 +0100
commit8779cba302f330f478ce10c4e58a34e7b6360471 (patch)
tree9894d31379ffcb851ed705e476b164d668864097
parentc404c2a2e3e9a05096fc86f080b6d966b2b0043c (diff)
parent3df146e198705dee11de890577f004e42126cd70 (diff)
downloadprotobuf-8779cba302f330f478ce10c4e58a34e7b6360471.tar.gz
protobuf-8779cba302f330f478ce10c4e58a34e7b6360471.tar.bz2
protobuf-8779cba302f330f478ce10c4e58a34e7b6360471.zip
Merge pull request #1764 from jskeet/remove-is-value-type
Remove unnecessary reflection call
-rw-r--r--csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs21
-rw-r--r--csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs17
-rw-r--r--csharp/src/Google.Protobuf/FieldCodec.cs3
3 files changed, 6 insertions, 35 deletions
diff --git a/csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs b/csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs
index 359c72c8..f430b06b 100644
--- a/csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs
+++ b/csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs
@@ -34,6 +34,7 @@ using System;
using System.Collections.Generic;
using System.Reflection;
+#if !DOTNET35
namespace Google.Protobuf.Compatibility
{
public class TypeExtensionsTest
@@ -51,24 +52,6 @@ namespace Google.Protobuf.Compatibility
}
[Test]
- [TestCase(typeof(int), true)]
- [TestCase(typeof(int?), true)]
- [TestCase(typeof(Nullable<>), true)]
- [TestCase(typeof(WireFormat.WireType), true)]
- [TestCase(typeof(string), false)]
- [TestCase(typeof(object), false)]
- [TestCase(typeof(Enum), false)]
- [TestCase(typeof(ValueType), false)]
- [TestCase(typeof(TypeExtensionsTest), false)]
- [TestCase(typeof(Action), false)]
- [TestCase(typeof(Action<>), false)]
- [TestCase(typeof(IDisposable), false)]
- 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)]
[TestCase(typeof(string), typeof(string), true)]
@@ -129,6 +112,6 @@ namespace Google.Protobuf.Compatibility
{
Assert.Throws<AmbiguousMatchException>(() => TypeExtensions.GetMethod(type, name));
}
-#endif
}
}
+#endif
diff --git a/csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs b/csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs
index fe9cda8d..2d93183b 100644
--- a/csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs
+++ b/csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs
@@ -33,6 +33,7 @@
using System;
using System.Reflection;
+#if !DOTNET35
namespace Google.Protobuf.Compatibility
{
/// <summary>
@@ -46,20 +47,6 @@ namespace Google.Protobuf.Compatibility
internal static class TypeExtensions
{
/// <summary>
- /// 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;
- }
-
- /// <summary>
/// See https://msdn.microsoft.com/en-us/library/system.type.isassignablefrom
/// </summary>
internal static bool IsAssignableFrom(this Type target, Type c)
@@ -114,6 +101,6 @@ namespace Google.Protobuf.Compatibility
}
return null;
}
-#endif
}
}
+#endif
diff --git a/csharp/src/Google.Protobuf/FieldCodec.cs b/csharp/src/Google.Protobuf/FieldCodec.cs
index 98313088..c28b47e1 100644
--- a/csharp/src/Google.Protobuf/FieldCodec.cs
+++ b/csharp/src/Google.Protobuf/FieldCodec.cs
@@ -347,7 +347,8 @@ namespace Google.Protobuf
public sealed class FieldCodec<T>
{
private static readonly T DefaultDefault;
- private static readonly bool TypeSupportsPacking = typeof(T).IsValueType() && Nullable.GetUnderlyingType(typeof(T)) == null;
+ // Only non-nullable value types support packing. This is the simplest way of detecting that.
+ private static readonly bool TypeSupportsPacking = default(T) != null;
static FieldCodec()
{