Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5966973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:52:33+00:00 2026-05-22T19:52:33+00:00

I am trying to build a utility method that will generically load entitycollections using

  • 0

I am trying to build a utility method that will generically load entitycollections using Reflection. The idea is that a programmer using the utility can specify any type of entity and this method will discover the correct EntityQuery and the load the context with what they requested. So, I have collected the Entity type and Where clause from the user, now I am trying to figure out how to invoke the method. Here is what I have:

public void Handle(LoadEntityQuery loadQuery, Action<LoadEntityQueryResult> reply)
{
    foreach (var entry in loadQuery.Entities)
    {

        Type entityType = entry.Key;
        Type _contextType = EmployeeJobsContext.Instance.GetType();

        MethodInfo _methodInfo = (from x in _contextType.GetMethods()
                                 where x.ReturnType.BaseType == typeof(EntityQuery)
                                 from y in x.ReturnType.GetGenericArguments()
                                 where y == entityType
                                 select x).FirstOrDefault();
        if (_methodInfo != null)
        {
            var query = _methodInfo.Invoke(EmployeeJobsContext.Instance, null);

           var _loadMethods = from x in _contextType.GetMethods()
                              where x.Name == "Load" &&
                                    x.GetParameters().Length == 3
                              select x;
           MethodInfo _loadMethod = null;

           if (_loadMethods != null)
           {
               foreach (MethodInfo item in _loadMethods)
               {
                   ParameterInfo[] _paramInfo = item.GetParameters();
                   if (_paramInfo[0].ParameterType.BaseType == typeof(EntityQuery) &&
                       _paramInfo[1].ParameterType.IsGenericType &&
                       _paramInfo[1].ParameterType.GetGenericArguments().Length == 1 &&
                       _paramInfo[1].ParameterType.GetGenericArguments()[0].BaseType == typeof(LoadOperation) &&
                       _paramInfo[2].ParameterType == typeof(object))
                   {
                       _loadMethod = item;
                       break;
                   }
               }
           }

           MethodInfo _loadOpMethod = this.GetType().GetMethod("LoadOperationResult");
           Delegate d = Delegate.CreateDelegate(typeof(LoadOpDel), _loadOpMethod);

           if (_loadMethod != null)
           {
               object [] _params = new object[3];
               _params[0] = query;
               _params[1] = d;
               _params[2] = null;

               _loadMethod = _loadMethod.MakeGenericMethod(entityType);
               _loadMethod.Invoke(_context, _params);
           }
        }           
    }
}

public delegate void LoadOpDel(LoadOperation loadOp);

public void LoadOperationResult (LoadOperation loadOp)
{
    if (loadOp.HasError == true)
    {
        //reply(new LoadEntityQueryResult { Error = loadOp.Error.Message });
        loadOp.MarkErrorAsHandled();
    }
} 

The foreach loop is iterating a Dictionary>>, where the Key is an Entity type and the value is a Where clause. The first part of code is finding the correct EntityQuery method and invoking it to get the actual query. It then discovers the correct Load overload (I know, likely there is a better way to find the method 🙂 ) This portion of the code works correctly, I am able to discover the correct EntityQuery and the Load method.

For the LoadOperation, I want to use the LoadOperationResult as my delegate method. When I try to run this code however, I receive an exception stating that the delegate type and the method type signatures do not match. I am pretty sure my signature is correct because if I was to call Load directly and pass the function name as the callback normally, this code would execute properly. I am fairly familiar with reflective programming, however throwing Generics and Action callbacks into the mix is a bit above my level at this point. I’m at a loss as to what I am doing wrong, does anyone have any pointers for me? Am I way off? Thanks for your help!!
Jason

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:52:34+00:00Added an answer on May 22, 2026 at 7:52 pm

    I found that I did not need to use reflection to invoke the Load method (negating the need for the delegate), instead I call Load directly by creating a generic method based on Entity type. Here is what I came up with for anyone interested:

        /// <summary>
        /// The Action callback for the LoadEntityQuery handler. This callback is used to respond to the 
        /// LoadEntityQuery when all Load calls are complete. See the Handle method 
        /// </summary>
        private Action<LoadEntityQueryResult> _reply = null;
    
        /// <summary>
        /// Accumulator used to determine when the last entity has been loaded
        /// </summary>
        private int EntityCount { get; set; }
    
        /// <summary>
        /// Collective error container for Errors from the LoadOperation. This is value is returned via
        /// the _reply callback to the calling code.
        /// </summary>
        private List<Exception> Errors = null;
    
        public void Handle(LoadEntityQuery loadQuery, Action<LoadEntityQueryResult> reply)
        {
            _reply = reply;
            Errors = new List<Exception>();
            EntityCount = loadQuery.Entities.Count();
    
            MethodInfo _loadOpMethod = this.GetType().GetMethod("Load", BindingFlags.NonPublic | BindingFlags.Instance);
            int _entityCount = loadQuery.Entities.Count();
    
            foreach (var entry in loadQuery.Entities)
            {
                Type entityType = entry.Key;
                Type _contextType = EmployeeJobsContext.Instance.GetType();
    
                MethodInfo _methodInfo = (from x in _contextType.GetMethods()
                                          where x.ReturnType.BaseType == typeof(EntityQuery)
                                          from y in x.ReturnType.GetGenericArguments()
                                          where y == entityType
                                          select x).FirstOrDefault();
                if (_methodInfo != null)
                {
                    var query = _methodInfo.Invoke(EmployeeJobsContext.Instance, null);
                    MethodInfo _typedLoadOpMethod = _loadOpMethod.MakeGenericMethod(new Type[] { entityType });
    
                    _typedLoadOpMethod.Invoke(this, new[] { query, entry.Value});
                }
            }
        }
    
        private void Load<T>(EntityQuery<T> query, Expression<Func<T, bool>> where) where T: Entity
        {
            if (where != null)
                query = query.Where(where);
    
            EmployeeJobsContext.Instance.Load(query, (loadOp) =>
                {
                    EntityCount--;
                    if (loadOp.HasError)
                    {
                        Errors.Add(loadOp.Error);
                        loadOp.MarkErrorAsHandled();
                    }
    
                    if (EntityCount == 0)
                        _reply(new LoadEntityQueryResult { ErrorList = Errors });
    
                }, null);
        }
    

    The handler for the Load operation watches for the last entity to finish loading, then responds to the client that loading is complete (with any errors, should they occur).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a utility method using Linq that will help me with
I'm trying to build a rake utility that will update my database every so
I am trying to build an Eclipse application that would work with a linux/motif
I'm trying to build a utility like this http://labs.ideeinc.com/multicolr , but I don't know
I'm trying build my application using REST and Spring MVC. For some entities I
Im trying to build a small frame that displays an image. My problem is
I'm trying to build a search that is similar to that on Google (with
I'm trying to build a C++ extension for python using swig. I've followed the
I'm trying to build a Chrome browser extension, that should enhance the way the
Trying to build a GUI application in Java/Swing. I'm mainly used to painting GUIs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.