[英]Get parameters of an Attribute using Reflection
My question is there any way to retrieve the parameter list with its value using Reflection? 我的问题是有没有办法使用Reflection检索参数列表及其值?
I want to use reflection to get the parameter list from the PropertyInfo. 我想使用反射从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; }
}
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: 我得到这个输出:
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
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
因此,这是一种在
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. 此外,您可能希望
public double version
和string name
是属性。 Something like this: 像这样的东西:
public double version
{
get;
private set;
}
Which will allow version
to be set from the constructor, and read from anywhere. 这将允许从构造函数设置
version
,并从任何地方读取。
string name = author.name;
is not allowed because the field name
is not public. 不允许,因为字段
name
不公开。 Does it work if you make name
public? 如果你公开
name
它会起作用吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.