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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:42:22+00:00 2026-06-14T05:42:22+00:00

So I have, say, this type of method: public ICollection<String> doSomething() { } Currently,

  • 0

So I have, say, this type of method:

public ICollection<String> doSomething() { }

Currently, I’m trying to check if the return type of method is of type ICollection. However, in C#, I have to pass in a generic when I do the check. So I can’t do, say, “method is ICollection”.

The problem is that I don’t want to restrict the type of generic when I’m checking. In Java, I could just use a wildcard, but I can’t do that in C#. I’ve thought of trying to use the Type.GetGenericParamterContraints() and trying to stick the first result of it in ICollection’s generic constraint to check, but that also didn’t work. Anybody have any ideas?

isCollection(MethodInfo method){
    Type[] ret = method.ReturnType.GetGenericParametersContraint();
    Type a = ret[0];
    return method.ReturnType is ICollection<a>;
}

EDIT: Added what I tried.

  • 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-14T05:42:23+00:00Added an answer on June 14, 2026 at 5:42 am

    If it’s allowed to be the non-generic System.Collections.ICollection (which is implemented by ICollection<T> too) then it’s simply:

    typeof(System.Collections.ICollection).IsAssignableFrom(method.ReturnType)
    

    If you only want to compare to generic ICollection<T> (I see no reason to, but you may have your reasons):

    method.ReturnType.IsGenericType 
      && typeof(ICollection<>)
      .IsAssignableFrom(method.ReturnType.GetGenericTypeDefinition())
    

    Note that that doesn’t work if the return type is non-generic. So it won’t work if there’s a class that implements ICollection<T> but isn’t generic itself. Meaning it won’t catch class Foo : ICollection<string> but it will catch class Foo<T> : ICollection<T>.

    The first way will catch both just fine though.

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

Sidebar

Related Questions

Lets say have this immutable record type: public class Record { public Record(int x,
Say I have this class Myclass that contains this method: public class MyClass {
I have a generic method: public bool DoSomething<T>(T item) where T: IBase { return
Lets say I have this class class Child { public string FirstName { get;
Say I have this code - public interface ParentInterface1 { public List<? extends ChildInterface1>
Lets say i have this usercontrol public class test : UserControl { public int
Let's say I have a C# method public void CheckXYZ(int xyz) { // do
Let's say that I have a method with the signature: public static bool ValidDateToSend(DateTime
Say I have some method like so: public void Method<T>(Func<T> func) { ... }
Lets say I have an object class Person { public string Name { get;

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.