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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:59:42+00:00 2026-05-13T18:59:42+00:00

My 1st objective is to filter the types based on a specific interface with

  • 0

My 1st objective is to filter the types based on a specific interface with a generic.

My 2nd objective is to obtain the type of the generic parameter itself.

public UserService : IUserService, IDisposable, IExportableAs<IUserService>
{
  ...
}

I cannot assume the structure of the class, its interfaces (if any at all) or alike. The only thing I know I am targetting ExportableAs<T> from my shared assembly that was used to create this plugin. But yet, I need to register the type dynamically.

So, I am using a generic interface to mark the type to export. In this case, it’s IUserService. I am doing this assuming some nifty Linq query can give me what I want. But, I am having a little trouble.

Here’s what I have so far:

assembly.GetTypes()
    .Where(t => t.GetInterfaces().Any(i => 
                i.IsGenericType &&
                i.GetGenericTypeDefinition() == typeof(IExportableAs<>))
            ).ToList()
    .ForEach(t => _catalogs.Add(
            new ComposablePart()
            {
                Name = t.FullName,
                Type = t // This is incorrect
            })
        );

This is working, but notice the comment above for “This is incorrect”. This type is the derived class of UserService.

What I need in my end result are:

  • The generic type pass into the IExportableAs<T> (IUserService in this case)
  • The derived class type (in this case, UserService)

This question got a good up-vote as it got me close (as you can see above): How to determine if a type implements a specific generic interface type But, I need to go one step further in finding that generic type.

Feel free to mangle my linq above.

Thanks in advance!

  • 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-13T18:59:42+00:00Added an answer on May 13, 2026 at 6:59 pm

    Got it

    assembly.GetTypes().SelectMany(t => t.GetInterfaces(), (t, i) => new { t, i })
        .Where(ti => ti.i.IsGenericType &&
                     ti.i.GetGenericTypeDefinition() == (typeof(IExportableAs<>)))
        .Select(ti => new ComposablePart() {
            Name = ti.t.FullName,
            Type = ti.i.GetGenericArguments()[0]
        });
    

    [Edit] In my excitement, I didn’t leave my test program running long enough to throw an Exception on the interface that wasn’t generic. Thought the .NET Framework had been especially clever there. Corrected code now that I know it isn’t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is the 2nd part of the 1st question using c# pointers so pointers
Objective: The Leaves Management and Payroll system requires the official weekend hodiday scheme stored
I am new to objective-C and iphone apps. I am accessing SQLite and have
1st the background - so that you see, that I'm not trying anything malicious:
I'm writing my 1st Java program (in Netbeans) and I'm lost. I have 2
This is my 1st C# project so I may be doing something obviously improper
i have two views 1st one is displaying list (uitableview used), user will select
I have a JTable which has 1st row blank. Now when I sort the
I am having 2 activities: 1st Activity contains 2 arrays which are filled up
This is the structure of my table CREATE TABLE [dbo].[TableA] ( [ObjectID] [int] IDENTITY(1,1)

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.