Custom attributes in C #

I have a custom attribute for my page:

[PageDefinition("My page", "~/Parts/MyPage.aspx")]

      

My PageDefinition looks like this where AttributeItemDefinitions is set for Title, Url, IsPage and IsUserControl

public class PageDefinition : AttributeItemDefinitions
{
    public PageDefinition(string title, string url)
        : this()
    {
        Title = title;
        Url = Url;
    }

    public PageDefinition()
    {
        IsPage = true;
        IsUserControl = false;
    }
}

      

But I cannot find a good way to add an entire page with this attribute to a placeholder where all links should be listed with a title and url. Do you have a good idea? Thanks for your help.

+2


source to share


2 answers


When I created custom attributes that define some of the metadata in a class, I often created a little routine that scans all of the assembly classes using reflection.

In my current project, I am using IoC (another story) and instead of setting it up in a custom config file, ve created the ComponentAttribute itself, which determines which interface the class belongs to. (Bird's eye view: I'm asking for an IoC interface for an interface later and he knows how to instantiate classes that implement this and how they fit together)

To set up this IoC framework, I need to call a member of a specific class and specify which class for the interface is displayed.

  ioc.ConfigureMapping(classType, interfaceType)

      

To find all these mappings, I use the following two methods in one of my helper classes

 internal static void Configure(IoCContainer ioc, Assembly assembly)
 {
     foreach (var type in assembly.GetTypes())
          AddToIoCIfHasComponentAttribute(type, ioc);
 }

 internal static void AddToIoCIfHasComponentAttribute(Type type, IoC ioc)
 {
     foreach (ComponentAttribute att in type.GetCustomAttributes(typeof(ComponentAttribute), false))
     {
          ioc.ConfigureMapping(attribute.InterfaceType, type);
     }
 }

      

What I am doing here is listing all assembly types in the first method, rather than calculating the attribute in the second.

Back to your problem:

Using a similar approach, you can find all the marked classes and write them into a container (ArrayList or similar) along with all the data you defined in the attribute (page path, etc.).



Update (response to comment)

When you create your program in Visual Studio, you usually have one or more projects. For each project, you will receive a separate assembly (.dll or .exe file). The above code will walk through all the classes within a single assembly. You can see that the assembly is a collection of assembled .cs files. Therefore, you want to search for an assembly, not a directory of .cs files (which are source code, not part of a running application.)

So what is probably missing: how can you access the assembly from your code when you want to search for classes? You just take ANY class that you know (i.e. in the assembly / project where your other classes are located) and get the assembly it is in by calling

var assembly = typeof(MyDummyClass).Assembly;

      

after which you will call something that you got from the code above

AnalyzeClasses(assembly)

      

and AnalyzeClasses will look like

 internal static void AnalyzeClasses(Assembly assembly)
 {
     foreach (var type in assembly.GetTypes())
          AnalzyeSingleClass(type);
 }

 internal static void AnalzyeSingleClass(Type type)
 {
     foreach (MyCustomAttribute att in type.GetCustomAttributes(typeof(MyCustomAttribute), false))
     {
          Console.WriteLine("Found MyCustomAttribute with property {0} on class {1}",
                  att.MyCustomAttributeProperty,
                  type);
     }
 }

      

And you just call the whole thing before you run your application code like right at the top in main () (for applications), or if it's tricky in an advanced state, you can also call on demand when you need the collected data. (For example on the ASP.NET page)

+1


source


It may be more than you need, but ...

I came across this pattern all the time in my projects, so I implemented a type loader that can be provided with specific delegates defined to be searched by type.



http://www.codeproject.com/KB/architecture/RuntimeTypeLoader.aspx

0


source







All Articles