I have ToggleActive in C#, and, normally I can call it like ToggleActive where Person is a link 2 sql object in MyProject.Data namespace.
How can I do something like this: ToggleActive<“MyProject.Data.Person”>? Is something like that possible? Do I use typeof somehow?
Thanks!
UPDATE – let me reword this whole thing. I have an admin site, where users can click “delete” for multiple records from different linq to sql tables.
Html…
<table class="persons">
<tr><td>Eric Davis</td><td><a href="/person/delete/14">delete</a></td></tr>
<tr><td>Tamara Davis</td><td><a href="/person/delete/15">delete</a></td></tr>
</table>
<table class="orders">
<tr><td>PB & J Sandwich</td><td><a href="/order/delete/6442">delete</a></td></tr>
<tr><td>Brat Sandwich</td><td><a href="/order/delete/6443">delete</a></td></tr>
</table>
Route in PersonController…
public ActionResult Delete(int id)
{
var repository = new MyLinq2SqlRepository();
repository.DeleteRecord<MyProject.Data.Person>; // MyProject.Data.Person is a linq to sql type
return redirect("~/"); // return home view
}
Route in OrderController…
public ActionResult Delete(int id)
{
var repository = new MyLinq2SqlRepository();
repository.DeleteRecord<Order>; // this is the only thing different from above action
return redirect("~/"); // return home view
}
What I want in new, ObjectController…
public ActionResult Delete(int id, string type) {
var repository = new MyLinq2SqlRepository();
Type linq2SqlType = Type.GetType(type); // where type = "MyProject.Data.Person" or "MyProject.Data.Order"
repository.DeleteRecord<linq2SqlType>(id);
return redirect("~/");
}
This way I can have for the tables…
<table class="persons">
<tr><td>Eric Davis</td><td><a href="/object/delete/14/MyProject.Data.Person">delete</a></td></tr>
<tr><td>Tamara Davis</td><td><a href="/object/delete/15/MyProject.Data.Person">delete</a></td></tr>
</table>
And the Order table would look very similar, but the links would look like this:
<a href="/object/delete/6442/MyProject.Data.Order">delete</a>
Hopefully that’s more clear as to what I’m trying to do! A way to combine those route actions into one.
UPDATE: this is how I eventually ended up doing it…
Html…
<table typeName="@typeof(MyProject.Data.Person)">
@foreach (var p in this.Model.Persons) {
<tr id="@p.PersonId"><td>@p.FirstName</td><td><a href="#" class="deleteObjLink">delete</a></td></tr>
}
</table>
JS/Ajax…
$(".deleteObjLink").on('click', function(e) {
e.preventDefault();
var link = $(this);
var id = link.closest("tr").attr("id");
var typeName = link.closest("table").attr("typeName");
$.ajax({
url: '/object/delete', type: 'post',
data: JSON.stringify({ id: id, typeName = typeName }),
contentType: 'application/json',
successFunc
});
});
In MVC Object controller…
[HttpPost]
public JsonResult Delete(int id, string typeName)
{
Assembly asm = typeof(MyProject.Data.Order).Assembly; // get assembly of any object w/in MyProject.Data.Order namespace (NS). all obj's in that NS share the same assembly
Type targetEntityType = asm.GetType(typeName);
MethodInfo genericMethodDefinition = typeof(MyRepository).GetMethod("Delete");
MethodInfo genericMethod = genericMethodDefinition.MakeGenericMethod(targetEntityType);
bool success = (bool)genericMethod.Invoke(myRepo, new object[] { id });
return this.Json(new { Success = success }, JsonRequestBehavior.DenyGet);
}
MyRepository class has Delete(int id) method.
If you’re trying to use reflection to construct this, you’d need to use Type.MakeGenericType to compose the appropriate type.
This would look something like:
Edit:
Given your edit, the goal is to call a generic method, not make a generic type. This is done via MethodInfo.MakeGenericMethod, like so: