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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:55:54+00:00 2026-05-17T00:55:54+00:00

1st Question: In PHP there is: $b = new SomeClass(); $a = foo; $b->$a(something);

  • 0

1st Question:

In PHP there is:

$b = new SomeClass();
$a = "foo";
$b->$a("something"); // This is the same as $b->foo("something");

How do you do this in C#?


2nd Question:

In PHP I can iterate through each method on a class, similar to:

$a = new SomeClass(); // Which implements methodA, methodB;
foreach($method in get_class_methods(SomeClass()))
{
  $method("do stuff with each method");
}

How to do this in C#?


3rd Question

PHP has a magic method __call() if a Class does not implement a method it executes it as a default such as if a method doesn’t exist it still can run

class NewClass()
{
// constructor

__call(class name, array of parameters)
{

}
}

so if you do

$a = new NewClass();
$a->thisDoesntExistButWorks("123","abc");

// The method gets "magically created"  such as
thisDoesntExistButWorks(array with 123 and abc)....
  • 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-17T00:55:54+00:00Added an answer on May 17, 2026 at 12:55 am

    1 and 2 have been answered more than once. So i’ll attempt 3.

    C# does not provide an unknown method method because it doesn’t have to. If you call the Method directly like a.NonExistentMethod() then the program simply wont compile, and the idea of catching the bad call at runtime is irrelevant.

    As you have already found out though it it possible to call methods dynamically at runtime by doing a.GetType().GetMethod("NonExistentMethod").Invoke(a, new object[0]). This will fail with a NullReferenceException because the call to GetMethod returns null. However this situation is detectable, and so maybe we can do something about it. Here is an extension method that behaves as much like PHP as possible. Please note that this is untested, and should never be used in production (or probably even experimental) code.

    This can by used by doing this a.CallOrDefault("method name", arg1, arg2);

    public static class PhpStyleDynamicMethod
    {
        public static void __call(Type @class, object[] parameters)
        {
            // Do stuff here.
        }
    
        public static object CallOrDefault(this object b, string method, params object[] parameters)
        {
            var methods = b.GetType().GetMethods().Where(m => m.Name == method && DoParametersMatch(m.GetParameters(), parameters));
    
            var count = methods.Count();
    
            if (count == 1)
                return methods.Single().Invoke(b, parameters);
    
            if (count > 1)
                throw new ApplicationException("could not resolve a single method.");
    
            __call(b.GetType(), parameters);
    
            return null;
        }
    
        public static bool DoParametersMatch(ParameterInfo[] parameters, object[] values)
        {
            if (parameters == null && values == null) return true;
            if (parameters == null) return false;
            if (values == null) return false;
            if (parameters.Length != values.Length) return false;
    
            for(int i = 0; i < parameters.Length; i++)
            {
                var parameter = parameters[i];
                var value = values[i];
    
                if (!parameter.GetType().IsAssignableFrom(value.GetType()))
                    return false;
            }
    
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question specifically relates to my experience with PHP, but there's no reason why
this is the 2nd part of the 1st question using c# pointers so pointers
I have 1st popup, and this 1st popup opens 2nd popup Code: <view-state id=paneMaintenance
I am new to php and sql, and i have one tiny question about
I am wondering this question for a long time, how does PHP handle references
I have a site orders.something.com and other site hr.something.com. My 1st question should I
As a PHP programmer new to Perl working through 'Programming Perl', I have come
Probably a silly question, but I'm new to all this. I am creating a
I'm new to Drupal & PHP. I have a similar problem to this unanswered
1st Question: How do I get the title and subtitle of a selected annotation?

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.