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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:00:39+00:00 2026-05-24T06:00:39+00:00

I have an object of type X , which I can (obviously) retrieve in

  • 0

I have an object of type X, which I can (obviously) retrieve in runtime.

var type = myObject.GetType();

And I have a generic static class.

public static class MyStaticClass<T>
{
    public static void DoStuff(T something)
    {
        // bla bla
    }
}

What I’d like to do is:

MyStaticClass<myObject.GetType()>.DoStuff(myObject);

But I can’t.

In fact, there is just a few types on which MyStaticClass would operate, and they share several interfaces. One workaround is to write:

if (myObject.GetType() == typeof(X))
{
    MyStaticClass<X>.DoStuff(myObject as X);
}
if (myObject.GetType() == typeof(Y))
{
    MyStaticClass<Y>.DoStuff(myObject as Y);
}

but it’s verbose, and writing that everywhere is really ugly – I feel that I shouldn’t do that but also that I shouldn’t have to do that.

I can’t believe there is no solution. Or any neater workaround at least? Or is my approach wrong to start with (what’s the alternative if so)? Should I create some (abstract?) base class for X, Y, Z?

  • 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-24T06:00:40+00:00Added an answer on May 24, 2026 at 6:00 am

    You can do this with reflection, using Type.MakeGenericType – but then you’ll also need to use reflection to invoke the method. That’s a bit of a pain though.

    If you’re using C# 4 you could use dynamic typing and type inference – although that only works for generic methods rather than generic types, so you’d need to use:

    public void DoStuffDynamic(dynamic item)
    {
        DoStuffHelper(item);
    }
    
    private static void DoStuffHelper<T>(T item)
    {
        MyClass<T>.DoStuff(item);
    }
    

    EDIT: For performance, you can avoid doing too much actual reflection. You can perform reflection once per item type, create a delegate of the form Action<object>, and cache it in a dictionary. This can be far faster than performing reflection on every execution.

    Here’s a short but complete sample:

    using System;
    using System.Collections.Generic;
    using System.Reflection;
    
    public static class MyStaticClass
    {
        private static readonly object mapLock = new object();
    
        private static readonly Dictionary<Type, Action<object>>
            typeActionMap = new Dictionary<Type, Action<object>>();
    
        private static readonly MethodInfo helperMethod =
            typeof(MyStaticClass).GetMethod("ActionHelper",
                                            BindingFlags.Static |
                                            BindingFlags.NonPublic);
    
        public static void DoStuffDynamic(object item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
    
            Type type = item.GetType();
            Action<object> action;
            lock (mapLock)
            {
                if (!typeActionMap.TryGetValue(type, out action))
                {
                    action = BuildAction(type);
                    typeActionMap[type] = action;
                }
            }
            action(item);
        }
    
        private static Action<object> BuildAction(Type type)
        {
            MethodInfo generic = helperMethod.MakeGenericMethod(type);
            Delegate d = Delegate.CreateDelegate(typeof(Action<object>),
                                                 generic);
            return (Action<object>) d;
        }
    
        private static void ActionHelper<T>(object item)
        {
            MyStaticClass<T>.DoStuff((T) item);
        }
    }
    
    
    public static class MyStaticClass<T>
    {
        public static void DoStuff(T something)
        {
            Console.WriteLine("DoStuff in MyStaticClass<{0}>",
                              typeof(T));
        }
    }
    
    public class Test
    {
        static void Main()
        {
            MyStaticClass.DoStuffDynamic("Hello");
            MyStaticClass.DoStuffDynamic(10);        
        }
    }
    

    I only use this sort of thing when I have to, but occasionally there really isn’t any sensible alternative.

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

Sidebar

Related Questions

I have a class with a Property called 'Value' which is of type Object.
I have two ClassLoaders which loads the same class. So, obviously these can't cast
I have an object of type IMAGE which holds image. I wanted to display
I have object A which in turn has a property of type Object B
I have an object of type list from which I wish to use to
Lets say I have a single object of type Car which I want to
I have function some_func_1 which will create an object of type some_type and will
I have a Word.Field object which is a checkbox and the Type property equals
I have a class which is serialized to XML. This class has an Object
I have the following method, which works perfectly. //private static IEnumerable<object> movieSequence; private static

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.