Dec 21, 2012, 1:06 PM
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:
This is the screenshot of the exception:
It seems that "args" is null...
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:
It seems that "args" is null...