The Labs \ Source Viewer \ SSCLI \ System \ TypeLoadException

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*=============================================================================
  16. **
  17. ** Class: TypeLoadException
  18. **
  19. **
  20. ** Purpose: The exception class for type loading failures.
  21. **
  22. **
  23. =============================================================================*/
  24. namespace System
  25. {
  26.    
  27.     using System;
  28.     using System.Globalization;
  29.     using System.Runtime.Remoting;
  30.     using System.Runtime.Serialization;
  31.     using System.Runtime.CompilerServices;
  32.     using System.Security.Permissions;
  33.    
  34.     [Serializable()]
  35.     [System.Runtime.InteropServices.ComVisible(true)]
  36.     public class TypeLoadException : SystemException, ISerializable
  37.     {
  38.        
  39.         public TypeLoadException() : base(Environment.GetResourceString("Arg_TypeLoadException"))
  40.         {
  41.             SetErrorCode(__HResults.COR_E_TYPELOAD);
  42.         }
  43.        
  44.         public TypeLoadException(string message) : base(message)
  45.         {
  46.             SetErrorCode(__HResults.COR_E_TYPELOAD);
  47.         }
  48.        
  49.         public TypeLoadException(string message, Exception inner) : base(message, inner)
  50.         {
  51.             SetErrorCode(__HResults.COR_E_TYPELOAD);
  52.         }
  53.        
  54.         public override string Message {
  55.             get {
  56.                 SetMessageField();
  57.                 return _message;
  58.             }
  59.         }
  60.        
  61.         private void SetMessageField()
  62.         {
  63.             if (_message == null) {
  64.                 if ((ClassName == null) && (ResourceId == 0))
  65.                     _message = Environment.GetResourceString("Arg_TypeLoadException");
  66.                
  67.                 else {
  68.                     if (AssemblyName == null)
  69.                         AssemblyName = Environment.GetResourceString("IO_UnknownFileName");
  70.                     if (ClassName == null)
  71.                         ClassName = Environment.GetResourceString("IO_UnknownFileName");
  72.                    
  73.                     _message = String.Format(CultureInfo.CurrentCulture, GetTypeLoadExceptionMessage(ResourceId), ClassName, AssemblyName, MessageArg);
  74.                 }
  75.             }
  76.         }
  77.        
  78.         public string TypeName {
  79.             get {
  80.                 if (ClassName == null)
  81.                     return String.Empty;
  82.                
  83.                 return ClassName;
  84.             }
  85.         }
  86.        
  87.         // This is called from inside the EE.
  88.         private TypeLoadException(string className, string assemblyName, string messageArg, int resourceId) : base(null)
  89.         {
  90.             SetErrorCode(__HResults.COR_E_TYPELOAD);
  91.             ClassName = className;
  92.             AssemblyName = assemblyName;
  93.             MessageArg = messageArg;
  94.             ResourceId = resourceId;
  95.            
  96.             // Set the _message field eagerly; debuggers look at this field to
  97.             // display error info. They don't call the Message property.
  98.             SetMessageField();
  99.         }
  100.        
  101.         protected TypeLoadException(SerializationInfo info, StreamingContext context) : base(info, context)
  102.         {
  103.             if (info == null)
  104.                 throw new ArgumentNullException("info");
  105.            
  106.             ClassName = info.GetString("TypeLoadClassName");
  107.             AssemblyName = info.GetString("TypeLoadAssemblyName");
  108.             MessageArg = info.GetString("TypeLoadMessageArg");
  109.             ResourceId = info.GetInt32("TypeLoadResourceID");
  110.         }
  111.        
  112.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  113.         private static extern string GetTypeLoadExceptionMessage(int resourceId);
  114.        
  115.         //We can rely on the serialization mechanism on Exception to handle most of our needs, but
  116.         //we need to add a few fields of our own.
  117.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  118.         public override void GetObjectData(SerializationInfo info, StreamingContext context)
  119.         {
  120.             if (info == null)
  121.                 throw new ArgumentNullException("info");
  122.            
  123.             base.GetObjectData(info, context);
  124.             info.AddValue("TypeLoadClassName", ClassName, typeof(string));
  125.             info.AddValue("TypeLoadAssemblyName", AssemblyName, typeof(string));
  126.             info.AddValue("TypeLoadMessageArg", MessageArg, typeof(string));
  127.             info.AddValue("TypeLoadResourceID", ResourceId);
  128.         }
  129.        
  130.         // If ClassName != null, GetMessage will construct on the fly using it
  131.         // and ResourceId (mscorrc.dll). This allows customization of the
  132.         // class name format depending on the language environment.
  133.         private string ClassName;
  134.         private string AssemblyName;
  135.         private string MessageArg;
  136.         internal int ResourceId;
  137.     }
  138. }

Developer Fusion