Using reflection to Grab NetTiers TList and then how to iterate that list

141 Views Asked by At

The below code works without issue, it will grab the exact TList i'm looking for. But I have no clue how to iterate it.

What i'm looking for is either how to turn my var dataTableObject into a DataTable or how to iterate the var and then use reflection to grab the specific objects i'm looking for. Meaning my TList has 100 objects, i only want 2 of the objects out of the 100 objects for all my rows.

Assembly assem = Assembly.LoadFrom(Context.Server.MapPath("~/bin/MyApp.Services.dll"));
Type typMyService = assem.GetType(string.Format(
                 "MyApp.Services.{0}Service", pc[i].RadComboBoxDataSourceTable), true);
object oMyService = Activator.CreateInstance(typMyService);
System.Reflection.MethodInfo objMethod = typMyService.GetMethod("GetAll", Type.EmptyTypes);
var dataTableObject = objMethod.Invoke(oMyService, null);

Thanks!

1

There are 1 best solutions below

0
Uranus On

If the GetAll result type is DataTable, you can use the direct cast:

DataTable dataTableObject = (DataTable)objMethod.Invoke(oMyService, null);

Using var in this case has no effect, because the compiler do know the method signature and therefore cannot guess the variable type.