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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:34:36+00:00 2026-05-10T21:34:36+00:00

Edit: Of course my real code doesn’t look exactly like this. I tried to

  • 0

Edit:

Of course my real code doesn’t look exactly like this. I tried to write semi-pseudo code to make it more clear of whay I wanted to do.

Looks like it just messed things up instead.

So, what I actually would like to do is this:

Method<Interface1>(); Method<Interface2>(); Method<Interface3>(); ... 

Well … I thought that maybe I could turn it into a loop using reflection. So the question is: How how do I do it. I have very shallow knowledge of reflection. So code examples would be great.

The scenario looks like this:

public void Method<T>() where T : class {} public void AnotherMethod() {     Assembly assembly = Assembly.GetExecutingAssembly();      var interfaces = from i in assembly.GetTypes()     where i.Namespace == 'MyNamespace.Interface' // only interfaces stored here     select i;      foreach(var i in interfaces)     {         Method<i>(); // Get compile error here!     } 


Original post:

Hi!

I’m trying to loop through all interfaces in a namespace and send them as arguments to a generic method like this:

public void Method<T>() where T : class {} public void AnotherMethod() {     Assembly assembly = Assembly.GetExecutingAssembly();      var interfaces = from i in assembly.GetTypes()     where i.Namespace == 'MyNamespace.Interface' // only interfaces stored here     select i;      foreach(var interface in interfaces)     {         Method<interface>(); // Get compile error here!     } } 

The error I get is ‘Type name expected, but local variable name found’. If I try

...     foreach(var interface in interfaces)     {         Method<interface.MakeGenericType()>(); // Still get compile error here!     } } 

I get ‘Cannot apply operator ‘<‘ to operands of type ‘method group’ and ‘System.Type” Any idea on how to get around this problem?

  • 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. 2026-05-10T21:34:37+00:00Added an answer on May 10, 2026 at 9:34 pm

    EDIT: Okay, time for a short but complete program. The basic answer is as before:

    • Find the ‘open’ generic method with Type.GetMethod
    • Make it generic using MakeGenericMethod
    • Invoke it with Invoke

    Here’s some sample code. Note that I changed the query expression to dot notation – there’s no point in using a query expression when you’ve basically just got a where clause.

    using System; using System.Linq; using System.Reflection;  namespace Interfaces {     interface IFoo {}     interface IBar {}     interface IBaz {} }  public class Test {     public static void CallMe<T>()     {         Console.WriteLine('typeof(T): {0}', typeof(T));     }      static void Main()     {         MethodInfo method = typeof(Test).GetMethod('CallMe');          var types = typeof(Test).Assembly.GetTypes()                                 .Where(t => t.Namespace == 'Interfaces');          foreach (Type type in types)         {             MethodInfo genericMethod = method.MakeGenericMethod(type);             genericMethod.Invoke(null, null); // No target, no arguments         }     } } 

    Original answer

    Let’s leave aside the obvious problems of calling a variable ‘interface’ to start with.

    You have to call it by reflection. The point of generics is to put more type checking at compile time. You don’t know what the type is at compile-time – therefore you’ve got to use generics.

    Get the generic method, and call MakeGenericMethod on it, then invoke it.

    Is your interface type itself actually generic? I ask because you’re calling MakeGenericType on it, but not passing in any type arguments… Are you trying to call

    Method<MyNamespace.Interface<string>>(); // (Or whatever instead of string) 

    or

    Method<MyNamespace.Interface>(); 

    If it’s the latter, you only need a call to MakeGenericMethod – not MakeGenericType.

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

Sidebar

Ask A Question

Stats

  • Questions 82k
  • Answers 82k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't believe there's a single source of docs for… May 11, 2026 at 4:44 pm
  • Editorial Team
    Editorial Team added an answer Hi i guess you needed to do a quick search… May 11, 2026 at 4:44 pm
  • Editorial Team
    Editorial Team added an answer Please see: FIX: You may experience problems when you use… May 11, 2026 at 4:44 pm

Related Questions

Edit: Of course my real code doesn't look exactly like this. I tried to
In this thread, we look at examples of good uses of goto in C
It is good practice to remove old code instead of just commenting it out.
I ran across a new problem in the last week. Due to the nature
My web application has a login page that submits authentication credentials via an AJAX

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.