通过反射获取 class 的字段

getting fields of a class with reflection

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

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";


    }

}

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

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