无效的表达术语' foreach'

时间:2016-05-17 10:49:33

标签: c# xml object serialization

我尝试将对象初始化为序列化,类对象是使用xsd.exe创建的。可以毫无问题地初始化各个属性,但是当尝试初始化“无界”时。 XSD属性我不能使用foreach循环遍历数组中的每个值并将其添加到对象:

        object[] test = new object[0];

        test[0] = "ring";

        export export = new export();

        export.info = new exportInfo 
        {
            dateandtimeofexport = DateTime.Now,
            duration = "10",
            planningsoftware = new exportInfoPlanningsoftware
            {
                name = "",
                major = "",
                minor = "",
                revision = "",
                build = ""
            },
           exporter = new exportInfoExporter
           {
               version = new exportInfoExporterVersion
               {
                    name = "",
                    major = "",
                    minor = "",
                    revision = "",
                    build = ""
               },
               module = new exportInfoExporterModule[]
               {
                   foreach(Object x in test)
                   {
                   new exportInfoExporterModule{name = x.name, major = x.major, minor = x.minor, revision = x.revision, build = x.build;}
                   }
               }
           }
        };

我认为这里的主要问题是我对如何初始化使用XSD.exe从我的XSD创建的对象类下的对象数组的理解,是否有人可以建议如何在没有foreach循环的情况下完成此操作?

非常感谢任何指导。

6 个答案:

答案 0 :(得分:5)

Your code is not valid c#. You cannot use foreach inside an array initializer.

// NOT VALID C# CODE!
module = new exportInfoExporterModule[]
{
    foreach(Object x in test)
    {
        new exportInfoExporterModule{name = x}
    }
}

This is not possible, because foreach is a statement, but the array intializer expects an expression that results in a exportInfoExportedModule.

What you can do instead is something like this:

module = test.Select(x => new exportInfoExporterModule{name = x}).ToArray()

But note that at the start of your code you create the array test with length 0 and then try to set "ring" as its first (of zero) elements! That gives an IndexOutOfRangeException. Initialize test like that:

object[] test = new object[] {"ring"};

答案 1 :(得分:3)

module = test.Select(t => new exportInfoExporterModule{name = t}).ToArray()

而不是

module = new exportInfoExporterModule[]
           {
               foreach(Object x in test)
               {
               new exportInfoExporterModule{name = x}
               }
           }

应该做的伎俩。学习一些LINQ,它会清楚这里发生的事情;)

答案 2 :(得分:3)

You can't use a loop within an initializer.

See if you can initialize the array after you've constructed the rest of the object:

export.info = new exportInfo 
{
        // ...
        version = new exportInfoExporterVersion
        {
            name = "",
            major = "",
            minor = "",
            revision = "",
            build = ""
        }
    }
};

export.info.exporter.module = new exportInfoExporterModule[test.Length];

for (int i = 0; i < test.Length; i++)
{
    export.info.exporter.module[i] = new exportInfoExporterModule
    {
        name = test[i].name,
        major = test[i].major,
        minor = test[i].minor,
        build = test[i].build,
        revision = test[i].revision
    };
}

Or, with LINQ:

export.info = new exportInfo 
{
        // ...
        version = new exportInfoExporterVersion
        {
            name = "",
            major = "",
            minor = "",
            revision = "",
            build = ""
        },
        module = test.Select(x => new exportInfoExporterModule
        {
            name = x.name,
            major = x.major,
            minor = x.minor,
            build = x.build,
            revision = x.revision
        }).ToArray()
    }
};

答案 3 :(得分:1)

代码的前两行将导致运行时异常,因为您试图将值放入零长度数组中。

object[] test = new object[0];
test[0] = "ring";
  

运行时异常(第-1行):索引超出了数组的范围。

以下是显示该内容的.NET Fiddle

通过foreach迭代数组完全没问题!但问题实际上是循环在对象初始化器中。不允许这样做,而是将Linq.Select.ToArray()一起使用。

module = test.Select(t => new exportInfoExporterModule { name = t })
             .ToArray();

答案 4 :(得分:0)

You have to create the array you want to assign before you create your exporter object.

Another way could be to use Linq to assign module. Something like module = test.Select(x => new exportInfoExporterModule{name = x}).ToArray() should work.

答案 5 :(得分:-1)

为了能够使用foreach,你必须迭代实现接口IEnumerable的容器。

不是简单数组的情况。只需在List中转换测试(List实现IEnumerable),它应该可以工作。

List<object> test = new List<object>();