A
Aj Singh
Hi Rod,
I am trying to iterate through the MSProject.Tasks for every MSProject.Task
object, i am using System.Reflection to get the value of each property and
its value.
Below is the code that i am using to fetch the values for MSProject.Task
Requirement:
1) Every user would configure what "property" he wishes to see, which would
be fetched from database.
2) Hence hard coding the property names is not possible, therefore,
System.Reflection is used to keep it dynamic.
Problem
Not able to iterate through the MSProject.Task object in active project.
The same code works for MSProject.Project object. Using C# as the language,
VS 2008, MSProject 2003, PIA 2003
Approch 1:
/*******************code starts here**************************/
MSProject.Project _objActiveProject;
///_objActiveProject is the object which is obtained by openning the file
through the interop of MSProject 2003.
MSProject.Tasks _objTasks = _objActiveProject.Tasks;
///Type to get the Type for the MSProject.Task object.
Type _objTaskType;
///_objPropertyInfo would contain each property for the _objTaskType object
and its value can be fetched later.
PropertyInfo _objPropertyInfo;
for (int _intCounter = 1; _intCounter < _objTasks.Count; _intCounter++)
{
_objTaskType = _objTasks[_intCounter].GetType();
for (int _intNewCounter = 0; _intNewCounter <
_objProperties.Rows.Count; _intNewCounter++)
{
/// _objProperties.Rows[_intNewCounter]["PROPERTY_NAME"].ToString() This
contains the property names
/// for the task object, which can be obtained if i access the
property directly. If i try to make it dynamic
/// it does not return the instance of that object.
///I have also tried: _objTaskType.GetProperty("ActualDuration"),
however _objPropertyInfo remains null, hence i don't get the value.
_objPropertyInfo =
_objTaskType.GetProperty(_objProperties.Rows[_intNewCounter]["PROPERTY_NAME"].ToString());
string _strName = "ActualDuration";
_objPropertyInfo = _objTaskType.GetProperty(_strName);
if (_objPropertyInfo != null)
{
_objstrProjectTable.Append("<tr
style='text-align:left'><td>" + _objPropertyInfo.Name + "</td><td>" +
_objPropertyInfo.GetValue(_objTasks[_intCounter], null).ToString() +
"</td><tr>");
}
}
}
/*******************code ends here**************************/
Approach 2:
I have also tried writing code using:
foreach task in tasks
//try to get the type of task object
//get the property name and its value,
//write to output media (response, string, file , db etc)
loop
however even here the type does not seem to get property for the task object
however the same thing work for MSProject.Project
Any help or pointer is deeply appreciated as i have already quite some time
on this, looks simple and logical however it doesn't seem to work.
Thanks & Regards,
Ajay Singh
I am trying to iterate through the MSProject.Tasks for every MSProject.Task
object, i am using System.Reflection to get the value of each property and
its value.
Below is the code that i am using to fetch the values for MSProject.Task
Requirement:
1) Every user would configure what "property" he wishes to see, which would
be fetched from database.
2) Hence hard coding the property names is not possible, therefore,
System.Reflection is used to keep it dynamic.
Problem
Not able to iterate through the MSProject.Task object in active project.
The same code works for MSProject.Project object. Using C# as the language,
VS 2008, MSProject 2003, PIA 2003
Approch 1:
/*******************code starts here**************************/
MSProject.Project _objActiveProject;
///_objActiveProject is the object which is obtained by openning the file
through the interop of MSProject 2003.
MSProject.Tasks _objTasks = _objActiveProject.Tasks;
///Type to get the Type for the MSProject.Task object.
Type _objTaskType;
///_objPropertyInfo would contain each property for the _objTaskType object
and its value can be fetched later.
PropertyInfo _objPropertyInfo;
for (int _intCounter = 1; _intCounter < _objTasks.Count; _intCounter++)
{
_objTaskType = _objTasks[_intCounter].GetType();
for (int _intNewCounter = 0; _intNewCounter <
_objProperties.Rows.Count; _intNewCounter++)
{
/// _objProperties.Rows[_intNewCounter]["PROPERTY_NAME"].ToString() This
contains the property names
/// for the task object, which can be obtained if i access the
property directly. If i try to make it dynamic
/// it does not return the instance of that object.
///I have also tried: _objTaskType.GetProperty("ActualDuration"),
however _objPropertyInfo remains null, hence i don't get the value.
_objPropertyInfo =
_objTaskType.GetProperty(_objProperties.Rows[_intNewCounter]["PROPERTY_NAME"].ToString());
string _strName = "ActualDuration";
_objPropertyInfo = _objTaskType.GetProperty(_strName);
if (_objPropertyInfo != null)
{
_objstrProjectTable.Append("<tr
style='text-align:left'><td>" + _objPropertyInfo.Name + "</td><td>" +
_objPropertyInfo.GetValue(_objTasks[_intCounter], null).ToString() +
"</td><tr>");
}
}
}
/*******************code ends here**************************/
Approach 2:
I have also tried writing code using:
foreach task in tasks
//try to get the type of task object
//get the property name and its value,
//write to output media (response, string, file , db etc)
loop
however even here the type does not seem to get property for the task object
however the same thing work for MSProject.Project
Any help or pointer is deeply appreciated as i have already quite some time
on this, looks simple and logical however it doesn't seem to work.
Thanks & Regards,
Ajay Singh