Accessing a WorkItemController via the WorkItem.WorkItems collection

Topics: CAB & Smart Client Software Factory
May 17, 2007 at 1:00 PM
Hi,

We have a problem where we need to access an instance of WorkItemController using the IWorkItemController interface by looping through the WorkItem.WorkItems collection but the problem is because WorkItemControllers are added to a WorkItem collection using the ControlledWorkItem<TController> you cannot cast an instance of ControlledWorkItem or even a WorkItem to WorkItemController or IWorkItemColler. In all of the examples below the controller returned is always null;

foreach (KeyValuePair<string, WorkItem> item in WorkItem.WorkItems)
{
IWorkItemController controller = item.Value as IWorkItemController;

//In this instance controller is always set to null
if (controller != null)
controller.DoSomething();
}

foreach (KeyValuePair<string, WorkItem> item in WorkItem.WorkItems)
{
WorkItemController controller = item.Value as WorkItemController;

//Again, the controller object is set to null
if (controller != null)
controller.DoSomething();
}

foreach (KeyValuePair<string, WorkItem> item in WorkItem.WorkItems)
{
ControlledWorkItem<IWorkItemController> controller = item.Value as ControlledWorkItem<IWorkItemController>;

//Again the controller is set to null
if (controller != null)
controller.DoSomething();
}

The problem as I see it is the ControlledWorkItem class acts as a wrapper for the inherited WorkItemController in order to add the WorkItemController to its parent’s WorkItems collection. Because the ControlledWorkItem class uses a template in order to get the ControlledWorkItem from the WorkItem.WorkItems collection you must cast to the specific type;

ControlledWorkItem<MyWorkItemConroller> controller = item.Value as ControlledWorkItem<MyWorkItemConroller>;

Does anyone know a way of accessing the base workitemcontroller or even an instance of the IWorkItemConroller interface through the WorkItem.WorkItems collection without having to specify the specific WorkItemController type?

Hope this makes sense and thanks in advance,

Chris
May 18, 2007 at 1:06 AM
Hey

All i can think now is only way to get this is through Reflection by that you are not bind to the Concrete workitem.Check out the below code it works perfectly



foreach (System.Collections.Generic.KeyValuePair<string, WorkItem> wi in _rootWorkItem.WorkItems)
{

IWorkItemController cw = GetPropertyValue(wi.Value, "Controller") as IWorkItemController;

}


public static object GetPropertyValue(object obj, string propertyName)
{
Type type = obj.GetType();
PropertyInfo property = type.GetProperty(propertyName);
if(property!=null)
{
return property.GetValue(obj, null);
}
else

{
return null;
}
}


Hope this will help you

Thnx
Mani