Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6389849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:27:19+00:00 2026-05-25T03:27:19+00:00

I’m trying to create a custom attribute that will work in a sort-of AOP

  • 0

I’m trying to create a custom attribute that will work in a sort-of AOP way (I don’t have access to postsharp, unfortunately and I’m not very familiar with Unity). It has AttributeUsage.Method and in its constructor it configures some parts of the test environment (pulls some info out of app.config and calls some exes that config the environment).

It works, except that right now, when I build the solution, the attribute is executed – which is undesireable.

Is there a way to create a custom attribute that isn’t executed at compile time?

edit> I guess an example usage might help:

public void Scenario1Tests
{
    [Test]
    [Environment(Environments.A)]
    public void Scenario1TestA()
    {
        Assert.Something();
    }

    [Test]
    [Environment(Environments.Any)]
    public void Scenario1TestB()
    {
        Assert.SomethingElse();
    }
}

// Most tests will be written environment independent, some must not
public enum Environments
{
    A, 
    B, 
    Any
};

[AtrributeUsage(AttributeTargets.Method)]
public void Environment : Attribute
{
    public Environment(Environments env)
    {
        // lots of test can have this attribute, requirement is 
        // that it is only configured once as it is a lengthy configuration
        if (this.EnvironmentIsAlreadyConfigured())
            return;

        this.GetSettingsFromAppConfig();
        Process.Start(/* ... */); // can take 20 mins+
    }        

    public Environment()
        : this(Environments.Any)
    {
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:27:20+00:00Added an answer on May 25, 2026 at 3:27 am

    The usual method is to use the attribute strictly as a marker. You configure it in the constructor, but take no actions. Then, at runtime, you inspect the methods via reflection, extract the configuration information from the attribute, and take the appropriate actions based on that information.

    For example, if you wanted an attribute that checked parameters for null prior to executing the method, you could create it like so:

    [AttributeUsage(AttributeTargets.Method)]
    public class CheckArgumentsNullAttribute : Attribute
    {
        public CheckArgumentsNullAttribute() { }
    }
    

    Then, mark your method as such:

    [CheckArgumentsNull]
    public Foo(object o) { Console.WriteLine(o.ToString()); }
    

    Then, in your code, get the Foo method via reflection, and check it for the attribute:

    MethodInfo m = typeof(FooClass).GetMethod("Foo");
    if (m.GetCustomAttributes(typeof(CheckArgumentsNullAttribute), false).Length > 0)
    {
        // Check parameters for null here
    }
    

    Update: Since you need this in a scenario where MSTest is running the method, you should take a look at this article, which discusses how to hook into the testing process. Basically, you need to extended from ContextBoundObject, and intercept the method calls to perform the attribute processing you want.

    Update 2: Given what the attribute does, I would probably just make the environment setup a method, and call it from the beginning of the appropriate tests. I don’t think you gain all that much by having an attribute. Alternatively, you could divide your fixtures by environment, and have the environment setup/teardown performed in the fixture setup/teardown. Either way is probably a lot easier than trying to make AOP work here, especially given the “do it once” nature of the attribute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.