.NET Reflection: Determing whether a method is unsafe

  • Thread starter Thread starter Manish Soni
  • Start date Start date
M

Manish Soni

I am loading an assembly using reflection.
Then I get all the methods in all types.
I want to check whether a method is "unsafe".
The MethodInfo class does not seem to provide this information.


How can I get the info?

Thanks and Regards
Manish Soni
 
Manish,

Unfortunately, you can not tell this on the method level (think, how
would you determine if there was an unsafe block of code inside the
method?).

The best you can do is check the module that the method on the type is
in. On the module, check to see if it has the UnverifiableCode attribute
attached to it. If it does, then this means that the compiler flag was set
to allow unsafe code (however, this doesn't mean that there necessarily is
unsafe code).

Hope this helps.
 
The SDK comes with a tool called PEVerify.exe. See if you can run this
on your assembly before using reflection to load it into your application.

You might want to use the methods in Process class (from
System.Diagnostics namespace) to run PEVerify.exe.

eg., assume your test assembly is called abcd.dll
so, you'd run...

PEVerify.exe abcd.dll /md > results.txt

now, parse through the results.txt to see if it came up with any errors.
If the code is verifiably safe, you'd come up with something like...
===========================================================
Microsoft (R) .NET Framework PE Verifier Version 1.1.4322.573
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

All Classes and Methods in abcd.dll Verified.
===========================================================

Hope this was of some help.
-Azhagan.
 
Back
Top