Implement ILGenerator on Unix to use System.Console.dll
authorstephentoub <stoub@microsoft.com>
Wed, 10 Jun 2015 13:24:43 +0000 (09:24 -0400)
committerstephentoub <stoub@microsoft.com>
Wed, 10 Jun 2015 14:35:32 +0000 (10:35 -0400)
The System.Console type in mscorlib on Unix is internal and lacks any ability to write out.  As such, System.Reflection.Emit.ILGenerator.EmitWriteLine needs to use the System.Console from System.Console.dll.

src/mscorlib/src/System/Reflection/Emit/ILGenerator.cs

index 4c44eb0..fa6d539 100644 (file)
@@ -1174,6 +1174,17 @@ namespace System.Reflection.Emit
             this.Emit(OpCodes.Throw);
         }
 
+        private static Type GetConsoleType()
+        {
+#if FEATURE_LEGACYSURFACE
+            return typeof(Console);
+#else
+            return Type.GetType(
+                "System.Console, System.Console, Version=4.0.0.0, Culture=neutral, PublicKeyToken=" + AssemblyRef.MicrosoftPublicKeyToken, 
+                throwOnError: true);
+#endif
+        }
+
         public virtual void EmitWriteLine(String value)
         {
             // Emits the IL to call Console.WriteLine with a string.
@@ -1181,7 +1192,7 @@ namespace System.Reflection.Emit
             Emit(OpCodes.Ldstr, value);
             Type[] parameterTypes = new Type[1];
             parameterTypes[0] = typeof(String);
-            MethodInfo mi = typeof(Console).GetMethod("WriteLine", parameterTypes);
+            MethodInfo mi = GetConsoleType().GetMethod("WriteLine", parameterTypes);
             Emit(OpCodes.Call, mi);
         }
 
@@ -1198,7 +1209,7 @@ namespace System.Reflection.Emit
                 throw new ArgumentException(Environment.GetResourceString("InvalidOperation_BadILGeneratorUsage"));
             }
 
-            MethodInfo prop = typeof(Console).GetMethod("get_Out");
+            MethodInfo prop = GetConsoleType().GetMethod("get_Out");
             Emit(OpCodes.Call, prop);
             Emit(OpCodes.Ldloc, localBuilder);
             Type[] parameterTypes = new Type[1];
@@ -1230,7 +1241,7 @@ namespace System.Reflection.Emit
             }
             Contract.EndContractBlock();
             
-            MethodInfo prop = typeof(Console).GetMethod("get_Out");
+            MethodInfo prop = GetConsoleType().GetMethod("get_Out");
             Emit(OpCodes.Call, prop);
 
             if ((fld.Attributes & FieldAttributes.Static)!=0) {