1

I have a ViewState holding a List<T>

public List<AwesomeInt> MyList
{
  get { return ((List<int>)ViewState["MyIntList"]).Select(i => new AwesomeInt(i)).ToList(); }
  set { ViewState["MyIntList"] = value.GetIntegers(); }
}

Now, when I call MyList.Add(new AwesomeInt(3)) let's say, the list does not persist the change.

I believe this is because the .ToList() in the get is creating a new List object and therefore the set will never be called, thus never saving in ViewState.

I have worked around this problem before by either

  1. not calling .Select/.ToList() by saving/calling directly without a conversion.
  2. not using the .Add or .Remove functions and instead re-initializing the List with an equals.

However sometimes 1. is impractical if the class is not serializable and I have no control over that, and 2. is kind of ugly because I have to copy it to a temp first, then add, then re-assign, or play around with Concat to add a single item.

Is there a better way of doing this?

4

1 回答 1

0

好的,下面是一个功能齐全的控制台应用程序,它应该为您提供所需的功能。我利用了泛型,以便您可以构建任何必要类型的周边集合——但还要序列化一些更基本的东西。请记住,我没有要写入的真实视图状态,set但您可以修复它。

另外请记住,我在很短的时间内(例如20 分钟)编写了此代码,因此可能存在优化(例如,属性set上的ViewState属性通常不是必需的,因为此解决方案修改了底层的基本数据源)。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static List<int> _viewState = new List<int>();
        static RawCollection<AwesomeInt, int> ViewState
        {
            get { return new RawCollection<AwesomeInt, int>(_viewState); }
            set { _viewState = value.RawData; }
        }

        static void Main(string[] args)
        {
            ViewState.Add(new AwesomeInt(1));
            ViewState.Add(new AwesomeInt(2));
            WriteViewState();

            ViewState[0].Val = 5;
            WriteViewState();

            ViewState.RemoveAt(0);
            WriteViewState();

            for (int i = 10; i < 15; i++)
            {
                ViewState.Add(new AwesomeInt(i));
            }
            WriteViewState();
        }

        private static void WriteViewState()
        {
            for (int i = 0; i < ViewState.Count; i++)
            {
                Console.WriteLine("The value at index {0} is {1}.", i, ViewState[i].Val);
            }

            Console.WriteLine();
            Console.WriteLine();
        }
    }

    public class RawCollection<T, K> : Collection<T>
    {
        private List<K> _data;

        public RawCollection(List<K> data)
        {
            foreach (var i in data)
            {
                var o = (T)Activator.CreateInstance(typeof(T), i);
                var oI = o as IRawData<K>;
                oI.RawValueChanged += (sender) =>
                {
                    _data[this.IndexOf((T)sender)] = sender.Val;
                };

                this.Add(o);
            }
            _data = data;
        }

        public List<K> RawData
        {
            get
            {
                return new List<K>(
                    this.Items.Select(
                        i => ((IRawData<K>)i).Val));
            }
        }

        protected override void ClearItems()
        {
            base.ClearItems();

            if (_data == null) { return; }
            _data.Clear();
        }

        protected override void InsertItem(int index, T item)
        {
            base.InsertItem(index, item);

            if (_data == null) { return; }
            _data.Insert(index, ((IRawData<K>)item).Val);
        }

        protected override void RemoveItem(int index)
        {
            base.RemoveItem(index);

            if (_data == null) { return; }
            _data.RemoveAt(index);
        }

        protected override void SetItem(int index, T item)
        {
            base.SetItem(index, item);

            if (_data == null) { return; }
            _data[index] = ((IRawData<K>)item).Val;
        }
    }

    public class AwesomeInt : IRawData<int>
    {
        public AwesomeInt(int i)
        {
            _val = i;
        }

        private int _val;
        public int Val
        {
            get { return _val; }
            set
            {
                _val = value;
                OnRawValueChanged();
            }
        }

        public event Action<IRawData<int>> RawValueChanged;

        protected virtual void OnRawValueChanged()
        {
            if (RawValueChanged != null)
            {
                RawValueChanged(this);
            }
        }
    }

    public interface IRawData<T> : INotifyRawValueChanged<T>
    {
        T Val { get; set; }
    }

    public interface INotifyRawValueChanged<T>
    {
        event Action<IRawData<T>> RawValueChanged;
    }
}
于 2013-03-07T18:12:17.590 回答