Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据流发送到其他类_C#_Silverlight_Stream - Fatal编程技术网

C# 将数据流发送到其他类

C# 将数据流发送到其他类,c#,silverlight,stream,C#,Silverlight,Stream,帮助理解将数据传输到另一个类的本质。 所以,Silverlight应用程序。我有page Home.xaml(以及Home.xaml.cs后面的代码)。这里有一个按钮。单击按钮时,执行以下代码: Home.xaml.cs private void Button_Click_1(object sender, RoutedEventArgs e) { OpenFileDialog opendialog = new OpenFileDialog();

帮助理解将数据传输到另一个类的本质。 所以,Silverlight应用程序。我有page 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);
                      ............
 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);
    }
}
在这里,我需要像reader这样的数据,因为我希望这个数据流(reader)发送到一些完全不同的类(例如,一个dataprocess.cs):

dataprocess.cs

namespace SilverlightApplication1.Models
{
public static class DataProcess
{

    {

    }

}
这将使用正则表达式处理数据流(readerfrom 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);
        }
    }
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);
        }
    }
private void按钮\u单击(对象发送者,事件参数e)
{
OpenFileDialog opendialog=新建OpenFileDialog();
opendialog.Multiselect=true;
bool?dialogResult=opendialog.ShowDialog();
if(dialogResult.HasValue&&dialogResult.Value)
{
Stream fileStream=opendialog.File.OpenRead();
var processor=新处理器();
ICollection results=processor.Process(fileStream);
}
}
处理器.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);
                      ............
 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);
    }
}
公共类处理器
{
公共ICollection进程(流)
{
StreamReader=新的StreamReader(流);
字符串模式=@“set vrouter”“([\w-]+)”“;
而(!reader.EndOfStream)
{
变量匹配=
Regex.Matches(reader.ReadToEnd(),pattern)
.Cast().Where(m=>m.Success)
.Select(m=>m.Groups[1]。值)
.Distinct();
foreach(匹配中的变量匹配)
{
var val=match+Environment.NewLine;
返回新集合().Add(val);//此处出错
}
}
//返回新集合(val);
}
}
此类错误:
Error1/无法将类型“void”隐式转换为“System.Collections.Generic.ICollection”

创建一个将处理结果的新类

public class Processor
{
    public ICollection<object> Process(Stream stream)
    {
        StreamReader reader = new StreamReader(stream);
        // do stuff

        return new Collection<object>();
    }
}
公共类处理器
{
公共ICollection进程(流)
{
StreamReader=新的StreamReader(流);
//做事
返回新集合();
}
}
然后创建它的一个实例并调用Process方法

Stream fileStream = opendialog.File.OpenRead();
var processor = new Processor();
ICollection<object> results = processor.Process(fileStream);
Stream fileStream=opendialog.File.OpenRead();
var processor=新处理器();
ICollection results=processor.Process(fileStream);

Shawn Kendrot,你好!我根据您的指示添加了正确的代码。是否表示现在有错误?:)错误来自您的.Add(val)。这是一种无效的方法。您应该使用
返回新集合{val}