为什么可以像数组一样索引对象?

时间:2018-10-26 17:11:30

标签: c# lumenworks

在我在const icon = el('svg', { width: 24, height: 24 }, el('path', { d: "M 22.933333,5.7999995 V 18.2 H 1.066667 V 5.7999995 H 22.933333 M 24,4.7333335 H 0 V 19.266666 h 24 z" } ) ); link)上看到的所有示例中,读取记录后,可以对 //var chromeOptions = new ChromeOptions(); //chromeOptions.AddArguments("headless"); string tokenizedUrl = null; string token = null; string validUser = AtConfiguration.GetConfiguration("userName"); string validPass = AtConfiguration.GetConfiguration("password"); using (var browser = new ChromeDriver(/*chromeOptions*/)) { bool found = false; browser.Navigate().GoToUrl(AtConfiguration.GetConfiguration("baseURL")); if (browser.FindElements(By.XPath("//A[@id='uxOptInLink']")).Count !=0) { browser.FindElement(By.XPath("//A[@id='uxOptInLink']")).Click(); } browser.FindElement(By.Name("loginfmt")).SendKeys(validUser); browser.FindElement(By.Id("idSIButton9")).Click(); if (browser.FindElements(By.XPath("//*[text()='Work or school account']")).Count != 0) { browser.FindElement(By.XPath("//*[text()='Work or school account']")).Click(); } WebDriverWait wait = new WebDriverWait(browser, TimeSpan.FromMinutes(1)); wait.PollingInterval = TimeSpan.FromMilliseconds(500); wait.Until(wd => browser.FindElements(By.Name("Password")).Count > 0); browser.FindElement(By.Name("Password")).SendKeys(validPass); browser.FindElement(By.Id("submitButton")).Click(); //maybe implement do while while (found == false) { if (browser.Url.Contains("token")) { tokenizedUrl = browser.Url; found = true; break; } } int index = tokenizedUrl.IndexOf("="); int index2 = tokenizedUrl.IndexOf("&"); int index3 = index2 - 1; token = tokenizedUrl.Substring(index + 1, index3-index); 对象本身进行索引以检索字段的值。例如:

CsvReader

显然,CsvReaderusing (CsvReader csv = new CsvReader(new StreamReader("data.csv"), true)) { int fieldCount = csv.FieldCount; //Line X while (csv.ReadNextRecord()) { for (int i = 0; i < fieldCount; i++) Console.WriteLine(csv[i])); //Line Y } } 的实例,无论是哪种类型。

在第X行,如果我尝试引用csv,则会得到CsvReader

在Y行,我可以访问csv[i]等。[]是什么意思?不是在索引数组吗?

由于System.ArgumentOutOfRangeException没有csv[0]属性,如何动态找出其上限?

1 个答案:

答案 0 :(得分:3)

已实现索引器

public virtual string this[int field]
{
  get
  {
      return ReadField(field, false, false);
  }
}

这是异常来自哪里

private string ReadField(int field, bool initializing, bool discardValue)
{
 if (!initializing)
 {
     if (field < 0 || field >= _fieldCount)
         throw new ArgumentOutOfRangeException("field", field, 
         string.Format(CultureInfo.InvariantCulture, ExceptionMessage.FieldIndexOutOfRange, field));
 }

它之所以被抛出是因为没有调用ReadNextRecord _fieldCount等于0。