Asked  6 Months ago    Answers:  5   Viewed   42 times

I know in php you are able to make a call like:

$function_name = 'hello';
$function_name();

function hello() { echo 'hello'; }

Is this possible in .Net?

 Answers

55

Yes. You can use reflection. Something like this:

Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(TheCommandString);
theMethod.Invoke(this, userParameters);

With the above code, the method which is invoked must have access modifier public. If calling a non-public method, one needs to use the BindingFlags parameter, e.g. BindingFlags.NonPublic | BindingFlags.Instance:

Type thisType = this.GetType();
MethodInfo theMethod = thisType
    .GetMethod(TheCommandString, BindingFlags.NonPublic | BindingFlags.Instance);
theMethod.Invoke(this, userParameters);
Tuesday, June 1, 2021
 
Deyson
answered 6 Months ago
72

No. Since bytearrays are also strings in PHP, a simple replacement of the 8-bit string functions with their mb_* counterparts will cause nothing but trouble. Functions like strlen() and substr() are probably more frequently used with bytes than actual text strings.

At the place I last worked, we managed to build a multilingual web-site (Arabic, Hindi, among other languages) in PHP without using the mbstring library at all. Text string manipulation actually doesn't happen that often. When it does, it would require far more care than just changing a function name. Most of the challenges, I've found, lie on the HTML side. Getting a page layout to work with a RTL language is the non-trivial part.

I don't know if you're just using Arabic as an example. The difficulty of internationalization can vary quite substantially depending on whether "international" means European languages only (plus Russian), or if it's inclusive of Middle-Eastern, South-Asian, and Far-East languages.

Wednesday, March 31, 2021
 
Smandoli
answered 9 Months ago
66

You should simply store the return value in a variable:

$deliveryPrice = getDeliveryPrice(12);
echo $deliveryPrice; // will print 20

The $deliveryPrice variable above is a different variable than the $deliveryPrice inside the function. The latter is not visible outside the function because of variable scope.

Thursday, August 5, 2021
 
dimitarvp
answered 4 Months ago
87

You can do it, but there's not much built-in to help out.

typedef struct { 
    char *event_name;
    void (*handler)();
} event_handler;

then search through an array (or whatever) of event_handlers, and when you find the right name, call the associated function.

Monday, August 30, 2021
 
giser_yugang
answered 3 Months ago
89

Have a look at Activator.CreateInstance(String, String):

Activator.CreateInstance("Namespace.Forms", "Login");

you can also use the Assembly class (in System.Reflection namespace):

Assembly.GetExecutingAssembly().CreateInstance("Login");
Wednesday, November 10, 2021
 
Averroes
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share