使用反射获取字段属性标记

时间:2012-06-19 07:40:22

标签: c# reflection postsharp

这是我的程序

<code>
 [demosapect]
 SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=xx;User ID=sa;");
</code>

我正在使用这些代码

foreach (FieldInfo targetfield in type.GetFields(BindingFlags.CreateInstance | BindingFlags.DeclaredOnly | BindingFlags.Default | BindingFlags.ExactBinding ))
  {

// here i need to list out those attributes......
}

foreach (FieldInfo targetfield in type.GetFields(BindingFlags.CreateInstance | BindingFlags.DeclaredOnly | BindingFlags.Default | BindingFlags.ExactBinding )) { // here i need to list out those attributes...... }

我需要通过反射列出属性名称[demoaspect]!

等待你的回复......

1 个答案:

答案 0 :(得分:0)

将此代码用作

if (targetfield.GetCustomAttributesData().Count > 0)
                {

                    Console.WriteLine(targetfield.GetCustomAttributesData()[0].ToString());
                }

if (targetfield.GetCustomAttributesData().Count > 0) { Console.WriteLine(targetfield.GetCustomAttributesData()[0].ToString()); }

它解决了我的疑问!!!!!!!!