Address ReflectionTypeLoadException feedback (#15738)
authorJustin Van Patten <jvp@justinvp.com>
Fri, 5 Jan 2018 04:42:58 +0000 (20:42 -0800)
committerJan Kotas <jkotas@microsoft.com>
Fri, 5 Jan 2018 04:42:58 +0000 (20:42 -0800)
* Address ReflectionTypeLoadException feedback

Address additional minor feedback from #15711:

 - Share the code for `Message` and `ToString`
 - Remove trailing `NewLine` from resulting string
 - Pass initial base string to `StringBuilder..ctor`
 - Cache the exceptions array in a local (passed-in as a parameter)
 - Move methods below properties
 - Remove trailing whitespace

src/mscorlib/shared/System/Reflection/ReflectionTypeLoadException.cs

index d78c068..5011c50 100644 (file)
@@ -27,7 +27,7 @@ namespace System.Reflection
             HResult = HResults.COR_E_REFLECTIONTYPELOAD;
         }
 
-        private ReflectionTypeLoadException(SerializationInfo info, StreamingContext context) 
+        private ReflectionTypeLoadException(SerializationInfo info, StreamingContext context)
             : base(info, context)
         {
             LoaderExceptions = (Exception[])(info.GetValue("Exceptions", typeof(Exception[])));
@@ -40,48 +40,34 @@ namespace System.Reflection
             info.AddValue("Exceptions", LoaderExceptions, typeof(Exception[]));
         }
 
-        public override string Message
+        public Type[] Types { get; }
+
+        public Exception[] LoaderExceptions { get; }
+
+        public override string Message => CreateString(isMessage: true);
+
+        public override string ToString() => CreateString(isMessage: false);
+
+        private string CreateString(bool isMessage)
         {
-            get
-            {
-                if (LoaderExceptions == null || LoaderExceptions.Length == 0)
-                {
-                    return base.Message;
-                }
+            string baseValue = isMessage ? base.Message : base.ToString();
 
-                StringBuilder text = new StringBuilder();
-                text.AppendLine(base.Message);
-                foreach (Exception e in LoaderExceptions)
-                {
-                    if (e != null)
-                    {
-                        text.AppendLine(e.Message);
-                    }
-                }
-                return text.ToString();
+            Exception[] exceptions = LoaderExceptions;
+            if (exceptions == null || exceptions.Length == 0)
+            {
+                return baseValue;
             }
-        }
 
-        public override string ToString()
-        {
-            StringBuilder text = new StringBuilder();
-            text.AppendLine(base.ToString());
-            if (LoaderExceptions != null)
+            var text = new StringBuilder(baseValue);
+            foreach (Exception e in exceptions)
             {
-                foreach (Exception e in LoaderExceptions)
+                if (e != null)
                 {
-                    if (e != null)
-                    {
-                        text.AppendLine(e.ToString());
-                    }
+                    text.AppendLine();
+                    text.Append(isMessage ? e.Message : e.ToString());
                 }
             }
-
             return text.ToString();
         }
-
-        public Type[] Types { get; }
-
-        public Exception[] LoaderExceptions { get; }
     }
 }