POSTSHARP: How to disable postsharp for Onentry and Onexit functions, basically need to enable/disable these functions for debug mode in web.config
You can disable PostSharp completely for Debug build configuration on the PostSharp page of the project properties. Look for "Disable PostSharp for this configuration" setting at the top of the page.
If you want to disable only some of the aspects, then you can do so using C# directives around the applied attributes:
#if !DEBUG
[MyAspect]
#endif
public class MyClass
// ...
You cannot control the build process with settings in web.config, however. You can use it to control execution at application run-time, and skip some of the aspects this way (but they still will be weaved into your code and invoked at run-time):
[Serializable]
public class MyAspect : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
if ( ConfigurationManager.AppSettings["DisableMyAspect"] == "true" )
return;
// ...
}
}
Related
Well, I don't get it. Trying to apply a PostSharp attribute for exception handling on a method, the aspect's OnException method is simply not reached by any means. This is very disappointing.
For the sake of testing, let's say this is the aspect:
[PSerializable]
public class HandleExceptionsAttribute : OnExceptionAspect
{
public override void OnException(MethodExecutionArgs args)
{
Trace.TraceError(args.Exception.Message);
args.FlowBehavior = FlowBehavior.Return;
}
}
And it is applied on a method that only throws an exception:
[HandleExceptions]
public static void Test()
{
throw new Exception("Test");
}
Well, nothing happens. The aspect's OnException method is never reached at all (via debugging or otherwise).
What am I missing?
Having the PostSharp NuGet package installed, the aspect code should be reached, but break-points within the aspect code might not be reached. You need to have PostSharp Tools for Visual Studio installed to make debugging of aspect code work. You can download it at https://www.postsharp.net/download.
I know this was aksed and answered a a couple of times e.g.
Solution-wide #define, Is There anyway to #define Constant on a Solution Basis? and How to define a constant globally in C# (like DEBUG).
But in my case I can not use any of the suggested methods:
I'm writing on different "modules" (or plugins if you want so) for UnityProjects (kind of a package providing a certain functionality). The idea is that a developer can load a certain "module" to use in his project by importing a UnityPackage with all scripts and resources in it.
But some of these modules themselves depend on other modules. So what I tried so far was having a class Constants in each module with seperated namespaces and preprocessor definitions.
Module A
#if !MODULE_A
#define MODULE_A // BUT I WOULD NEED THIS GLOBAL NOT ONLY HERE
#endif
namespace Module_A
{
public static class Constants
{
// some constants for this namespace here
}
}
Module B
#if !MODULE_B
#define MODULE_B // BUT I WOULD NEED THIS GLOBAL NOT ONLY HERE
#endif
#if !MODULE_A // WILL BE NOT DEFINED OFCOURSE SINCE #define IS NOT GLOBAL
#error Module A missing!
#else
namespace Module_B
{
public static class Constants
{
// some constants for this namespace here
}
// and other code that might require Module A
}
#endif
But ofcourse this cannot work like this since #defines are not global but only in the current file.
Problem
For this whole idea of modules and a simple "load your modules" I can not ask the user to first make changes to the project or solution settings how e.g. suggested by this answer but instead have to use only the (c#) resources that come imported with the UnityPackage (at least with my current know-how).
Is there any way to somehow set/define those constants for the entire Unity-Project by only importing the module's UnityPackage?
Edit:
I could find a solution for 1 definition in Unity using Assets/msc.rsp. But this still wouldn't work for multiple modules since they would have to write into the same file.
After a lot of searches I've finally been able to put together a surprisingly simple solution I'ld like to share with you:
InitializeOnLoad
Unity has an attribute [InitializeOnLoad]. It tells Unity to initialize according class as soon as
Unity is launched
After any re-compiling of scripts => also after importing a new unitypackage with scripts
static Constructor
In their Running Editor Code On Launch example, they show, how to combine this with a static constructor.
From static-constructors:
A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
While usually you still would have to create an instance of the class, the static constructor is "instanciated/executed" instantly when the class is initliazed, which we force using the [InitializeOnLoad] attribute.
Scripting Define Symbols
Further Unity actually has project wide defines in the PlayerSettings.
And the good part is: We also have access to them via scripting API:
PlayerSettings.GetScriptingDefineSymbolsForGroup
PlayerSettings.SetScriptingDefineSymbolsForGroup.
So what I did now is the following
Module A
This module has no dependencies but just defines a "global define" in the PlayerSettings. I placed this script somewhere e.g. in Assets/ModuleA/Editor (important is the last folder's name).
using System.Linq;
using UnityEditor;
namespace ModuleA
{
// Will be initialized on load or recompiling
[InitializeOnLoad]
public static class Startup
{
// static constructor is called as soon as class is initialized
static Startup()
{
#region Add Compiler Define
// Get the current defines
// returns a string like "DEFINE_1;DEFINE_2;DEFINE_3"
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup);
// split into list just to check if my define is already there
var define = defines.Split(';').ToList();
if (!define.Contains("MODULE_A")
{
// if not there already add my define
defines += ";MODULE_A";
}
// and write back the new defines
PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, defines);
#endregion
}
}
}
Module B
This module depends on Module A. So itself defines a "global define" (so later Modules can check their dependecies on Module B) but additionally it checks first, if Module A is imported. If Module A is missing, it prints an error to the Debug Console.
(You could as well throw a compiler error using #error SOME TEXT, but for some reason this is not capable of printing out the URL correctly so I decided for the Debug.LogError)
I placed this script somewhere e.g. in Assets/ModuleB/Editor
#if MODULE_A
using System.Linq;
#endif
using UnityEditor;
#if !MODULE_A
using UnityEngine;
#endif
namespace ModuleB
{
// Will be initialized on load or recompiling
[InitializeOnLoad]
public static class Startup
{
// static constructor is called as soon as class is initialized
static Startup()
{
#if !MODULE_A
Debug.LogErrorFormat("! Missing Module Dependency !" +
"\nThe module {0} depends on the module {1}." +
"\n\nDownload it from {2} \n",
"MODULE_B",
"MODULE_A",
"https://Some.page.where./to.find.it/MyModules/ModuleA.unitypackage"
);
#else
// Add Compiler Define
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup);
var define = defines.Split(';').ToList();
if (!define.Contains("MODULE_B"))
{
defines += ";MODULE_B";
}
PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, defines);
#endif
}
}
}
So later in other scripts of Module B I have two options (both do basically the same)
I can either check everywhere #if MODULE_A to check exactly the module this script relies on
or I can instead check #if MODULE_B to rather check with one line if all dependecies are fulfilled since otherwise I don't define MODULE_B.
On this way I can completely check all dependencies between certain modules which is awesome. The only two flaws I saw until now are:
We have to know how the define (e.g. MODULE_A) looks like for every module and if it is changed in the future it has to be changed in all depending modules as well
The "global define" isn't getting removed in case the module is deleted from the project
But well - which solution is perfect?
In general, the way I would solve this problem in C# is by defining a common set of interfaces that all your modules would contain. I think you can do this with Unity by placing the files from each module in the same location, thus allowing later installations to overwrite those same files (with, obviously, the same content). You would then put editor controls that expose properties to hold instances of those interfaces and then wire them up in the UI. You would test those properties for a value of null to determine which ones are missing.
Common.cs:
public interface IModuleA {}
public interface IModuleB {}
ModuleA.cs
public class ModuleA : IModuleA {}
ModuleB.cs
public class ModuleB : IModuleB
{
public IModuleA ModuleAInstance {get; set;}
private bool IsModuleAPresent()
{
return !ModuleAInstance == null;
}
}
The ideal way to solve it would be with a package manager and proper dependency injection, but doing that with Unity is not straightforward.
I'm currently creating a unit custom JUnit runner (which will precisely call custom code before/after each test method) e.g.
class MyRunner extends BlockJUnit4ClassRunner {
private MyApi api = new MyApi();
public MyRunner(Class<?> klass) throws InitializationError {
super(klass);
}
// todo
}
However, I would like to support other runners e.g. MockitoJunitRunner and SpringRunner, so instead of reinventing the wheel, I'd like to use my runner like the following (using a custom MyConfig annotation to specify existing test runners): -
#RunWith(MyRunner.class)
#MyConfig(testRunner=MockitoJUnitRunner.class)
public class MockitoRunnerTest {
}
... or ...
#RunWith(MyRunner.class)
#MyConfig(testRunner=SpringRunner.class)
public class MockitoRunnerTest {
}
This means the test runner is very light i.e. it's like a Junit rule and simply proxies to another existing Junit runner after calling it's own code.
My gut feeling is that this has already be implemented/solved - just having problems finding it.
NOTE: I want to avoid using rules due to these problems - see Apply '#Rule' after each '#Test' and before each '#After' in JUnit
I am looking to add my aspect to the system.io namespace, I already searches lots of solutions which didn't do the trick but basically I want to handle exceptions and do some logging on the File class from System.IO namespace. Just like adding [MyAspect] to the top of the class..
Is there a way?
Let's consider aspect like this:
[PSerializable]
public class MyAspect : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
}
}
You can apply this aspect to any 3rd assembly called from your project by applying MyAspect:
[assembly: MyAspect(AttributeTargetAssemblies = "mscorlib", AttributeTargetTypes = "System.IO.*")]
When you specify this assembly attribute then PostSharp decorates all calls from your project to methods from System.IO namespace in mscorlib assembly with MyAspect.
We have implemented a SonarQube-plugin with an extension that implements Decorator and deployed it to extensions/plugins.
Unfortunately, when triggering the sonar-analysis, the extension implementation is not called. Is there anything more that needs to be configured?
The implementation looks as simple as this:
public class MyPlugin extends SonarPlugin {
#Override
public List getExtensions() {
return Arrays.asList(MyExt.class);
}
}
#DependsUpon(DecoratorBarriers.ISSUES_TRACKED)
public class MyExt implements Decorator, BatchComponent {
#Override
public void decorate(Resource resource, DecoratorContext decoratorContext) {
Project project = decoratorContext.getProject();
Measure measure = new Measure();
measure.setData("abc");
decoratorContext.saveMeasure(measure);
}
#Override
public boolean shouldExecuteOnProject(Project project) {
return true;
}
}
If you're using SQ 5.2, Decorator does not work anymore. You have to move to the MeasureComputer interface instead.
The API is really unclear about this, and the #Deprecated annotation is misused IMHO (usually you first deprecate code, keep it working until it does not work anymore, then you just delete it... you don't deprecate code in order to inform people that it's not usable anymore...)
I guess that somebody at SonarSource forgot what code deprecation is about...
While a deprecated software feature remains in the software, its use may raise warning messages recommending alternative practices; deprecated status may also indicate the feature will be removed in the future. Features are deprecated rather than immediately removed, to provide backward compatibility and give programmers time to bring affected code into compliance with the new standard.