Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attributes on an interface

I have a interface that defines some methods with attributes. These attributes need to be accessed from the calling method, but the method I have does not pull the attributes from the interface. What am I missing?

public class SomeClass: ISomeInterface
{
    MyAttribute GetAttribute()
    {
        StackTrace stackTrace = new StackTrace();
        StackFrame stackFrame = stackTrace.GetFrame(1);
        MethodBase methodBase = stackFrame.GetMethod();
        object[] attributes = methodBase.GetCustomAttributes(typeof(MyAttribute), true);
        if (attributes.Count() == 0)
            throw new Exception("could not find MyAttribute defined for " + methodBase.Name);
        return attributes[0] as MyAttribute;
    }

    void DoSomething()
    {
        MyAttribute ma = GetAttribute();
        string s = ma.SomeProperty;
    }
}
like image 481
Thad Avatar asked Oct 30 '08 21:10

Thad


People also ask

What are the properties of an interface?

The byte code of an interface appears in a . All of the methods in an interface are abstract. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. An interface is not extended by a class; it is implemented by a class.

CAN interface have attributes C#?

No. Whenever implementing an interface or overriding members in a derived class, you need to re-declare the attributes.

CAN interface have attributes C++?

C++ has no distinct "interface", just abstract classes. In an interface in eg. Java, it's just not possible to have variables.

What should an interface contain?

What does an interface contain? Explanation: Interface contains the only declaration of the method.


2 Answers

The methodBase will be the method on the class, not the interface. You will need to look for the same method on the interface. In C# this is a little simpler (since it must be like-named), but you would need to consider things like explicit implementation. If you have VB code it will be trickier, since VB method "Foo" can implement an interface method "Bar". To do this, you would need to investigate the interface map:

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
interface IFoo
{
    void AAA(); // just to push Bar to index 1
    [Description("abc")]
    void Bar();
}
class Foo : IFoo
{
    public void AAA() { } // just to satisfy interface
    static void Main()
    {
        IFoo foo = new Foo();
        foo.Bar();
    }
    void IFoo.Bar()
    {
        GetAttribute();
    }

    void GetAttribute()
    { // simplified just to obtain the [Description]

        StackTrace stackTrace = new StackTrace();
        StackFrame stackFrame = stackTrace.GetFrame(1);
        MethodBase classMethod = stackFrame.GetMethod();
        InterfaceMapping map = GetType().GetInterfaceMap(typeof(IFoo));
        int index = Array.IndexOf(map.TargetMethods, classMethod);
        MethodBase iMethod = map.InterfaceMethods[index];
        string desc = ((DescriptionAttribute)Attribute.GetCustomAttribute(iMethod, typeof(DescriptionAttribute))).Description;
    }
}
like image 162
Marc Gravell Avatar answered Sep 20 '22 16:09

Marc Gravell


Mark's method will work for non-generic interfaces. But it appears that I am dealing with some that have generics

interface IFoo<T> {}
class Foo<T>: IFoo<T>
{
  T Bar()
}

It appears that the T is replaced with the actual classType in the map.TargetMethods.

like image 27
Thad Avatar answered Sep 18 '22 16:09

Thad