IEnumerable property without type

2019-04-20 20:08发布

I'm trying to make a property like the official DataGrid.ItemsSource, from MSDN:

public IEnumerable ItemsSource { get; set; }

This provides the support of any type, in any derived class. With this, I can set something like

var list = new List<ObservableCollection<KeyValuePair<decimal, bool>>>();
MyDataGrid.ItemsSource = list;

But when I try to make a property of an IEnumerable without the Type T, exactly as MSDN says, I get an error on VisualStudio:

Using the generic type 'System.Collections.Generic.IEnumerable<T>' requires 1 type arguments

So, what is wrong?

1条回答
Anthone
2楼-- · 2019-04-20 20:25

You need to use the non-generic type System.Collections.IEnumerable.
(note the different namespace)

Note that in .Net 4.0+, you can use IEnumerable<object> instead (due to covariance).

查看更多
登录 后发表回答