本文介绍了为什么无法从"btn"获取文本和ID?使用反射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能使用反射"从"btn"获取文本和ID?
请帮助....

Why cannot get the Text&ID from "btn" use Reflection??
Pls help....

public class FieldInfoClass
{
    protected Button btn;
    public FieldInfoClass()
    {
        btn = new Button();
        btn.Text = "test button";
        btn.ID = "btn1";
    }
    public void Show()
    {

        ShowFields(this.GetType());             //Why cannot get the Text&ID from "btn"??
        ShowFields(typeof(FieldInfoClass));     //Why cannot get the Text&ID from "btn"??

         ShowProperties(btn.GetType()); //It can be.
    }

    public void ShowFields(Type thisGetType)
    {
        Console.WriteLine("\n\t\t\t ShowFields");
        FieldInfo[] fieldinfo = thisGetType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);

        foreach (FieldInfo item in fieldinfo)
        {
            if (item.FieldType.ToString().Equals("System.Web.UI.WebControls.Button"))
            {
                ShowProperties(item.GetType().BaseType);
            }
        }

    }

    public void ShowProperties(Type itemGetType)
    {
        Console.WriteLine("\n\t\t\t ShowProperties");
        PropertyInfo[] propertys = itemGetType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);
        Console.WriteLine("\n PropertyInfo         ");
        foreach (PropertyInfo property in propertys)
        {

            if (property.Name.ToLower() == "text")
            {
                Console.WriteLine("\t\t\t\t\tName          : {0}", property.Name);
                Console.WriteLine("\t\t\t\t\ttext      : {0}", property.GetValue(btn, null));
            }
            if (property.Name.ToLower() == "id")
            {
                Console.WriteLine("\t\t\t\t\tName       : {0}", property.Name);
                Console.WriteLine("\t\t\t\t\t{0}      : {1}", property.Name, property.GetValue(btn, null));
            }

        }
    }
}

推荐答案

//ShowProperties(item.GetType().BaseType);
 ShowProperties(item.FieldType);


这篇关于为什么无法从"btn"获取文本和ID?使用反射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:40