我认为有两种不同的方法:
extended
当原始更改时更改。
- 根本不创建
extended
,而是创建一些类,它将在每个查询中添加“_extention”。
使用 bindingList,第一种方法将足够有效。
public class MyClass
{
private BindingList<string> original = new BindingList<string>();
private List<string> extended = new List<string>();
public BindingList<string> Original
{
get
{
return original;
}
}
public IEnumerable<string> Extended
{
get
{
return extended;
}
}
public MyClass()
{
original.ListChanged += OnChanging;
}
void OnChanging(object sender, ListChangedEventArgs e)
{
switch (e.ListChangedType)
{
case ListChangedType.ItemAdded:
extended.Add(original[e.NewIndex] + "_extention");
break;
case ListChangedType.ItemChanged:
extended[e.NewIndex] = original[e.NewIndex] + "_extention";
break;
case ListChangedType.ItemDeleted:
extended.RemoveAt(e.NewIndex);
break;
case ListChangedType.ItemMoved:
string tmp = extended[e.NewIndex];
extended[e.NewIndex] = extended[e.OldIndex];
extended[e.OldIndex] = tmp;
break;
case ListChangedType.Reset:
extended = new List<string>();
foreach (string originalItem in original)
{
extended.Add(originalItem + "_extention");
}
break;
}
}
}
第二种方法看起来像这样:我们创建自己的类女巫包含链接到originals
public class MyExtendedList : IEnumerable
{
private List<string> original;
public MyExtendedList(List<String> original)
{
this.original = original;
}
public IEnumerator GetEnumerator()
{
return new ExtendedEnum(original);
}
}
public class ExtendedEnum : IEnumerator
{
private List<string> original;
private int position = -1;
public ExtendedEnum(List<String> original)
{
this.original = original;
}
public object Current
{
get
{
return CurrentString;
}
}
public bool MoveNext()
{
position++;
return (position < original.Count);
}
public void Reset()
{
position = -1;
}
public String CurrentString
{
get
{
try
{
return original[position] + "_extension";
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}