jeudi 14 janvier 2016

Object does not match target type after method invoke

Trying to make nested SQL Queries with objects that contain each other but I keep getting object does not match target type

 public ICollection<T> SelectTunnel<T>(object param, string previous = null)
        {
            var props = typeof(T).GetProperties();
            string SqlCommand = $"SELECT * FROM [dbo].[{typeof(T).Name}] WHERE {previous??typeof(T).Name}_Id = @ID";
            var LocalQueryRes = (ICollection<T>)Context.Query<T>(SqlCommand, param);
            var IdProp = props.SingleOrDefault(d => d.Name == $"{typeof(T).Name}_Id");
            if (IdProp != null)
            {
                foreach (var item in LocalQueryRes)
                {
                    foreach (var prop in props)
                    {
                        if (prop.PropertyType.IsGenericType && typeof(ICollection<>) == prop.PropertyType.GetGenericTypeDefinition())
                        {
                            MethodInfo method = typeof(ClearingHouse_db).GetMethod("SelectTunnel");
                            method = method.MakeGenericMethod(prop.PropertyType.GenericTypeArguments);
                            var res = method.Invoke(this,new object[2] { new { ID = IdProp.GetValue(item) }, typeof(T).Name });
                            prop.SetValue(LocalQueryRes, res ,null); // AT THIS POINT EXCEPTION OCCURS
                        }
                    }
                }
            }
            return LocalQueryRes;
        }

What kind of casting can I do on the method I invoke?





Aucun commentaire:

Enregistrer un commentaire