GetFields()
От: Norad  
Дата: 21.02.07 14:40
Оценка:
почему эта строчка, находит только паблик поля? почему Private не находит?
FieldInfo[] fields = t.GetFields();


весь код ниже
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Web.UI;
namespace SimpleViewState
{

    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
    public class ViewStateAttribute : System.Attribute
    {
    }

    public class SmartPage : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);

            Type t = this.GetType();
            FieldInfo[] fields = t.GetFields();
            foreach (FieldInfo field in fields)
            {
                Object[] oList = field.GetCustomAttributes(false);
                foreach (Object o in oList)
                {
                    if (o is ViewStateAttribute)
                    {
                        string vField = string.Concat(field.DeclaringType.Name, ".", field.Name);
                        field.SetValue(
                            this,
                            ViewState[vField]);
                    }
                }
            }
        }

        protected override object SaveViewState()
        {
            Type t = this.GetType();
            FieldInfo[] fields = t.GetFields();
            foreach (FieldInfo field in fields)
            {
                Object[] oList = field.GetCustomAttributes(false);
                foreach (Object o in oList)
                {
                    if (o is ViewStateAttribute)
                    {
                        string vField = string.Concat(field.DeclaringType.Name, ".", field.Name);
                        Object vValue = field.GetValue(this);
                        ViewState[vField] = vValue;
                    }
                }
            }

            return base.SaveViewState();
        }

        public StateBag PublicViewState
        {
            get { return ViewState; }
        }

    }
}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.