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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:47:05+00:00 2026-05-12T10:47:05+00:00

No doubt elements of this question have been asked before, but I’m having trouble

  • 0

No doubt elements of this question have been asked before, but I’m having trouble finding an answer. (Disclaimer: this is related, but separate from a recent question I asked).

I have a method like this:

public static void Method<T>(MethodInfo m, T value)
{
  Type memberType = m.GetValueType();

  if (memberType.IsAssignableFrom(typeof(List<T>))
  {
    object memberValue = Activator.CreateInstance(memberType);
    ((List<T>)memberValue).Add(value);
  }
}

This works fine when I call it like this:

string s = "blah";
Method(memberInfo, s);

However, I need to call this method using a generic type, so I’m calling it like this:

Type valueType = someType;
object passValue = someMethod.MakeGenericMethod(new Type[] { valueType }).Invoke(this, new object[] { });
/* Call my original method */
Method(memberInfo, passValue );

Now, intellisense knows that ‘value’ in Method<T> is whatever type valueType is (say ‘FooObject’). But ‘T’ is object, which means that a List<FooObject> is not assignable from a List<T> (i.e. a List<object>).

I’ve tried using Convert.ChangeType on the variable (‘passValue’) beforehand but that wasn’t any more useful.

As there is no way to cast a variable to the Type of a type variable, how do I get around this?

Is the best solution to somehow not rely on IsAssignableFrom and do a looser type check of whether this will work? The problem with this is that I’m not sure I’ll be able to cast the memberValue properly unless ‘T’ is truly the element type of memberValue.

  • 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-12T10:47:05+00:00Added an answer on May 12, 2026 at 10:47 am

    You’re in luck. I actually had to do something very similar a few weeks ago.

    For a detailed explanation see the above blog post, but basically the general idea is to reflect the type and manually invoke the method with an explicit set of parameters.

    typeof(MyClass).GetMethod("Foo").MakeGenericMethod(new[] { param.GetType() }).Invoke(null, new[] { param });
    

    It’s not very type safe, but it does exactly what you’re looking for.

    class Program
    {
    
        static void Main(string[] args)
        {
            object str = "Hello World";
            object num = 5;
            object obj = new object();
    
            Console.WriteLine("var\tvalue\t\tFoo() Type\tCallFoo() Type");
            Console.WriteLine("-------------------------------------------------------");
            Console.WriteLine("{0}\t{1}\t{2}\t{3}", "str", str, MyClass.Foo(str), MyClass.CallFoo(str));
            Console.WriteLine("{0}\t{1}\t\t{2}\t{3}", "num", num, MyClass.Foo(num), MyClass.CallFoo(num));
            Console.WriteLine("{0}\t{1}\t{2}\t{3}", "obj", obj, MyClass.Foo(obj), MyClass.CallFoo(obj));
        }
    
    }
    
    class MyClass
    {
        public static Type Foo<T>(T param)
        {
            return typeof(T);
        }
    
        public static Type CallFoo(object param)
        {
            return (Type)typeof(MyClass).GetMethod("Foo").MakeGenericMethod(new[] { param.GetType() }).Invoke(null, new[] { param });
        }
    
    }
    

    Output

       var     value           Foo() Type      CallFoo() Type
       -------------------------------------------------------
       str     Hello World     System.Object   System.String
       num     5               System.Object   System.Int32
       obj     System.Object   System.Object   System.Object
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure if this question has been asked before ( searched through SOF
I don't know if this is a dumb question but I have this two
The title of this question makes me doubt if this exist, but still: I'm
I saw many question addressing this issue but still i have some doubts... I
I have a doubt considering changing this : List<String> elements = new ArrayList<String>(); elements
I doubt this is possible, but here's what I want to do. If my
Though I don't doubt this has been answered I cannot find a great match
I have to implement a ListView with elements from a Person object. This object
No doubt this will be something obvious, but the following code has 2 errors
I doubt this is possible, but just wanted to confirm. I am writing a

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.