I am using Unity Container for interception and so am trying to add a [Trace] attribute, e.g.
public class TraceAttribute : HandlerAttribute
and then in code, I can use it like so:
public class MyClass
{
[Trace]
public void DoSomething()
{
...
}
}
Here's my question/goal: I don't want the Trace attribute to be tied to the Unity Interception. If I later change DI containers, or want to implement my own, etc. I'd like to swap it out, much like we do for interfaces.
something like:
public interface TraceAttribute : ITraceAttribute
and then have a specific implementation? is that possible?
One of the challenges we are facing is that everywhere I use the Trace attribute, I have to have Unity assembly installed, and I'd like to avoid that.
Thanks!
In C# attribute needs to be a class which is directly or indirectly derived from class System.Attribute
. This means that using standard tools you cannot use interface as an attribute.
For your scenario what you could do is create an attribute, which has the way to create required instance from Unity. If you are using this for MVC filters, you can see the scenario on now to set this up here. For other cases it might be more complicated, but you can still achieve it using some kind of service locator.