c# - Why can't I use "yield" operator to return a lazy list in IValueConverter? -


suppose have following class:

public class values {     public string value1 {get;set;}     public string value2 {get;set;}     public string value3 {get;set;} } 

now want bind object's values gui component's itemssource in specific order, using ivalueconverter:

public class valuestolistconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, cultureinfo culture)     {         var valuesobj = (values ) value;         yield return valuesobj.value1;         yield return valuesobj.value3;         yield return valuesobj.value2;     }      public object convertback(object value, type targettype, object parameter, cultureinfo culture)     {         throw new notimplementedexception();     } } 

but when this, following error:

the body of 'convert' cannot iterator block because 'object' not iterator interface type.

is there way lazily create list in ivalueconverter? or have do:

return new list<string> { valuesobj.value1, valuesobj.value3, valuesobj.value2 } 

iterator blocks defined when return type 1 of:

  • ienumerable
  • ienumerable-of-t
  • ienumerator
  • ienumerator-of-t

the generated iterator becomes state machine fulfils specific contract provides deferred execution , lazy iteration of multiple values.

none of applies if returning object: need return 1 object now.

if converter needs return ienumerable, can - need add second (private) method iterator block - , return type must 1 of listed above.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -