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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:47:19+00:00 2026-05-11T03:47:19+00:00

This really has my stumped today. I’m sure its simple, but… Here is my

  • 0

This really has my stumped today. I’m sure its simple, but… Here is my sample code:

using System; using System.Collections;  namespace ConsoleApplication1 {     class Program     {         public ArrayList SomeProp { get; set; }          static void Main(string[] args)         {             // Get the Type of a property by reflection.             Type myPropType = typeof(Program).GetProperty('SomeProp').PropertyType;              // Create an instance of the determined Type.             var x = Activator.CreateInstance(myPropType);              // Now try to use that instance, passing to a method that takes a generic.             WhatAmI(x);              Console.ReadKey();         }          private static void WhatAmI<T>(T x)         {             Console.WriteLine('T is: ' + typeof(T).FullName);             Console.WriteLine('x is: ' + x.GetType().FullName);         }     } } 

The output here is:

T is: System.Object x is: System.Collections.ArrayList 

Basically what is going on here is I have some property on some class. It doesnt really matter what/where that comes from. The important part is that I get the PropertyInfo for that property. From there I create a new instance of its Type with Activator.

Now If I pass that created instance to a function that takes a generic parameter, the generic Type always comes across as Object, because Activator.CreateInstance() returns an Object, so ‘var x’ is an Object, not, in this case, an ArrayList.

What I need to happen is for the generic type to be the actual type, int his case ‘ArrayList’.

I know there is an Activator.CreateInstance() that I can use instead, but I can’t use a Type (PropertyInfo.PropertyType) within the angle brackets for that method.

I could also just cast the returned object, like:

myPropType x = (myPropType)Activator.CreateInstance(myPropType); 

But obviously that doesn’t compile… Plus it wouldn’t be valid anyway because the cast is compile time, and I don’t know the type until runtime, but conceptually its what I need…

So I’m stuck with this Type, but I can’t figure out how to get it passed over to the WhatAmI() method, and have T be ArrayList, not Object.

Ideas?

  • 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-11T03:47:20+00:00Added an answer on May 11, 2026 at 3:47 am

    To call generic methods using a Type at runtime, you need to use reflection – and MakeGenericMethod in particular, something like:

    typeof(Program).GetMethod('WhatAmI',     BindingFlags.Static | BindingFlags.NonPublic)     .MakeGenericMethod(x.GetType()).Invoke(null, new object[] { x }); 

    Otherwise, the compiler infers the <T> from the variable type – object in this case.

    One side point here is that you can improve things by only doing the reflection once – i.e. don’t use Activator, but use a generic constraint instead:

        private static void WhatAmI<T>() where T : new()     {         T x = new T();         Console.WriteLine('T is: ' + typeof(T).FullName);         Console.WriteLine('x is: ' + x.GetType().FullName);     } 

    Defines the method without an arg, but with a default constructor; then:

        // Get the Type of a property by reflection.     Type myPropType = typeof(Program).GetProperty('SomeProp').PropertyType;      // Now call a generic method just using the type     typeof(Program).GetMethod('WhatAmI',         BindingFlags.Static | BindingFlags.NonPublic)             .MakeGenericMethod(myPropType).Invoke(null, null); 

    calls the method just using the type – the instance is created inside the method. This is usually faster than repeated reflection.

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

Sidebar

Related Questions

Ok, this probably has a really simple answer, but I've never tried to do
I'm new to javascript/jQuery this really has me stumped. What I'm trying to achieve
This one really has me stumped. I have a documents table which hold info
I've got a simple problem that really has me stumped. I have a master
This one has really got me stumped. I have certain forms that are being
This one has me stumped. I found some code posted by Ray Burns to
This one really has me stumped. I have not ran across this problem on
I'm really asking this by proxy, another team at work has had a change
This sounds really stupid, but I was told that you could drag-and-drop visual components
This has not happened to me before, but for some reason both the client

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.