Mscorlib.dll


At least for v2 and earlier, mscorlib.dll is a special case. That causes it and its types to be loaded differently from other assemblies.


Loading Mscorlib.dll Without a Path
It and the execution engine are so closely integrated that it’s required that they match each other. So, once a version of the CLR is chosen, the version of mscorlib is also chosen automatically. That means that, when late binding (Assembly.Load(), etc.) for it without a path, the ‘official’ mscorlib Assembly will always be returned – regardless of the version that was requested. The same thing goes for loading by static reference to it.


This doesn’t mean that it’s okay to give an incomplete or incorrect reference for it! If/when mscorlib.dll is factored away from the execution engine, it won’t be such a special case and bad references will start to fail to load.


Reflecting On Another Mscorlib.dll
It’s possible to load another mscorlib.dll if it’s loaded by path. But, it’s not possible to load its types, due to optimizations done by the CLR’s execution engine. (Same goes for ReflectionOnlyLoadFrom() on another mscorlib.dll.) This means that even if you rename the mscorlib.dll that you want to reflect over, it may be loaded, but its types will still be unloadable. So, the options for working around this are:



  • Reflect on the ‘official’ mscorlib.dll. You could change the version of the CLR that your app loads in order to reflect on the desired mscorlib version.
  • Use the unmanaged metadata API to get the desired info.
  • Create an asmmeta file for the desired mscorlib.dll and parse it at runtime.

 

Comments (28)

  1. Phil says:

    Could you take a look at my comment here about Assembly.Load? Also, is the source code in the SSCLI for the threading & networking services the same in both the SSCLI and the .NET framework?

  2. Phil says:

    Oops I meant the comment in your post on Assembly.Codebase and Location.

  3. Suzanne says:

    Sure. Sorry, my responses are sometimes delayed, since I’m busy working on the product, not technical support. If you need a quicker response, you may want to consider talking to Microsoft’s official tech support channels. Microsoft’s official newsgroups are also good, since some non-MS MVP’s may also answer your questions quickly (in addition to the MS people who answer).

    Anyway, yes, the code for that is basically the same for both (though, of course, it takes time for changes from the CLR to propagate to SSCLI).

  4. Can you give me some pointers (articles, links to examples) about the unmanaged metadata API you mentioned at the end of your article, please?
    What newsgroups or forums I could ask about these kind of problems?

    Many thanks
    Cris

  5. Suzanne says:

    Try ms-help://MS.MSDNQTR.2003OCT.1033/dnmag00/html/metadata.htm from MSDN, or search for ".NET Unmanaged Metadata API" on the web.

    For newsgroups, try microsoft.public.dotnet.* (the CLR newsgroup is best for questions about metadata).

  6. Krisztian Gyuris says:

    Thanks Suzanne!

  7. binal dalal says:

    I am working in vb.net. I am getting an eror "Object reference not set to an instance of an object " which its error source in mscorlib. I am getting this error while working with the farpoint grid which has one of a columns as checkbox .When the statement .value = 1 (which checks the checkbox) it throws this error

    plz help me out

  8. Suzanne says:

    Binal Dalal, please keep comments here related to the loader. See http://blogs.msdn.com/suzcook/archive/2003/12/05/57253.aspx for general support.

  9. Kostas says:

    i have visual C++ 6 and not exist mscorlib…

    Why;

    help please

  10. Suzanne says:

    It’s not there because the CLR is not installed. VS6 is pre-.NET. VS.NET starts with version 7.

  11. Jason G says:

    I’m still reading through your archives, but what is a asmmeta file? Google returns 0 hits, and I can’t find it in MSDN.

  12. eddi says:

    Hello!

    I have unmanaged C++ code which I want to be managed, and than I want to make web service from that managed code. The best way to do it?

    Thanks!

  13. siva says:

    i got this problem, after downloading the sample code from msdn/

  14. Saqib says:

    I am a programmer of vb6 . now i am using VB.Net . i am trying to communicate the machine on serial port using vb.net. But i am facing a Error "Exception from HRESULT: 0x800A1F45". Remember i am using MSCOMM32 for serial communication. as we did in vb6.0

    File Include as Refenece

    (1)Interop.MSCommLib

    (2)mscorlib

    Plz Help me as soon as posible

    Regards

    Saqib

  15. Vachan says:

    I had faced this type of exception

    And i have no idea for what reason this will propogate..

    Please do reply me,

    ——————————————–

    Exception #1 Source: mscorlib

    Invalid length for a Base-64 char array.

    at System.Convert.FromBase64String(String s)

    at System.Web.UI.LosFormatter.Deserialize(String input)

    at System.Web.UI.Page.LoadPageStateFromPersistenceMedium()

    ——————————————–

  16. Andy says:

    What exactly is mscorlib all about? Is this the base building block for the primary namespaces in .Net?

    Can I open it and view it’s code?

  17. I am havin visual studio1.1. Can i use System.Collections.Generic Namespace??

  18. KORAY says:

    i am using .NET 2003 ,i install to my computer crystal report 10 and then all my reports in the project return to crystal report 10.

    And when i create a setup project All files dependency is mscorlib

    WHAT CAN I DO TO SOLVE THIS PROBLEM

  19. Venkatesh.K says:

    hai,

      While using the Crystal Report XI in ASP.NET there is an Error  Not Enough Memory for operation.

  20. By "Frameworks assemblies," I mean the assemblies that ship with the CLR. But, I’m not counting mscorlib.dll

  21. Mel says:

    I’m getting this error:

    Description:  Input string was not in correct format

    Source:  mscorlib

    Can you tell me what this might be about

  22. Rams says:

    Hi

     I am using Vb.net. whenever i call the ***.dll i got the following error

    mscorlib:Could not find file ‘C:Outputdebug.ddd’.

    and also

    Exception has been thrown by the target of an invocation.

  23. Saurabh Jain says:

    I want to know that when we double click a .net exe, how does the Hosting API gets called for loading the CLR in the process.

    Thanks and Regards

    Saurabh Jain

    itsaurabh@yahoo.com

  24. Ramesh H. Sahoo says:

    mscorlib is working on application at developmnent but not working after publish in the same machine.

    Please give me a suitable solution for the same.

    Thanks & regards

    Ramesh H. Sahoo

  25. Milano says:

    I see that mscorlib is integrated closely with CLR. That must be why errors thrown by mscorlib are so hard to trace.

    One such error that we face at our client’s site is the infamous ‘Handle is invalid error’. Give below is stack trace.

    mscorlib

    The handle is invalid.

    at System.IO.__Error.WinIOError(Int32 errorCode, String str)

    at System.IO.FileStream.ReadCore(Byte[] buffer, Int32 offset, Int32 count)

    at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count)

    at System.Xml.XmlStreamReader.Read(Byte[] data, Int32 offset, Int32 length)

    at System.Xml.XmlScanner.Read()

    at System.Xml.XmlScanner.ScanName()

    at System.Xml.XmlScanner.ScanMarkup()

    at System.Xml.XmlScanner.ScanToken(Int32 expected)

    at System.Xml.XmlTextReader.SetElementValues()

    at System.Xml.XmlTextReader.ParseElement()

    at System.Xml.XmlTextReader.Read()

    at System.Xml.XmlValidatingReader.ReadNoCollectTextToken()

    at System.Xml.XmlValidatingReader.Read()

    at System.Xml.XmlLoader.LoadCurrentNode()

    at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)

    at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)

    at System.Xml.XmlDocument.Load(XmlReader reader)

    at System.Xml.XmlDocument.Load(String filename)

    This happens occasionally while reading an xml document and seems to have no particular cause since at other times the same code executes successfully.

  26. Paul Smith says:

    The following error message results after an attempt to save a VB 2005 form–"Could not load type ‘System.Byte’ from assembly ‘mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ because the format is invalid. error message."

    Can you tell me what is causing this error?  (The software is on the hard drive and the application is stored on the server.)  And, I want to know how to correct this problem.