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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:09:22+00:00 2026-05-23T21:09:22+00:00

I am trying to get rid of some references by using string representation of

  • 0

I am trying to get rid of some references by using string representation of certain types. But compiler is not letting me do it the way I want in case of generic methods. I tried this:

IContainer container = components as IContainer;
Type type = Type.GetType("MyNamespace.MyType");
foreach (var component in container.Components.OfType<Type.GetType("MyNamespace.MyType")>()) {
    ...
}

And this:

foreach (var component in container.Components.OfType<type>()) {
    ...
}

For the first loop I get this: “Operator ‘<‘ cannot be applied to operands of type ‘method group’ and ‘System.Type’, and for the second the variable is out of scope for some reason. Why is this happening and is there a way around it? 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-23T21:09:23+00:00Added an answer on May 23, 2026 at 9:09 pm

    You can use Type.MakeGenericType. Example:

    Type.GetType("YourGenericType").MakeGenericType(Type.GetType("yourTypeParameter"))
    

    Example for a List with a typeparameter determined from a string:

    typeof(List<>).MakeGenericType(Type.GetType("System.String"))
    

    This gives you the same as

    typeof(List<string>)
    

    But you still can’t use a Type instance to call Enumerable.OfType. You could use something like this instead:

    foreach (var component in container.Components.Where(o => o.GetType().IsAssignableFrom(yourType))) 
    {
        //...
    }
    

    Of course, this get’s messy quickly, so I would consider changing your approach. Why do you wan’t to use strings?

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

Sidebar

Related Questions

I'm trying to get rid of some hints(*) the Delphi compiler emits. Browsing through
Using ASP/VB, I'm trying to get rid of some duplication in a list box
I'm trying to run some queries to get rid of XSS in our database
I'm trying to lock down PowerPoint and I can get rid of some of
I am trying to get rid of some of these warnings from my iPhone
I am trying to get rid of some confusion when working with C. Is
I'm trying to get rid of the margins of maps generated using the 'maps'
I'm trying to get rid of everything before the filename ex: /home/mike/whatever/test_1.txt leaving only
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get an ASP application deployed; it worked for a while but then

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.