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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:39:04+00:00 2026-05-25T15:39:04+00:00

I need some help figure out how to use reflection to get the concrete

  • 0

I need some help figure out how to use reflection to get the concrete implementation based of the Dto type:

public interface IDocumentService<TDto>
{
}

public interface ICommentService: IDoumentService<CommentDto>
{
}

public abstract class DocumentService<TEntity,TDto>: IDocumentService<TDto> where TEntity: Entity, where TDto: Dto
{
}

public class CommentService: DocumentService<Comment,CommentDto>, ICommentService
{
}

So, what I want to do, is pass the CommentDto to a method so I can get to the CommentService.

public IDocumentService<TDto> GetDocumentService<TDto>()
{
    //based on the TDto type I want to find the concrete that 
    //implements IDocumentService<TDto>
}

I would call it like this:

var commentDocumentService = GetDocumentService<CommentDto>();

So, I would get back CommentService, knowing that I would only see the methods part of the IDocumentService interface.

  • 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-25T15:39:05+00:00Added an answer on May 25, 2026 at 3:39 pm

    Here is a possible implementation for GetDocumentService.

        public static IDocumentService<TDto> GetDocumentService<TDto>()
        {
            // Gets the type for IDocumentService
            Type tDto=typeof(IDocumentService<TDto>);
            Type tConcrete=null;
            foreach(Type t in Assembly.GetExecutingAssembly().GetTypes()){
                // Find a type that implements tDto and is concrete.
                // Assumes that the type is found in the executing assembly.
                if(tDto.IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface){
                    tConcrete=t;
                    break;
                }
            }
            // Create an instance of the concrete type
            object o=Activator.CreateInstance(tConcrete);
            return (IDocumentService<TDto>)o;
        }
    

    It wasn’t clear whether you wanted to return a new object, so I assumed so.

    EDIT:

    Due to your comment, here is a modified version of GetDocumentService. The disadvantage is that you need to specify another type parameter. The advantage, though, is that this approach provides a certain degree of type safety, since both type parameters must be compatible.

        public static T GetDocumentService<TDto, T>() where T : IDocumentService<TDto>
        {
            // Gets the type for IDocumentService
            Type tDto=typeof(T);
            Type tConcrete=null;
            foreach(Type t in Assembly.GetExecutingAssembly().GetTypes()){
                // Find a type that implements tDto and is concrete.
                // Assumes that the type is found in the calling assembly.
                if(tDto.IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface){
                    tConcrete=t;
                    break;
                }
            }
            // Create an instance of the concrete type
            object o=Activator.CreateInstance(tConcrete);
            return (T)o;
        }
    

    EDIT 2:

    If I understand correctly, you want to get the other interfaces implemented by the type of the return value of GetDocumentService. For example, GetDocumentService<CommentDto> returns an object of type CommentService that implements the ICommentService interface. If I understand correctly, the return value should be a Type object (for example, the return value could be typeof(ICommentService)). Once you have the type, you should call the type’s FullName property to get the type’s name.

    Use the following method on the return value of GetDocumentService to get the type of interface implemented by that value, for instance, typeof(ICommentService).

        public static Type GetDocumentServiceType<TDto>(IDocumentService<TDto> obj){
            Type tDto=typeof(IDocumentService<TDto>);
            foreach(Type iface in obj.GetType().GetInterfaces()){
                if(tDto.IsAssignableFrom(iface) && !iface.Equals(tDto)){
                    return iface;
                }
            }
            return null;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all i am in need of some help trying to figure out how
I need some help trying to figure out how to format dates in perl.
Need some help assigning a mouseover event to display some icons that start out
I need some help from the shell-script gurus out there. I have a .txt
I need some help figuring out the best way to proceed with creating a
I need some help structuring my php. I am trying to get 3 different
I'm compiling PHP5.2.9 on Mac OS X 10.5.6. Need some help to figure this
Need some help about with Memcache. I have created a class and want to
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview

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.