Commit 3df146e1 authored by Jon Skeet's avatar Jon Skeet

Remove unnecessary reflection call

This is the only call to TypeExtensions.IsValueType, so we can remove
that method, making the whole type conditionally compiled out for .NET 3.5
parent 8b00675f
...@@ -34,6 +34,7 @@ using System; ...@@ -34,6 +34,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
#if !DOTNET35
namespace Google.Protobuf.Compatibility namespace Google.Protobuf.Compatibility
{ {
public class TypeExtensionsTest public class TypeExtensionsTest
...@@ -50,24 +51,6 @@ namespace Google.Protobuf.Compatibility ...@@ -50,24 +51,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] [Test]
[TestCase(typeof(object), typeof(string), true)] [TestCase(typeof(object), typeof(string), true)]
[TestCase(typeof(object), typeof(int), true)] [TestCase(typeof(object), typeof(int), true)]
...@@ -129,6 +112,6 @@ namespace Google.Protobuf.Compatibility ...@@ -129,6 +112,6 @@ namespace Google.Protobuf.Compatibility
{ {
Assert.Throws<AmbiguousMatchException>(() => TypeExtensions.GetMethod(type, name)); Assert.Throws<AmbiguousMatchException>(() => TypeExtensions.GetMethod(type, name));
} }
#endif
} }
} }
#endif
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
using System; using System;
using System.Reflection; using System.Reflection;
#if !DOTNET35
namespace Google.Protobuf.Compatibility namespace Google.Protobuf.Compatibility
{ {
/// <summary> /// <summary>
...@@ -45,20 +46,6 @@ namespace Google.Protobuf.Compatibility ...@@ -45,20 +46,6 @@ namespace Google.Protobuf.Compatibility
/// </summary> /// </summary>
internal static class TypeExtensions 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> /// <summary>
/// See https://msdn.microsoft.com/en-us/library/system.type.isassignablefrom /// See https://msdn.microsoft.com/en-us/library/system.type.isassignablefrom
/// </summary> /// </summary>
...@@ -114,6 +101,6 @@ namespace Google.Protobuf.Compatibility ...@@ -114,6 +101,6 @@ namespace Google.Protobuf.Compatibility
} }
return null; return null;
} }
#endif
} }
} }
#endif
...@@ -347,7 +347,8 @@ namespace Google.Protobuf ...@@ -347,7 +347,8 @@ namespace Google.Protobuf
public sealed class FieldCodec<T> public sealed class FieldCodec<T>
{ {
private static readonly T DefaultDefault; 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() static FieldCodec()
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment