Here's a patch implementing function assembly::is-assembly(assemblyFilename). 

I didn't not test it very extensively, so feedback welcome. Especially
I noticed (if I'm not wrong) undocumented behavior, as
AssemblyName.GetAssemblyName seems to throw a FileLoadException
sometimes, where msdn doesn't state this can happen.


cheers

Thibaut Barr�re
Index: src/NAnt.Core/Functions/AssemblyFunctions.cs
===================================================================
RCS file: /cvsroot/nant/nant/src/NAnt.Core/Functions/AssemblyFunctions.cs,v
retrieving revision 1.7
diff -u -r1.7 AssemblyFunctions.cs
--- src/NAnt.Core/Functions/AssemblyFunctions.cs        2 Oct 2004 08:50:25 
-0000       1.7
+++ src/NAnt.Core/Functions/AssemblyFunctions.cs        22 Nov 2004 21:59:25 
-0000
@@ -23,6 +23,7 @@
 using System.Collections;
 using System.Reflection;
 using System.Globalization;
+using System.Security;
 
 using NAnt.Core;
 using NAnt.Core.Types;
@@ -93,7 +94,47 @@
         public static Assembly LoadFromFile(string assemblyFile) {
             return Assembly.LoadFrom(assemblyFile);
         }
-        
-        #endregion Public Static Methods
-    }
+
+               /// <summary>
+               /// Check if a given file is an assembly.
+               /// </summary>
+               /// <param name="assemblyFile">The name or path of the file to 
be checked.</param>
+               /// <returns>True if the file is a valid assembly, false if 
it's not or if the assembly seems corrupted (invalid headers or 
metadata).</returns>
+               /// <exception cref="ArgumentNullException"><paramref 
name="assemblyFile" /> is a null <see cref="string"/>.</exception>
+               /// <exception cref="ArgumentException"><paramref 
name="assemblyFile" /> is an empty <see cref="string"/>.</exception>
+               /// <exception cref="FileNotFoundException"><paramref 
name="assemblyFile" /> is not found, or the file you are trying to check does 
not specify a filename extension.</exception>
+               /// <exception cref="SecurityException">The caller does not 
have path discovery permission.</exception>
+               /// <exception cref="FileLoadException">The file could not be 
loaded for some reason.</exception>
+               [Function("is-assembly")]
+               public static bool IsAssembly(string assemblyFile) 
+               {
+                       try
+                       {
+                               AssemblyName.GetAssemblyName(assemblyFile);
+                               // no exception occurred, this is an assembly
+                               return true;
+                       }
+                       catch (FileLoadException fle)
+                       {
+                               if ( (fle.InnerException!=null) && 
(fle.InnerException.GetType()==typeof(BadImageFormatException)) )
+                               {
+                                       // this is probably not an assembly, or 
it has invalid headers / metadata
+                                       return false;
+                               }
+                               // other exceptions should be thrown to the 
end-user
+                               throw;
+                       }
+                       catch (BadImageFormatException)
+                       {
+                               return false;
+                       }
+                       catch (Exception)
+                       {
+                               // other exceptions should be thrown to the 
end-user
+                               throw;
+                       }
+               }
+               
+               #endregion Public Static Methods
+       }
 }

Reply via email to