Really stuck with this problem,
There are grids at seperate pages and all of them keeps List of objects, all the rows has edit button at the end of the line, and trying to make an dynamic method to return single object when pressed Edit button.
So I add an generic method like this (it might be wrong please correct me), just trying to hit the breakpoint in this method thats why includes nothing inside right now
public T GetItem<T> (int ID) {
Type _type = typeof(T);
object result = null;
//some code
return (T)result;
}
And the ActionResult calls that GetItem method, ID and TypeName cames from Ajax post, we can assume ID = 7, TypeName = "ProjectViewModel"
public virtual ActionResult GetEditItem(int ID, string TypeName){
Type _type = Type.GetType("Project.Models." + TypeName); // returns ProjectViewModel
Type baseService= Type.GetType("Project.Services.BaseService"); // service that keeps the method
MethodInfo _method = baseService.GetMethod("GetItem");
object[] item = new object[] { ID }; // parameter to pass the method
object classInstance = Activator.CreateInstance(_type, null); // create instance with that type (ProjectViewModel right now)
_method.MakeGenericMethod(_type).Invoke(classInstance, item); // invoke it but it returns error in this line
return View();
}
Exception is;
An exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll but was not handled in user code
Additional information: Object does not match target type.
Something I miss, what object doesn't match, don't get it. Thanks from now.
Aucun commentaire:
Enregistrer un commentaire