467,075 Members | 1,044 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

.NET Reflection: Determing whether a method is unsafe

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
Nov 16 '05 #1
  • viewed: 2634
Share:
2 Replies
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Manish Soni" <ms***@microsoft.com> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #2
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.

"Manish Soni" <ms***@microsoft.com> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Rlrcstr | last post: by
2 posts views Thread by Stefan | last post: by
6 posts views Thread by Robin Haswell | last post: by
2 posts views Thread by wizofaus@hotmail.com | last post: by
7 posts views Thread by =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.