Blog Stats
  • Posts - 99
  • Articles - 5
  • Comments - 43
  • Trackbacks - 108

 

Tuesday, May 09, 2006

ICollection and ICollection<>

Eralier I posted (and deleted) about the Queue class not implementing ICollection from some research this is by design.

From: http://msdn2.microsoft.com/en-US/library/92t2ye13.aspx

"Some collections that limit access to their elements, like the Queue class and the Stack class, directly implement the ICollection interface."

If you look, the interfaces are also very different from each other in what they include. The generic one includes methods such as ... Add, Remove, and Clear which do not exist on the non-generic ICollection.

So conclusion .. Queue is correct but I have to say that having the generic and non-generic ICollections that represent completely different things is a bit confusing at best :-/

Random Useful Code

I don't remember where I got this (so whoever you are thanks but I can't link to you update .. http://groups.google.de/group/microsoft.public.de.german.entwickler.dotnet.framework/msg/1af0719168e45e84) some time ago but it is a quite useful (and portable) way to see if a file is a .NET assembly or not ... If you fall back to the Assembly.LoadFrom method .. you have to fail many times if you are searching many files ... In looking at this again (had nearly forgotten about it until today). It works by checking the PE/COFF http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx header of the file.

I told myself that I would clean it up a bit, make less exceptions happen if files were not big enough etc .. guess I have to put that on my board. 

static bool IsClrImage( string fileName ) {

      FileStream fs = null;

      try {

            fs = new FileStream( fileName, FileMode.Open, FileAccess.Read,

                  FileShare.Read );

            byte[] dat = new byte[300];

            fs.Read( dat, 0, 128 );

            if( (dat[0] != 0x4d) || (dat[1] != 0x5a) ) // "MZ" DOS header

                  return false;

 

            int lfanew = BitConverter.ToInt32( dat, 0x3c );

            fs.Seek( lfanew, SeekOrigin.Begin );

            fs.Read( dat, 0, 24 ); // read signature & PE file header

            if( (dat[0] != 0x50) || (dat[1] != 0x45) ) // "PE" signature

                  return false;

 

            fs.Read( dat, 0, 96 + 128 ); // read PE optional header

            if( (dat[0] != 0x0b) || (dat[1] != 0x01) ) // magic

                  return false;

 

            int clihd = BitConverter.ToInt32( dat, 208 ); // get IMAGE_COR20_HEADER

            rva-address

                  return clihd != 0;

      }

      catch( Exception ) {

            return false;

      }

      finally {

            if( fs != null )

                  fs.Close();

      }

}

 

 

Copyright © Greg Young