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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:25:24+00:00 2026-06-17T02:25:24+00:00

I have a public generic method which accepts one generic parameter. I also have

  • 0

I have a public generic method which accepts one generic parameter. I also have one several private methods accepting concrete type arguments that i’m calling from generic method. To better describe my problem take a look at the code below:

    public void Save<T>(T entity) where T : class
    {
        if(entity is LibraryItem)      Save(entity as LibraryItem);
        else if(entity is Folder)      Save(entity as Folder);
        else if(entity is ProductType) Save(entity as ProductType);
        else if(entity is ProcessName) Save(entity as ProcessName);
    }

And private methods:

private void Save(ProcessName proc){}
private void Save(ProductType type){}
private void Save(Folder folder){}
...

Looking at the code, I really don’t like the solution, checking for every possible type looks like a bad practice imho. So I wonder if there is any cleaner solution to my problem? Maybe it’s possible to cast T dynamically at runtime and call appropriate private 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-06-17T02:25:26+00:00Added an answer on June 17, 2026 at 2:25 am

    Use runtime type definition:

    public void Save<T>(T entity) where T : class
    {
        Save((dynamic)entity);
    }
    
    private void Save(LibraryItem lib){}
    private void Save(ProcessName proc){}
    private void Save(ProductType type){}
    private void Save(Folder folder){}
    

    You also will need one method with parameter of object type to handle case where entity is not LibraryItem, ProcessName, ProductType, or Folder:

    private void Save(object obj) {  }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Exporter which has a generic method which accepts an IEnumerable<T>
I have a method which returns a generic type, is there a way to
I have a generic method that has some parameter of a generic Type. What
I have a method which gets a parameter such as: public void Foo(ref Action<string>
I have an API which includes some methods that take in a type parameter.
I have a method which uses a generic parameter and requires the downcasted form
The lastIndexOf() method of List interface accepts a parameter which is of type Object.
I have forgotten the syntax for a generic method: public static void swap <T>
I have two classes: Superclass and derived Subclass:Superclass. I have a generic method: public
I have a generic method define as below public T MyMethod<T>(extra params) My method

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.