Joshua Beckers
Joshua Beckers

Reputation: 907

How to call static methods from name in string variable in C#?

I have strings with a math function names. E.g. Sin, Abs, Max ...

How can I call for example Math.Sin(arg) in a way where I can specify the Sin part based on a variable?

Upvotes: 2

Views: 468

Answers (1)

Shervin Ivari
Shervin Ivari

Reputation: 2501

You Can use reflection

var concreteType = typeof(Math);
var result = concreteType.GetMethod("Sin").Invoke(null, new object[] { numbers });

Upvotes: 2

Related Questions