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

  • SEARCH
  • Home
  • 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 6798837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:46:40+00:00 2026-05-26T18:46:40+00:00

I have ToggleActive in C#, and, normally I can call it like ToggleActive where

  • 0

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.

  • 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-26T18:46:41+00:00Added an answer on May 26, 2026 at 6:46 pm

    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:

    string typeName = "MyProject.Data.Person";
    Type toggleActiveType = typeof(ToggleActive<>);
    Type[] typeArgs = new[]{ Type.GetType(typeName) };
    
    Type fullyDefinedType = toggleActiveType.MakeGenericType(typeArgs);
    
    // Use fullyDefinedType as needed
    

    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:

    var repository = new MyLinq2SqlRepository();
    string typeName = "MyProject.Data.Person";
    var mi = repository.GetType().GetMethod("DeleteRecord");
    var fullyDefinedMethod = mi.MakeGenericMethod(Type.GetType(typeName));
    
    object[] parameters = new[] { id };
    fullyDefinedMethod.Invoke(repository, parameters);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a xml data like <Items><Item><name>test1</name></Item><Item><name>test2</name></Item></Items> Looks like jaxb cannot unmashall this when defining
Have this method call: -> simpleJdbcTemplate.queryForInt(SQL,null); -> queryForInt() method in the springs SimpleJdbcTemplate throws
have anyone can tell me what syntax error on this actionscript (actionscript3.0)? var rotY:
Have a look at this code: #include <iostream> using namespace std; int main() {
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you managed to get Aptana Studio debugging to work? I tried following this,
Have converted devise new session from erb to Haml but doens't work, this is
Have a bunch of WCF REST services hosted on Azure that access a SQL
Have not done this before, so obviously I suck at it. Here 64 pixels
Have a photography site that I want to prevent image copying from. How can

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.