English 中文(简体)
超过匿名类型
原标题:foreach over anonymous types
using System;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace LearningJustCode
{
    class Program
    {
        static void Main(string[] args)
        {
            Update();
        }

        static void Update()
        {
            var quote1 = new { Stock = "DELL", Quote = GetQuote("DELL") };
            var quote2 = new { Stock = "MSFT", Quote = GetQuote("MSFT") };
            var quote3 = new { Stock = "GOOG", Quote = GetQuote("GOOG") };

            var quotes = new object[] { quote1, quote2, quote3 };

            foreach (var item in quotes)
            {
                Console.WriteLine(item.Stock);
                Console.WriteLine(item.Quote.ToString());
            }
        Console.ReadKey();

        }

        static string GetQuote(string stock)
        {
            try
            {
                return InnerGetQuote(stock);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
                return "N/A";
            }
        }

        static string InnerGetQuote(string stock)
        {
            string url = @"http://www.webservicex.net/stockquote.asmx/GetQuote?symbol={0}";
            var request = HttpWebRequest.Create(string.Format(url, stock));

            using (var response = request.GetResponse())
            {
                using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
                {
                    return reader.ReadToEnd();
                }
            }


        }
    }
}

我正对项目进行盘问; 可变物品只被指定使用。 此代码稍作修改, 取自 Paul Kimmel C# Unleashed 的 Paul Kimmel 手册 C# Unleashed 。

最佳回答

Your array needs to be of the type of your stock quote. Your stock quote is an anonymous type, so we need to define the array anonymously as well. This can be done cleanly as:

    var quotes = new[]{  new { Stock = "DELL", Quote = "123" },
                         new { Stock = "MSFT", Quote = "123" },
                         new { Stock = "GOOG", Quote = "123" }};

    foreach (var item in quotes)
    {
        Console.WriteLine(item.Stock);
        Console.WriteLine(item.Quote.ToString());
    }
问题回答

我猜问题就在这行里:

var quotes = new object[] { quote1, quote2, quote3 };

引号是一个对象数组,不是这种匿名类型的数组。每个前台也只有对象值。您可以尝试在一个行内或羊排表达式内组成数组。

一种“强”的肮脏工作方式正在变换成动态

        var quote1 = new { Stock = "DELL", Quote = ("DELL") };
        var quote2 = new { Stock = "MSFT", Quote = ("MSFT") };
        var quote3 = new { Stock = "GOOG", Quote = ("GOOG") };

        var quotes = new object[] { quote1, quote2, quote3 };

        foreach (dynamic item in quotes)
        {
            var r = item.Stock;

        }

较清洁的解决方案正在丢弃对象, 所以编译器可以生成一个匿名打字阵列 。

        var quote1 = new { Stock = "DELL", Quote = ("DELL") };
        var quote2 = new { Stock = "MSFT", Quote = ("MSFT") };
        var quote3 = new { Stock = "GOOG", Quote = ("GOOG") };

        var quotes = new [] { quote1, quote2, quote3 };

        foreach (var item in quotes)
        {
            var r = item.Stock;

        }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签