.NET Framework Bookmark and Share   
 index > .NET Base Class Library > MethodBase.GetCurrentMethod().DeclaringType throwing an exception (.NET 3.5, VS 2008) "Cannot evaluate security function"
 

MethodBase.GetCurrentMethod().DeclaringType throwing an exception (.NET 3.5, VS 2008) "Cannot evaluate security function"

MethodBase.GetCurrentMethod().DeclaringType throwing an exception ( on .NET 3.5, VS 2008)

Exception message is"Cannot evaluate security function". Can anybody give idea to fix this??

I think this is something related to Reflection permission

Kumar.DS06

In C#windows application I'm using "Log4net"thrid party toolto generate the log files;

With the belowcode i'mtrying to create the instance of logger to generate the log file

Private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

the above code snippet was working fine with.NET F/w 2.0; VS2005; Windows XP software configuration.

i Started migrating the application to .NET F/w 3.5 VS2008; Windows Vista Business OS.

but the code "MethodBase.GetCurrentMethod().DeclaringType" throwing an "System.ArgumentException"exception with a Exceptionmessage of"Cannot evaluate security function".

This exception is not caught; but it's just not generating the Log file.

The abovecode was working fine with .NET F/w 2.0; VS2005; Windows XP.

I solved this problem by adding the below line ofcode instead of above code.

"XmlConfigurator.Configure(new Filenfo(//app.config));"

where it will getthe logging details from app.config file. and creates the log file and works as before.

But i want to find out why the code "MethodBase.GetCurrentMethod().DeclaringType"doesn't work with .NET3.5, VS2008, Vista.

Kumar.DS06

Kumar.DS06,

Based on your post, there is an exception on GetCurrentMethod.Declaring of System.Reflection.MethodBase class. I notice that in this thread, even you run with the "Full Trust mode" cannnot solve the problem, either.

In order to make your question clearer and can be reproduced by others, please provide the code snippet and the system information such as Windows Vista, Windows XP in your reply.

In addition, I would like to provide you the following thread and article on using GetCurrentMethod.DeclaringType and the Refelction permission:

1. DeclaringType in Static Methods

Code Snippet

class Base

{

public string GetTypeName1()

{

return this.GetType().ToString();

}

public static string GetTypeName2()

{

return System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;

}

}

2. When is ReflectionPermission Needed?

Reflection and its interaction with security can sometimes be a bit of a confusing matter. The easiest portion to figure out is the permissions needed to use Reflection.Emit. In order to do anything with the reflection emit feature, you'll need to have ReflectionPermission with the ReflectionEmit flag set. In the default policy, you'll have this permission if you're running on LocalIntranet or MyComputer.

Hope that can help you.

Bruno Yu

In C#windows application I'm using "Log4net"thrid party toolto generate the log files;

With the belowcode i'mtrying to create the instance of logger to generate the log file

Private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

the above code snippet was working fine with.NET F/w 2.0; VS2005; Windows XP software configuration.

i Started migrating the application to .NET F/w 3.5 VS2008; Windows Vista Business OS.

but the code "MethodBase.GetCurrentMethod().DeclaringType" throwing an "System.ArgumentException"exception with a Exceptionmessage of"Cannot evaluate security function".

This exception is not caught; but it's just not generating the Log file.

The abovecode was working fine with .NET F/w 2.0; VS2005; Windows XP.

I solved this problem by adding the below line ofcode instead of above code.

"XmlConfigurator.Configure(new Filenfo(//app.config));"

where it will getthe logging details from app.config file. and creates the log file and works as before.

But i want to find out why the code "MethodBase.GetCurrentMethod().DeclaringType"doesn't work with .NET3.5, VS2008, Vista.

Kumar.DS06

Kumar.DS06,

Thanks for your reply with the solution. I believe this will help others using MethodBase.GetCurrentMethod().DeclaringType. As mentioned in your post, it is really strange to throw the SystemArgumentException with "Cannot evaluate security function" in Vista.

Based on your post, the exception is not caught but not generating the Log file. This may be a little confused. I suggest you to generate the information on the exception in "View Detail" of the Stack Trace and Inner Exception that can help you to check the problem further. You can also provide in your reply.

In addition, please make sure you have the full control with the current account in Vista that does have the related permission of manipulating the assemblies.

Bruno Yu

You can use google to search for other answers

Custom Search

More Threads

• C# Windows Service Requires Network Share
• writing text into a .jpeg
• SQL Bitmap Type to System.Drawing.Bitmap
• BackgroundWorker Implementation in seperate class
• Total noob help needed. Could somebody explain this line to me?
• sort generic collection
• Cannot delete a dll
• Threading
• BarcodeGenerator Class
• parallel port