有助於瞭解將數據傳輸到另一個類的本質。 所以,Silverlight應用程序。我有頁面Home.xaml(及其代碼後面的Home.xaml.cs)。有按鈕。當我點擊按鈕時,執行以下代碼:發送數據流到另一個類
Home.xaml.cs
private void Button_Click_1(object sender, RoutedEventArgs e)
{
OpenFileDialog opendialog = new OpenFileDialog();
opendialog.Multiselect = true;
bool? dialogResult = opendialog.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value)
{
Stream fileStream = opendialog.File.OpenRead();
StreamReader reader = new StreamReader(fileStream);
............
在這裏,我需要這樣的數據作爲讀者,因爲我想這個數據流(閱讀器)發送給一些完全另一個類(例如,一個DataProccess.cs):
DataProccess.cs:
namespace SilverlightApplication1.Models
{
public static class DataProcess
{
{
}
}
將處理使用正則表達式和輸出數據將放入集合列表<>的數據流(讀者從Home.xaml.cs)。
如何實現它。我會很高興從你的幾行代碼? :)
訂正代碼:
Home.xaml.cs:
private void Button_Click(object sender, EventArgs e)
{
OpenFileDialog opendialog = new OpenFileDialog();
opendialog.Multiselect = true;
bool? dialogResult = opendialog.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value)
{
Stream fileStream = opendialog.File.OpenRead();
var processor = new Processor();
ICollection<object> results = processor.Process(fileStream);
}
}
Processor.cs
public class Processor
{
public ICollection<object> Process(Stream stream)
{
StreamReader reader = new StreamReader(stream);
string pattern = @"set vrouter ""([\w-]+)""";
while (!reader.EndOfStream)
{
var matches =
Regex.Matches(reader.ReadToEnd(), pattern)
.Cast<Match>().Where(m => m.Success)
.Select(m => m.Groups[1].Value)
.Distinct();
foreach (var match in matches)
{
var val = match + Environment.NewLine;
return new Collection<object>().Add(val);; //here error
}
}
//return new Collection<object>(val);
}
}
這樣的誤差: È rror1 /不能鍵入「無效」隱式轉換爲「了System.Collections.Generic.ICollection」
錯誤來自您的.Add(val)。這是一個無效的方法。你應該使用'返回新集合