0

I am using the VS Lightswitch ServerApplicationContext to access and modify lightswitch data entities within an ApiController.

Let's say I have a Customer entity, and i can query the collection of customers in the lightswitch db using linq:

IEnumerable<Customer> customers = from custs in serverContext.DataWorkspace
                                                             .ApplicationData
                                                             .Customers
                                                             .GetQuery()
                                                             .Execute()
                                  where c.AProperty == aProperty
                                  select custs;

or

IEnumerable<Customer> customers = 
                        serverContext.DataWorkspace
                                     .ApplicationData
                                     .Customers
                                     .Where(c => c.AProperty == aProperty)
                                     .Execute();

This works perfectly.

However, I have many more entities and several projects with different entities in each project and I am trying to create a library to allow me to query ServerApplicationContext using reflection.

I have used reflection to get the properties of the ServerApplicationContext object, which gives me access to the EntitySet<T>, but I can not execute any queries against it.

This is the code as it stands:

Type t = serverContext.DataWorkspace.ApplicationData.GetType();
PropertyInfo[] pInfo = t.GetProperties();
foreach (var p in pInfo)
{
    // p is equal to {Microsoft.LightSwitch.Framework.EntitySet`1[LightSwitchApplication.Customer] Customers}
    MethodInfo mInfo = p.PropertyType.GetMethod("GetQuery");
    var result = mInfo.Invoke(p.PropertyType, null) ; //<-- Error Here
}

The error returned is:

An exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll but was not handled in user code Additional information: Object does not match target type.

Has anyone had any joy with querying EntitySets (including where clauses) using reflection?

har07
  • 88,338
  • 12
  • 84
  • 137
simonw
  • 3
  • 1

1 Answers1

0

The part where you're trying to invoke the GetQuery MethodInfo has the wrong target - the way it's currently written it's trying to call a GetQuery method on an instance of System.Type (obtained from p.PropertyType), which isn't going to work. What you need to do is get the instance of your EntitySet<T> from serverContext.DataWorkspace.ApplicationData first, then invoke the GetQuery method on that instance.

Type t = serverContext.DataWorkspace.ApplicationData.GetType();
PropertyInfo[] pInfo = t.GetProperties();
foreach (var p in pInfo)
{
    // p is equal to {Microsoft.LightSwitch.Framework.EntitySet`1[LightSwitchApplication.Customer] Customers}
    MethodInfo mInfo = p.PropertyType.GetMethod("GetQuery");
    var entitySet = p.GetValue(serverContext.DataWorkspace.ApplicationData); // new line
    var result = mInfo.Invoke(entitySet, null); // updated line
}

For details on how to put together a dynamic Where clause against your EntitySet<T>, check out the links in this answer: https://stackoverflow.com/a/4799798/2611587.

Community
  • 1
  • 1
Steve Ruble
  • 3,875
  • 21
  • 27