字段初始化程序不能引用非静态字段、方法或属性。
using System;using System.Collections.Generic;using System.Linq;namespace MySite{ public class Reminders { public Dictionary<TimeSpan, string> TimeSpanText { get; set; } // We are setting the default values using the Costructor public Reminders() { TimeSpanText.Add(TimeSpan.Zero, "None"); TimeSpanText.Add(new TimeSpan(0, 0, 5, 0), "5 minutes before"); TimeSpanText.Add(new TimeSpan(0, 0, 15, 0), "15 minutes before"); TimeSpanText.Add(new TimeSpan(0, 0, 30, 0), "30 minutes before"); TimeSpanText.Add(new TimeSpan(0, 1, 0, 0), "1 hour before"); TimeSpanText.Add(new TimeSpan(0, 2, 0, 0), "2 hours before"); TimeSpanText.Add(new TimeSpan(1, 0, 0, 0), "1 day before"); TimeSpanText.Add(new TimeSpan(2, 0, 0, 0), "2 day before"); } }}
class SomeOtherClass{ private Reminders reminder = new Reminders(); // error happens on this line: private dynamic defaultReminder = reminder.TimeSpanText[TimeSpan.FromMinutes(15)]; ....
A field initializer cannot reference the nonstatic field, method, or property
慕田峪4524236
慕森王
相关分类