Ianmac45 Posted May 19, 2007 Share Posted May 19, 2007 is it possible to retrieve the name of the target ... member that an attribute is applied to? for ex, [STAThread] void Main() { } [Obsolete] int OldMethod() { } [SomeAttr] private int AField; [SomeAttr] private int AProperty { get; set; } i would like to get the name "Main" / "OldMethod" / "AField" / "AProperty" from code but the attributes themselves don't directly reference their targets any suggestions? Link to comment https://www.neowin.net/forum/topic/561585-c-getting-attributes-target-name/ Share on other sites More sharing options...
0 Linkinfamous Posted May 19, 2007 Share Posted May 19, 2007 You need to use Reflection to do it. (using System.Reflection needs to be at the top for this) Create a Type object corresponding to your class, then iterate through each member of GetMembers() class SomethingAttribute : Attribute { } class Test { [Obsolete] public void DoSomething() { } [Something] public void DoSomethingElse() { } } static void Main() { Type t = typeof(Test); foreach (MemberInfo m in t.GetMembers()) { object[] a = m.GetCustomAttributes(false); if (a.Length != 0) { Console.WriteLine("Member {0} has the following attributes: ", m.Name); foreach (Attribute x in a) { Console.WriteLine(x.ToString()); } } } Console.ReadLine(); } You can fine tune it by saying m.GetCustomAttributes(typeof(ObsoleteAttribute), false), that will ensure that it only returns Attributes that are Obsolete. So, for example. you can write: if (m.GetCustomAttributes(typeof(ObsoleteAttribute), false).Length != 0) { Console.WriteLine("{0} is obsolete.", m.Name); } Link to comment https://www.neowin.net/forum/topic/561585-c-getting-attributes-target-name/#findComment-588564960 Share on other sites More sharing options...
0 Ianmac45 Posted May 19, 2007 Author Share Posted May 19, 2007 ... what i want to do is have the name of the target in a variable in the attribute like, the attribute would figure out by itself the name of its target Link to comment https://www.neowin.net/forum/topic/561585-c-getting-attributes-target-name/#findComment-588565655 Share on other sites More sharing options...
0 Linkinfamous Posted May 19, 2007 Share Posted May 19, 2007 Sorry. I don't follow what you're trying to do. I thought you wanted the name of a member based on attributes....and your description isn't helping me. Link to comment https://www.neowin.net/forum/topic/561585-c-getting-attributes-target-name/#findComment-588565662 Share on other sites More sharing options...
0 Ianmac45 Posted May 19, 2007 Author Share Posted May 19, 2007 this is what i've got. i want the attribute itself to be able to figure out the name of its target [AttributeUsage(AttributeTargets.Property)] public class DbParamAttribute : Attribute { public readonly string ParameterName; public readonly bool IsUpdatable; public DbParamAttribute() : this(true) { } public DbParamAttribute(bool updatable) { throw new NotImplementedException(); //how do i have the attribute figure //out the name of its target? ParameterName = null; IsUpdatable = updatable; } //provides explicit setting of db parameter name public DbParamAttribute(string parameter) : this(parameter, true) { } public DbParamAttribute(string parameter, bool updatable) { ParameterName = parameter; IsUpdatable = updatable; } } Link to comment https://www.neowin.net/forum/topic/561585-c-getting-attributes-target-name/#findComment-588565820 Share on other sites More sharing options...
0 Linkinfamous Posted May 20, 2007 Share Posted May 20, 2007 I'm so sorry. I still don't follow what you need, exactly. Your code won't work because of the 'readonly' modifier on the variables. This should help there....but I'm sorry. I still can't figure out what you need here: //how do i have the attribute figure //out the name of its target? ParameterName = null; IsUpdatable = updatable; private int x; public int X { get { return x; } private set { x = value; } } Link to comment https://www.neowin.net/forum/topic/561585-c-getting-attributes-target-name/#findComment-588566102 Share on other sites More sharing options...
Question
Ianmac45
is it possible to retrieve the name of the target ... member that an attribute is applied to?
for ex,
i would like to get the name "Main" / "OldMethod" / "AField" / "AProperty" from code
but the attributes themselves don't directly reference their targets
any suggestions?
Link to comment
https://www.neowin.net/forum/topic/561585-c-getting-attributes-target-name/Share on other sites
5 answers to this question
Recommended Posts