7

My question is there any way to retrieve the parameter list with its value using Reflection?

I want to use reflection to get the parameter list from the PropertyInfo.

 Author author = (Author)attribute;
 string name = author.name;

is not OK. As there will be many Attribute, which is not typeof Author.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property,  AllowMultiple = true)]
public class Author : Attribute
{
    public Author(string name, int v)
    {
        this.name = name;
        version = v;
    }

    public double version;
    public string name;
}

public class TestClass
{
    [Author("Bill Gates", 2)]
    public TextBox TestPropertyTextBox { get; set; }
}

4 Answers 4

5

using this program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("Reflecting TestClass");
            foreach (var property in typeof(TestClass).GetProperties()) {
                foreach (Author author in property.GetCustomAttributes(typeof(Author), true).Cast<Author>()) {
                    Console.WriteLine("\tProperty {0} Has Author Attribute Version:{1}", property.Name, author.version);
                }
            }
            var temp = new TestClass();
            Console.WriteLine("Reflecting instance of Test class ");
            foreach (var property in temp.GetType().GetProperties()) {
                foreach (Author author in property.GetCustomAttributes(typeof(Author), true).Cast<Author>()) {
                    Console.WriteLine("\tProperty {0} Has Author Attribute Version:{1}", property.Name, author.version);
                }
            }
        }

    }

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true)]
    public class Author : Attribute {
        public Author(string name, int v) {
            this.name = name;
            version = v;
        }

        public double version;
        string name;
    }

    public class TestClass {
        [Author("Bill Gates", 2)]
        public TextBox TestPropertyTextBox { get; set; }
    }

}

I get this output:

alt text

Sign up to request clarification or add additional context in comments.

Comments

1

I assume by parameter list, you mean a list of all attribute uses?

If not, this code shows you how to get an attribute using reflection on a whole class. But you should be able to take what you need.

So here is a method to find all attributes of a certain type, on any property inside a TestClass

public IEnumberable<Result> GetAttributesFromClass(TestClass t)
{

    foreach(var property in t.GetType().GetProperties())
    {
        foreach(Author author in property.GetCustomAttributes(typeof(Arthor), true))
        {
             // now you have an author, do what you please
             var version = author.version;
             var authorName = author.name;

             // You also have the property name
             var name = property.Name;

             // So with this information you can make a custom class Result, 
             // which could contain any information from author, 
             // or even the attribute itself
             yield return new Result(name,....);
        }

    }
}

Then you could go:

var testClass = new TestClass();

var results = GetAttributesFromClass(testClass);

Also, you may want your public double version and string name to be properties. Something like this:

public double version
{
    get; 
    private set;
}

Which will allow version to be set from the constructor, and read from anywhere.

4 Comments

Thank you. My case is that do not use the static class. So using Author author = (Author)attribute; is not OK. I want to use reflection to get the parameter list from the PropertyInfo.
Can you define parameter list?
Parameter list is that I can get ("Bill Gates", 2) dynamically using reflection with have to use "Author" to cast attribute. Because there will be many such Attribute, some may not be Author attribute.
Well then you could iterate over the attribute and get all the properties?
0
string name = author.name;

is not allowed because the field name is not public. Does it work if you make name public?

2 Comments

Sorry it's a typo, I added the public back.
Your latest edit reveals your problem. Either use GetCustomAttributes with a type parameter to filter and only look at attributes which are Author, or else use Author author = attribute as Author; instead of a cast, to do a dynamic type check, and ignore the ones that come back null.
0

i had the same issue in one of my apps. this is my solution:

public static string GetAttributesData(MemberInfo member)
{            
    StringBuilder sb = new StringBuilder();
    // retrives details from all attributes of member
    var attr = member.GetCustomAttributesData();
    foreach (var a in attr)
    {
        sb.AppendFormat("Attribute Name        : {0}", a)
            .AppendLine();
        sb.AppendFormat("Constructor arguments : {0}", string.Join(",  ", a.ConstructorArguments))
            .AppendLine();
        if (a.NamedArguments != null && a.NamedArguments.Count > 0 )
        sb.AppendFormat("Named arguments       : {0}", string.Join(",  ", a.NamedArguments))
            .AppendLine();
        sb.AppendLine();
    }            
    return sb.ToString();
}

i have tested your example.

var t = typeof (TestClass);
var prop = t.GetProperty("TestPropertyTextBox", BindingFlags.Public | BindingFlags.Instance);
var scan = Generator.GetAttributesData(prop);

here is output:

Attribute Name        : [Author("Bill Gates", (Int32)2)]
Constructor arguments : "Bill Gates",  (Int32)2

Comments

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.