0

Is there any way to call the CallCommonCode() attribute constructor automatically when CodedMethod() is called?

using System;

[AttributeUsage(AttributeTargets.Method|AttributeTargets.Struct,
                   AllowMultiple=false,Inherited=false)]
public class CallCommonCode : Attribute
{
    public CallCommonCode() { Console.WriteLine("Common code is not called!"); }
}

public class ConcreteClass {

  [CallCommonCode]
  protected void CodedMethod(){
     Console.WriteLine("Hey, this stuff does not work :-(");
  }
  public static void Main(string[] args)
  {
     new ConcreteClass().CodedMethod();
  }
}
4
  • Ô.o Why do you want to do such a thing? Commented Feb 7, 2013 at 16:05
  • @Aniket Sounds like you want to employ something like Aspect Oriented Programming. Lucky for you, you can actually do this with some tooling like PostSharp: sharpcrafters.com Commented Feb 7, 2013 at 16:05
  • @DHN well, I am trying to up my skills in C#, learning as much as I can. I might also want to do something like MVC in WebForms Commented Feb 7, 2013 at 16:09
  • @Aniket Puh ok thought you want to try something very odd. ;o) Well, sircodesalot is right. :o) Commented Feb 7, 2013 at 16:27

1 Answer 1

2

No, because the attribute exists independently of the function. You can scan for the attribute or you can execute the function, but you can't do both at the same time.

The point of an attribute is just to tag stuff with extra metadata, but it's not strictly speaking actually part of the code itself.

What you would normally do in this situation is scan for the tag on the function, and if it goes against your business logic, you would throw some sort of exception. But in general, an attribute is just a 'tag'.

class Program
{
    [Obsolete]
    public void SomeFunction()
    {

    }

    public static void Main()
    {
        // To read an attribute, you just need the type metadata, 
        // which we can get one of two ways.
        Type typedata1 = typeof(Program);       // This is determined at compile time.
        Type typedata2 = new Program().GetType(); // This is determined at runtime


        // Now we just scan for attributes (this gets attributes on the class 'Program')
        IEnumerable<Attribute> attributesOnProgram = typedata1.GetCustomAttributes();

        // To get attributes on a method we do (This gets attributes on the function 'SomeFunction')
        IEnumerable<Attribute> methodAttributes = typedata1.GetMethod("SomeFunction").GetCustomAttributes();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

so how do we circumvent that? how do [Dllimport] and [Obsolete] attributes work then?
Visual Studio monitors those. Attributes are not technically part of code, someone somewhere has to monitor them. I'll write an example.
Good answer. <here a banana>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.