获取具有反射

时间:2015-12-22 09:28:24

标签: c# reflection webforms

美好的一天。我想要获得一个类的2个静态字段,但我无法使它工作。我正在尝试获取字段[event]和[adduserlocation]。这是asp.net webform的代码隐藏页面。任何帮助非常感谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
public partial class test2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var type = typeof(FormBLL).GetType();
        var fieldInfos = type.GetFields(BindingFlags.Instance |
                   BindingFlags.Static |
                   BindingFlags.NonPublic |
                   BindingFlags.Public);

        var q = fieldInfos.ToList();
        foreach(var f in q)
        {
            Response.Write(f.Name + "<br/>");
        }

    }

    public static class FormBLL
    {
        public static string @event = "abc";

        public static string adduserlocation = "123";


    }

}

1 个答案:

答案 0 :(得分:1)

typeof(FormBLL).GetType()System.RuntimeType。你不需要那里的GetTypetypeof(FormBLL)就足够了

var type = typeof(FormBLL);
var fieldInfos = type.GetFields(BindingFlags.Instance |
               BindingFlags.Static |
               BindingFlags.NonPublic |
               BindingFlags.Public);