The following code shows how to create a list of different objects and handle there properties and methods. This example deals with a list of services.
Create your object classes and add them to your list like this;
|
1 2 3 4 5 |
svcBusRelation = new clsBusRelation(); svcBusRelationSector = new clsBusRelationSector(); List<object> Services = new List<object>(); Services.Clear(); Services.Add(svcBusRelation); Services.Add(svcBusRelationSector); |
This is a handy function for getting values of properties;
|
1 2 3 4 |
public static object GetPropValue(object src, string propName) { return src.GetType().GetProperty(propName).GetValue(src, null); } |
To call a method from an object in the list;
|
1 2 3 4 5 |
private static void CallMethod(object src, string methodName) { MethodInfo methodInfo = src.GetType().GetMethod(methodName); methodInfo.Invoke(src, null); } |
An example query;
|
1 |
var query = (from service in Services select new { Name = GetPropValue(service, "Name").ToString(), Status = GetPropValue(service, "Status").ToString(), IsRunning = (bool)GetPropValue(service, "IsRunning") }).ToList(); |