Click here to Skip to main content
16,022,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why cannot get the Text&ID from "btn" use Reflection??
Pls help....

C#
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));
            }

        }
    }
}
Posted

1 solution

do following change in ShowFields

C#
//ShowProperties(item.GetType().BaseType);
 ShowProperties(item.FieldType);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900