aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp
diff options
context:
space:
mode:
authorGravatar Jon Skeet <jonskeet@google.com>2018-03-29 17:13:52 +0100
committerGravatar Jon Skeet <skeet@pobox.com>2018-04-06 08:05:32 +0100
commitda3ce6717156c8b4f8890207895069ce6b102f65 (patch)
treeb7216368389616b4fd1b4bce07b2f7a71e1c1fb5 /csharp
parenta72da27f5a46844e1a70725bb4362c2e51d7195d (diff)
Deliberately call simple code to avoid Unity linker pruning
The SampleEnumMethod method was previously only called via reflection, so the Unity linker thought it could be removed. Ditto the parameterless constructor in ReflectionHelper. This PR should avoid that issue, reducing the work needed by customers to use Google.Protobuf from Unity.
Diffstat (limited to 'csharp')
-rw-r--r--csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs19
1 files changed, 19 insertions, 0 deletions
diff --git a/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs b/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs
index 1afaa90c..80d5c774 100644
--- a/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs
+++ b/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs
@@ -163,6 +163,8 @@ namespace Google.Protobuf.Reflection
{
try
{
+ PreventLinkerFailures();
+ // Try to do the conversion using reflection, so we can see whether it's supported.
MethodInfo method = typeof(ReflectionUtil).GetMethod(nameof(SampleEnumMethod));
// If this passes, we're in a reasonable runtime.
method.CreateDelegate(typeof(Func<int>));
@@ -174,6 +176,23 @@ namespace Google.Protobuf.Reflection
}
}
+ /// <summary>
+ /// This method is effectively pointless, but only called once. It's present (and called)
+ /// to avoid the Unity linker from removing code that's only called via reflection.
+ /// </summary>
+ private static void PreventLinkerFailures()
+ {
+ // Exercise the method directly. This should avoid any pro-active linkers from stripping
+ // the method out.
+ SampleEnum x = SampleEnumMethod();
+ if (x != SampleEnum.X)
+ {
+ throw new InvalidOperationException("Failure in reflection utilities");
+ }
+ // Make sure the ReflectionHelper parameterless constructor isn't removed...
+ var helper = new ReflectionHelper<int, int>();
+ }
+
public enum SampleEnum
{
X