c# How can i use class name in another class string -
given 2 following method definitions:
public int sumclass(int a, int b) { return + b; } public int multclass(int a, int b) { return * b; }
i able call either of methods using name of method string
public int process(string classname, int a, int b) { // classname="sumclass" or classname="multclass" return classname(a,b); }
i show reflection approach want. won't because there better approach. why want that? because have different methods addition , multiplication. 1 better way to use enum
:
public enum processtype { multiplication, addition, division, subtraction } public int process(processtype processtype, int a, int b) { switch (processtype) { case processtype.addition: return sumclass(a, b); case processtype.multiplication: return multclass(a, b); // ... } }
so if want sum 2 + 3:
int result = process(processtype.addition, 2, 3);
Comments
Post a Comment