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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:07:17+00:00 2026-05-13T08:07:17+00:00

Edit: In real life i don’t have a Book class. This is just an

  • 0

Edit: In real life i don’t have a Book class. This is just an example to be clear. Real problem really needs reflection to solve it.

Suppose that I have some classes:

Book, Apple, Door.

class Book
{
   ...
   public decimal getPrice()
   {...}
   public string getTitle()
   {...}
   public decimal getAuthor()
   {...}
}

and something same for other classes.

May i call a class method dynamically from a string:

Book myBook = new Book("Title", "Author", 44);

string title = runMethod(myBook, "getTitle");
  • 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-13T08:07:17+00:00Added an answer on May 13, 2026 at 8:07 am

    You can use something like this, using reflection:

    class Program
    {
    static void Main(string[] args)
    {
        var b = new Book("Book Title", 2342);
    
        Console.WriteLine(CallMethod(b, "GetTitle", "Not Found"));
    }
    
    public static K CallMethod<T,K>(T a, string method, K defaultOjb)
    {
        var t = a.GetType();
    
        var mi = t.GetMethod(method);
        if (mi == null) return defaultOjb;
    
        var ret=mi.Invoke(a, new object[] {});
    
        return (K) ret;
    }
    }
    
    public class Book
    {
    private readonly string _title;
    private readonly decimal _price;
    
    public decimal GetPrice()
    {
        return _price;
    }
    public string GetTitle()
    {
        return _title;
    }
    
    public Book(string title, decimal price)
    {
        _title = title;
        _price = price;
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll try to start with the real life example in my case: I have
I'm trying to solve the following real-life problem you might have encountered yourselves: You
EDIT I isolated a real minimal example which does not work (it is a
This is NOT a life or death issue. I do have a backup from
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
EDIT: See my answer below--> I am wanting to have a view that when
Edit (updated question) I have a simple C program: // it is not important
EDIT : It turned out that this can only be done through an external
I have hit upon a real brain scorcher in C++, it has never happened
Firstly, please don't dismiss this question - I'm aware it's an ugly situation but

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.