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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:46:51+00:00 2026-06-09T23:46:51+00:00

I need to invoke a method of a static class. This class in known

  • 0

I need to invoke a method of a static class. This class in known only at runtime (it is a System.Type variable). I know that my method is implemented in class “MyObject”. How do I invoke such method? Code that illustrates what I need to do is below. It may look a bit perverted but I swear I’ll use this for good purposes and will not allow the Universe to implode.

public class MyObject
{
    public static string ReturnUsefulStuff()
    {
        return "Important result.";
    }
}

public class MyChildObject: MyObject
{
    // Hey! I know about ReturnUsefulStuff() method too!
}

public class App
{
    public void Main()
    {
        // The following type isn't supposed to be known at compile time.
        // Except that it will always be MyObject type or its descendent.
        Type TypeOfMyObject = typeof(MyChildObject); 
        // My erotic fantasy below. That line doesn't actually work for static methods
        string Str = (TypeOfMyObject as MyObject).ReturnUsefulStuff();
        // I know that type has this method! Come on, let me use it!

        MessageBox.Show(Str);
    }
}

In Delphi, this can be achieved by declaring

// ...
// interface
Type TMyObjectClass = class of TMyObject;
// ...
// implementation
ClassVar := TMyChildObject;
Str := TMyObjectClass(ClassVar).ReturnUsefulStuff();

This works thanks to Delphi’s “class of” construct. Compiler is aware that TMyObject has “ReturnUsefulStuff” and TMyChildObject is derived from it, and also it has the reference to the class in ClassVar. It’s everything that is needed. C# doesn’t have the notion of “class of”, it only has The One System.Type that will (hardly) rule them all.
Any suggestions? Will I be forced to use all kinds of ugly Reflection techniques?

  • 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-09T23:46:53+00:00Added an answer on June 9, 2026 at 11:46 pm

    There is a misconception in your code:

    Delphi has the concept of “class of”, which means that a field/variable can keep a reference for the class itself. The .net clr doesn’t have this concept. In .Net at runtime you can query for information about a specific type. When you call obj.GetType() you get a Type object containing information about the type. However the Type is not the class itself like in Delphi, it is just a regular object with a bunch of information.

    That’s why this is illegal in .net:

    // TypeOfMyObject is a object of the class Type, which MyObject does not inherit.
    (TypeOfMyObject as MyObject).ReturnUsefulStuff();
    

    So, yes, in this case you must use reflection.

    var method = typeOfMyObject.GetMethod("ReturnUsefulStuff", 
          BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
    var result = method.Invoke(null, null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement in my class Invoke() method with the same behavior as
So I have this method that get executed repeatedly public static boolean isReady(String dirPath,
I need to invoke a method without knowing a priori what will the amount
Basically; I need to invoke a method on my WPF page, from a WPF
I need to invoke the setter methods of a class using reflection, and the
I have a list of callback functions that I need to invoke when an
Sorry for the verbose introduction that follows. I need insight from someone knowing P/Invoke
It's there a way to invoke a method and the return type is strong
I have a DLL from which I need to P/Invoke the following C method:
I need to create an method invoker that any thread (Thread B for example

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.