JSon Writer with reflection TargetParameterCountException

  1. #1

    JSon Writer with reflection TargetParameterCountException

    Hi,

    I made a GridPanel which writes changes to db via a JsonWriter.

    I used a Generic HandleChanges Method invked by a method based on reflection.

    When I save changes I get a TargetParameterCountException caused by Json parameters (something wrong between "data", "StoreDataHandler" and "params object[]" of reflection. It seems that "params object[]" is null.

    View:

    buttons.Add(Html.X().Button()
                                            .ItemID("btnSave")
                                            .Text("Save")
                                            .Icon(Icon.Disk)
                                            .DirectEvents(de =>
                                            {
    
                                                de.Click.Url = Url.Action("InvokeGeneric");
                                                de.Click.ExtraParams.Add(new Parameter { Name = "nomeMetodo", Value = "HandleChanges", Mode = ParameterMode.Value, Encode = true });
                                                de.Click.ExtraParams.Add(new Parameter { Name = "model", Value = Model.GetType().ToString(), Mode = ParameterMode.Value, Encode = true });
    
                                                de.Click.ExtraParams.Add(new Parameter
                                                {
                                                    Name = "data",
                                                    Value = "this.up('window').down('component[itemId=gridPanel]').store.getChangedData({skipIdForNewRecords : false})",
                                                    Mode = ParameterMode.Raw,
                                                    Encode = true
                                                });
                                                
                                            })
                                            );
    Generic Controller:

    public ActionResult HandleChanges<T>(StoreDataHandler handler) where T : class
            {
                ChangeRecords<T> records = handler.BatchObjectData<T>();
                ProvaContext db = new ProvaContext();
                var set = db.Set<T>();
    
    
                foreach (T created in records.Created)
                {
                    set.Add(created);
                }
    
                foreach (T deleted in records.Deleted)
                {
                    set.Attach(deleted);
                    set.Remove(deleted);
                }
    
                foreach (T updated in records.Updated)
                {
                    set.Attach(updated);
                    db.Entry<T>(updated).State = EntityState.Modified;
                }
    
                try
                {
                    db.SaveChanges();
    
                    Notification.Show(new NotificationConfig
                    {
                        Title = "Records saved!",
                        Html = "Pending records have been saved.",
                        Icon = Icon.DatabaseSave
                    });
    
                }
                catch (DbUpdateConcurrencyException)
                {
    
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Title = "Concurrency exception!",
                        Message = "A db concurrency exception has thrown.\nData will be refreshed.",
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.WARNING,
                        Modal = true
                    });
                }
    
                return this.Direct();
            }
    Reflection invoke method:

    public object InvokeGeneric(string nomeMetodo, string model, params object[] args){
    
                nomeMetodo = nomeMetodo.Trim('"');
                model = model.Trim('"');
    
                if(model.Contains('[') && model.Contains(']')) {
                    char[] delimPar = { '[', ']' };
                    model = model.Split(delimPar)[1];
                }
    
                Type type = Type.GetType(model);
                GenericController g = new GenericController();
    
                
    
                MethodInfo mi = g.GetType().GetMethods().Where(m => m.Name == nomeMetodo && m.IsGenericMethod).SingleOrDefault();
                return mi.MakeGenericMethod(new[] { type }).Invoke(g, args);
                
            }

    This is the screenshot of the exception: Click image for larger version. 

Name:	Exception.jpg 
Views:	170 
Size:	52.2 KB 
ID:	5299

    It seems that "args" is null...
  2. #2
    You are passing a wrong number of parameter to the method, as exemplified below:

    class Program
    {
        static void Main(string[] args)
        {
            NumberHelper helper = new NumberHelper();
    
            MethodInfo mi = helper.GetType().GetMethod("Sum");
    
            //Success - 2 parameters
            int sum = (int)mi.Invoke(helper, new object[] { 10, 20 });
    
            //Failure - 1 parameter, when 2 are expected
            int sum2 = (int)mi.Invoke(helper, new object[] { 10 });
        }
    
    }
    
    public class NumberHelper
    {
        public int Sum(int valueA, int ValueB)
        {
            return valueA + ValueB;
        }
    }
    You have to debug your code and check the parameters you are passing and the expected parameters.

    Let me know if you need further assistance
    Last edited by RCN; Dec 21, 2012 at 1:45 PM.
  3. #3
    Hmm, is it Ext.Net issue?
  4. #4
    You can move StoreDataHandler from arguments to inside action
    StoreDataHandler handler = new StoreDataHandler();
  5. #5
    Thank to both for the support.

    I noticed that the method expects a parameter of type StoreDataHandler, which should come from the data parameter of json..
    I do not fill a parameter of type StoreDataHandler (and I've never done it nor when it was working without reflection), but I fill in the "data" parameter of json and I obtain the handler parameter of type StoreDataHandler to be filled automatically. I would know how this "translation" works in json, and then what can be wrong with this solution and reflection.

    @Vladimir: if I declare a new StoreDataHandler, then it will be not filled by json, and I will not have changed records inside that "handler".

    Can be possible that if the method does not expect a StoreDataHandler, but only a params object[], then Json Writer will not include the StoreDataHandler param to the param object array?

    Thanks again!

    UPDATE: every param filled by directevent like:

    de.Click.ExtraParams.Add(new Parameter { Name = "dummy", Value = "aaa", Mode = ParameterMode.Value, Encode = true });
    will never add an object to params object[] as a param to be passed via reflection.

    How can i manage to have direct events to add parameters to that params array?

    Thanks.
    Last edited by millenovanta; Dec 21, 2012 at 2:26 PM.
  6. #6
    Partially solved:
    Setting the array of data directly to the value of json data, it works.

    I cannot pass more than one parameter to params object[].
    I would like to pass more value. How can I fill an array of parameters while calling a direct event?


    Thanks
  7. #7
    Quote Originally Posted by millenovanta View Post
    Partially solved:
    Setting the array of data directly to the value of json data, it works.

    I cannot pass more than one parameter to params object[].
    I would like to pass more value. How can I fill an array of parameters while calling a direct event?


    Thanks
    Any ideas?

Similar Threads

  1. Crud json
    By oseqat in forum 1.x Help
    Replies: 0
    Last Post: Jul 11, 2011, 1:05 PM
  2. [CLOSED] Json 4.0
    By majunior in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Jun 24, 2011, 6:32 PM
  3. Get json in codebehind.
    By luiz in forum 1.x Help
    Replies: 0
    Last Post: Apr 19, 2011, 4:14 PM
  4. Performance and Reflection
    By jeybonnet in forum 1.x Help
    Replies: 20
    Last Post: Dec 14, 2008, 2:50 PM

Tags for this Thread

Posting Permissions