[英]Get parameters of an Attribute using Reflection
我的問題是有沒有辦法使用Reflection檢索參數列表及其值?
我想使用反射從PropertyInfo獲取參數列表。
Author author = (Author)attribute;
string name = author.name;
不行。 因為會有很多屬性,這不是作者的類型。
[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 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; }
}
}
我得到這個輸出:
我在其中一個應用中遇到了同樣的問題。 這是我的解決方案:
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();
}
我測試了你的例子。
var t = typeof (TestClass);
var prop = t.GetProperty("TestPropertyTextBox", BindingFlags.Public | BindingFlags.Instance);
var scan = Generator.GetAttributesData(prop);
這是輸出:
Attribute Name : [Author("Bill Gates", (Int32)2)]
Constructor arguments : "Bill Gates", (Int32)2
我假設通過參數列表,你的意思是所有屬性使用的列表?
如果沒有,此代碼將向您展示如何使用整個類的反射來獲取屬性。 但是你應該能夠拿走你需要的東西。
因此,這是一種在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,....);
}
}
}
然后你可以去:
var testClass = new TestClass();
var results = GetAttributesFromClass(testClass);
此外,您可能希望public double version
和string name
是屬性。 像這樣的東西:
public double version
{
get;
private set;
}
這將允許從構造函數設置version
,並從任何地方讀取。
string name = author.name;
不允許,因為字段name
不公開。 如果你公開name
它會起作用嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.