Determine unsafe member using reflection?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When a type's member is marked as unsafe
unsafe Int32* Ptr(){

Is it possible, using reflection, to determine that the member is unsafe

The best I could come up with for the above example is
methodInfo.ReturnType.IsPointe

Thanks

don
 
Don,
When a type's member is marked as unsafe:
unsafe Int32* Ptr(){}

Is it possible, using reflection, to determine that the member is unsafe?
The best I could come up with for the above example is:
methodInfo.ReturnType.IsPointer

That's a good start. You should also check any parameter types the
same way.

But then it's perfectly legal to use the unsafe keyword even if it's
not needed, for example

unsafe void Foo() {}

and there's no way you can detect that.



Mattias
 
Mattias,

Thanks for the reply.

If there's no way to determine programmatically where an unsafe construct
exists (short of the appearance of pointer types in a member signature) how
does the CLR detect an unsafe code block in a compiled assembly? Is there
something emitted in the MSIL?

don
 
Donald,
If there's no way to determine programmatically where an unsafe construct
exists (short of the appearance of pointer types in a member signature)
how does the CLR detect an unsafe code block in a compiled assembly?

It doesn't, really. "unsafe" code is a C# concept.

The C# compiler will, however, add the attribute

[assembly: SecurityPermission(SecurityAction.RequestMinimum,
SkipVerification=true)]

to your assembly when you compile with /unsafe.



Mattias
 
Back
Top