[英]Unable to set public properties on instance of C# Class
我正在嘗試在移動服務應用程序中編寫我的第一個NSpec測試。 我在規范中創建了一個屬性。 但是,當我嘗試在下一行訪問該元素時,由於Visual Studio無法識別變量,因此無法訪問實例的公共屬性。
public class AppointmentSpec : nspec
{
private AppointmentDTO _dto = new AppointmentDTO();
_dto.PatientId = "124234"; // Visual Studio is not regonizing _dto
}
public class AppointmentDTO
{
public string PatientId { get; set; }
// public string PathwayId { get; set; }
public string ItemId { get; set; }
public string Subject { get; set; } //Dr. Visit, labtest, labtest name, follow up, other...
// public string ProviderName { get; set; }
public string Location { get; set; } //clinic, hsopital name, etc
public string Address { get; set; } //street address
public string PhoneNumber { get; set; }
//to automatically add a corresponding appointment to the provider's calendar
public bool SetProviderAppointment { get; set; }
public string ProviderItemId { get; set; }
public List<string> ProviderItemIds { get; set; }
public bool IsVideoCall { get; set; }
public TimeSpan StartTime { get; set; }
public TimeSpan EndTime { get; set; }
public DateScheduleInfo EventDateSchedule { get; set; }
//public TimeScheduleInfo EventTimeSchedule { get; set; }
//public void Send(object target, string methodName, params object[] args)
//{
// var properties = GetProperties(target.GetType());
// var property = properties.First(p => p.Name == methodName);
// if(property == null)
// throw new ArgumentException($"{target.GetType()} has no property or method ");
// property.SetValue(target, args.First());
//}
//private static IEnumerable<PropertyInfo> GetProperties(Type t)
//{
// return t == null ? Enumerable.Empty<PropertyInfo>() : t.GetProperties().Union(GetProperties(t.BaseType));
//}
}
在類的構造函數中進行賦值:
public class AppointmentSpec : nspec
{
private AppointmentDTO _dto = new AppointmentDTO();
public AppointmentSpec()
{
_dto.PatientId = "124234";
}
}
您正在無效的上下文中訪問_dto
。 如果要使用一些數據初始化PatientId
,請嘗試以下操作:
private AppointmentDTO _dto = new AppointmentDTO
{
PatientId = "124234"
};
參見MSDN
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.