I have the following class which will save some data extracted from MS project:
public class Milestone
{
MSProject.Project ActiveProject = Globals.ThisAddIn.Application.ActiveProject;
public int startDay;
public string startFullMonthName;
public int startYear;
public int endDay;
public string endFullMonthName;
public int endYear;
public Milestone(MSProject.Task oSubTask)
{
string pjStartDate = oSubTask.GetField(ActiveProject.Application.FieldNameToFieldConstant("Start", MSProject.PjFieldType.pjTask));
DateTime startDate = dateToName(pjStartDate);
string pjEndDate = oSubTask.GetField(ActiveProject.Application.FieldNameToFieldConstant("Start", MSProject.PjFieldType.pjTask));
DateTime endDate = dateToName(pjEndDate);
startDay = startDate.Day;
startFullMonthName = startDate.ToString("MMMM", CultureInfo.CreateSpecificCulture("es"));
startYear = startDate.Year;
endDay = endDate.Day;
endFullMonthName = endDate.ToString("MMMM", CultureInfo.CreateSpecificCulture("es"));
endYear = endDate.Year;
}
}
Then I create a list of objects of the Milestone
previously created class, and I want to read the properties of it. However when I try to access these (in the foreach) it shows me the error "object does not contain definition for startDay".
List<object> milestoneList = new List<object>();
MSProject.Project ActiveProject = Globals.ThisAddIn.Application.ActiveProject;
MSProject.Task oSubTask;
int pjStart = 101;
for (int i = pjStart; i <= ActiveProject.Tasks.Count; i++)
{
oSubTask = ActiveProject.Tasks[i];
if (oSubTask.Summary)
{
Milestone m = new Milestone(oSubTask);
milestoneList.Add(m);
}
}
int rows = milestoneList.Count();
int columns = 6;
object[,] data = new object[rows, columns];
object[] marray = milestoneList.ToArray();
foreach (var m in milestoneList)
{
Debug.WriteLine(m.startDay); // aquí intento acceder a la propiedad
}
On the other hand, when I do a debug, inside the local window it does show them:
What am I doing wrong?
milesstoneList
is a list of the typeSystem.Object
and the typeSystem.Object
has no property namedstartDay
. You have to cast or convert the typeSystem.Object
to the data type that the property hasstartDay
, in this case aMilestone
, in order to access the property:You can also use the type
dynamic
where the property will be attempted to be accessed at runtime if it exists: