2007-10-27 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / class / corlib / System / Exception.cs
index f439ec2fdf97f24c5f53ce8e1ce67139e22745a8..97a6d5bd3fd9d77d644c89aa93a72282ffb289ae 100644 (file)
@@ -35,26 +35,36 @@ using System.Text;
 using System.Runtime.InteropServices;
 using System.Runtime.CompilerServices;
 using System.Runtime.Serialization;
+using System.Security.Permissions;
 
 namespace System
 {
        [Serializable]
+#if NET_2_0
+       [ComVisible(true)]
+       [ComDefaultInterface (typeof (_Exception))]
+       [ClassInterface (ClassInterfaceType.None)]
+#else
        [ClassInterface (ClassInterfaceType.AutoDual)]
+#endif
        public class Exception : ISerializable 
 #if NET_2_0
        , _Exception
 #endif
        {
+               #region Sync with object-internals.h
                IntPtr [] trace_ips;
                Exception inner_exception;
-               string message;
+               internal string message;
                string help_link;
                string class_name;
                string stack_trace;
                string remote_stack_trace;
                int remote_stack_index;
-               int hresult = unchecked ((int)0x80004005);
+               internal int hresult = unchecked ((int)0x80004005);
                string source;
+               private IDictionary _data;
+               #endregion
 
                public Exception ()
                {
@@ -121,6 +131,9 @@ namespace System
                }
 
                public virtual string Source {
+#if ONLY_1_1
+                       [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
+#endif
                        get {
                                if (source == null) {
                                        StackTrace st = new StackTrace (this, true);
@@ -129,7 +142,7 @@ namespace System
                                                if (st != null) {
                                                        MethodBase method = sf.GetMethod ();
                                                        if (method != null) {
-                                                               source = method.DeclaringType.Assembly.GetName ().Name;
+                                                               source = method.DeclaringType.Assembly.UnprotectedGetName ().Name;
                                                        }
                                                }
                                        }
@@ -146,22 +159,22 @@ namespace System
 
                public virtual string StackTrace {
                        get {
-                               if (trace_ips == null)
-                                       /* Not thrown yet */
-                                       return null;
-
                                if (stack_trace == null) {
-                                       StackTrace st = new StackTrace (this, true);
+                                       if (trace_ips == null)
+                                               /* Not thrown yet */
+                                               return null;
+
+                                       StackTrace st = new StackTrace (this, 0, true, true);
 
                                        StringBuilder sb = new StringBuilder ();
 
-                                       string newline = String.Format ("{0}{1} ", Environment.NewLine, Locale.GetText ("in"));
+                                       string newline = String.Format ("{0}  {1} ", Environment.NewLine, Locale.GetText ("at"));
                                        string unknown = Locale.GetText ("<unknown method>");
 
                                        for (int i = 0; i < st.FrameCount; i++) {
                                                StackFrame frame = st.GetFrame (i);
                                                if (i == 0)
-                                                       sb.AppendFormat ("{0} ", Locale.GetText ("in"));
+                                                       sb.AppendFormat ("  {0} ", Locale.GetText ("at"));
                                                else
                                                        sb.Append (newline);
 
@@ -172,16 +185,17 @@ namespace System
                                                        else
                                                                sb.AppendFormat ("<0x{0:x5}> {1}", frame.GetNativeOffset (), unknown);
                                                } else {
+                                                       sb.Append (GetFullNameForStackTrace (frame.GetMethod ()));
+
                                                        if (frame.GetILOffset () == -1)
-                                                               sb.AppendFormat ("<0x{0:x5}> ", frame.GetNativeOffset ());
+                                                               sb.AppendFormat (" <0x{0:x5}> ", frame.GetNativeOffset ());
                                                        else
-                                                               sb.AppendFormat ("[0x{0:x5}] ", frame.GetILOffset ());
+                                                               sb.AppendFormat (" [0x{0:x5}] ", frame.GetILOffset ());
+
                                                        string fileName = frame.GetFileName ();
                                                        if (fileName != null)
-                                                               sb.AppendFormat ("(at {0}:{1}) ", fileName, frame.GetFileLineNumber ());
-
-                                                       sb.Append (GetFullNameForStackTrace (frame.GetMethod ()));
-                                               }
+                                                               sb.AppendFormat ("in {0}:{1} ", fileName, frame.GetFileLineNumber ());
+                                                       }
                                        }
                                        stack_trace = sb.ToString ();
                                }
@@ -191,6 +205,9 @@ namespace System
                }
 
                public MethodBase TargetSite {
+#if ONLY_1_1
+                       [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
+#endif
                        get {
                                StackTrace st = new StackTrace (this, true);
                                if (st.FrameCount > 0)
@@ -201,8 +218,6 @@ namespace System
                }
 
 #if NET_2_0
-               private IDictionary _data;
-
                public virtual IDictionary Data {
                        get {
                                if (_data == null) {
@@ -229,11 +244,18 @@ namespace System
                        return this;
                }
 
+#if ONLY_1_1
+               [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
+#endif
+               [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
                public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
                {
+                       if (info == null)
+                               throw new ArgumentNullException ("info");
+
                        if (class_name == null)
                                class_name = GetType ().FullName;
-                       
+
                        info.AddValue ("ClassName", class_name);
                        info.AddValue ("Message", message);
                        info.AddValue ("InnerException", inner_exception);
@@ -246,6 +268,9 @@ namespace System
                        info.AddValue ("ExceptionMethod", null);
                }
 
+#if ONLY_1_1
+               [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
+#endif
                public override string ToString ()
                {
                        System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
@@ -283,22 +308,22 @@ namespace System
 
                internal string GetFullNameForStackTrace (MethodBase mi)
                {
-                       string parms = "";
+                       string parms = String.Empty;
                        ParameterInfo[] p = mi.GetParameters ();
                        for (int i = 0; i < p.Length; ++i) {
                                if (i > 0)
                                        parms = parms + ", ";
-                               string paramName = (p [i].Name == null) ? "" : (" " + p [i].Name);
+                               string paramName = (p [i].Name == null) ? String.Empty : (" " + p [i].Name);
                                Type pt = p[i].ParameterType;
-                               if (pt.IsClass && pt.Namespace != "")
+                               if (pt.IsClass && pt.Namespace != String.Empty)
                                        parms = parms + pt.Namespace + "." + pt.Name + paramName;
                                else
                                        parms = parms + pt.Name + paramName;
                        }
 
-                       string generic = "";
+                       string generic = String.Empty;
 #if NET_2_0 || BOOTSTRAP_NET_2_0
-                       if (mi.HasGenericParameters) {
+                       if (mi.IsGenericMethod) {
                                Type[] gen_params = mi.GetGenericArguments ();
                                generic = "[";
                                for (int j = 0; j < gen_params.Length; j++) {
@@ -309,7 +334,20 @@ namespace System
                                generic += "]";
                        }
 #endif
-                       return mi.DeclaringType.ToString () + ":" + mi.Name + generic + " (" + parms + ")";
-               }                               
+                       return mi.DeclaringType.ToString () + "." + mi.Name + generic + " (" + parms + ")";
+               }
+
+#if NET_2_0
+               //
+               // The documentation states that this is available in 1.x,
+               // but it was not available (MemberRefing this would fail)
+               // and it states the signature is `override sealed', but the
+               // correct value is `newslot' 
+               //
+               public new Type GetType ()
+               {
+                       return base.GetType ();
+               }
+#endif
        }
 }