Итак, я хочу написать кусок кода, который получает результат из EntitySet и помещает данные в другой объект, используя SystemReflection.
Пример
Объект Ticket
имеет свойство с именем Application
, значение этого свойства определено в таблице отношений с именем TicketRelation
.Программа должна проверить свойство Application
в TicketRelation
и, если оно там, будет передавать данные в свойство Application
Ticket
.
Это то, что у меня есть до сих пор:
private object ConvertRelations(object origin, object to)
{
// haal relTable op met getValue op relatie property.
// The propertyInfo types are EntitySets
List<PropertyInfo> relationProperties = new List<PropertyInfo>();
PropertyInfo[] cRels = to.GetType().GetProperties();
foreach (var property in origin.GetType().GetProperties())
{
// all the possible endings of relation table names
if (property.Name.EndsWith("Relatie") || property.Name.EndsWith("Rel") || property.Name.EndsWith("Relaties"))
{
relationProperties.Add(property);
}
}
foreach (var relProp in relationProperties)
{
var parent = relProp.GetValue(origin, null);
var parentProps = parent.GetType().GetProperties();
object match;
PropertyInfo[] matchProps = null;
foreach(var parentProp in parentProps)
{
// the property's name of which I assumed to hold the data I want was called Item
if (parentProp.Name == "Item")
{
match = parentProp.GetValue(parent, null);
if(match != null)
{
matchProps = match.GetType().GetProperties();
}
}
}
}
// this will return the result later on.
return null;
}
Но почему-то метод GetValue, который я пытаюсь вызвать на parentProp, не работает.Я использую этот метод каждый раз для GetValue, но когда я делаю это с EntitySet
, он выдает исключение, которое я не понимаю.Трассировка стека, которую он генерирует в parentProp.GetValue(parent, null)
:
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at TicketSystemDb.DTO.Helper.DTOHelper.ConvertRelations(Object origin, Object to) in C:\TicketSystem\TicketSystemDb\DTO\Helper\DTOHelper.cs:line 90
at TicketSystemDb.DTO.Helper.DTOHelper.SimplifyResult(Object from, Object to) in C:\TicketSystem\TicketSystemDb\DTO\Helper\DTOHelper.cs:line 61
at TicketSystemDb.REPO.TicketREPO.GetTickets() in C:\TicketSystem\TicketSystemDb\REPO\TicketREPO.cs:line 35
at TicketSystemDb.UOW.TicketUOW.GetTickets() in C:\TicketSystem\TicketSystemDb\UOW\TicketUOW.cs:line 21
at TicketSystemAPI.Controllers.TicketController.Get() in C:\TicketSystem\TicketSystemAPI\Controllers\TicketController.cs:line 22
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_1.<GetExecutor>b__3(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)