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 8940119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:57:57+00:00 2026-06-15T10:57:57+00:00

I have the following class public class AbstractJobDao<T> where T : IJob { protected

  • 0

I have the following class

public class AbstractJobDao<T> where T : IJob {
  protected T buildJob() {
    // create an IJob implementation instance based on the generic parameter        
  }      
}

public class FullTimeJobDao : AbstractJobDao<FullTimeJob> {      
}

public class InternshipDao : AbstractJobDao<Internship> {      
}

Both FullTimeJob and Internship implement the IJob interface. I’d like the buildJob() method to be able to infer the generic in the Dao implementation class using reflection, and then create an instance of that type. Is there a way to do this using reflection in .NET 3.5?

If so, what should the line/lines of code look like in the buildJob() method?

EDIT — I think I’m not clear on my question. What I want is for when buildJob() is called inside of the FullTimeJobDao, to create an instance of FullTimeJob. When buildJob() is called from inside of InternshipDao, it should create an instance of Internship, based on the generic type as defined at the top of the class.

  • 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-15T10:57:58+00:00Added an answer on June 15, 2026 at 10:57 am

    What you have should almost work, except you don’t require your generic argument to be instantiable. If you add that as a constraint, you should be able to use new:

    public class AbstractJobDao<T> where T : IJob, new() {
      protected T buildJob() {
        return new T();
      }      
    }
    

    Alternatively, if you have some reason why that constraint is not appropriate in your situation, you can use Activator.CreateInstance:

    public class AbstractJobDao<T> where T : IJob {
      protected T buildJob() {
        return Activator.CreateInstance<T>();
      }      
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following class public abstract class Rule { protected Rule() { Nodes =
I have the following class: public abstract class BaseGridViewModel { protected BaseGridViewModel() { Timer
I have the following class: public abstract class MyClass<T extends Object> { protected T
I have the following class: public abstract class TableServiceEntity { protected TableServiceEntity(); protected TableServiceEntity(string
I have following class with a constructor : public class TestAdapter { protected static
I have the following class: public class getURLData extends AsyncTask<String, Integer, String>{ @Override protected
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have following class (as seen through reflector) public class W : IDisposable {
I have the following class public class MenuVeiculo { public string Nome { get;
I have the following class public class UIControl { public string FName{ get; set;

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.