0

For example:

public class MyClass
{
 string stuff = "";
 public doStuff(string stuff)
 {
   this.stuff = stuff;
 }
}

public static Main()
{
string methodName = "doStuff";
List<MyClass> classList = new List<MyClass>();
for(int i = 0;i < 100;i++)
{
classList.Add(new MyClass);
}
foreach(MyClass class in classList)
{
class.Invoke(methodName, new object[]{randomString})
}
}

And so every MyClass's stuff has a different value.

Is there a way I can invoke a method this way using reflection?

Morga121
  • 3
  • 6
  • Do you mean a method on an object within the same process? Or a different one? – Ian Jul 15 '15 at 13:35
  • I cant imagine a possibility of doing this. Because a process is a bit more than a simple class or object you are calling. – ckruczek Jul 15 '15 at 13:36
  • This was just an example with Processes. I could imagine doing this with multiple different objects of a class. – Morga121 Jul 15 '15 at 13:37
  • See this : http://stackoverflow.com/questions/6469027/call-methods-using-names-in-c-sharp – PaulF Jul 15 '15 at 13:37
  • 1
    This sounds like a [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), What problem are you having that using reflection to call methods by name is the solution to that problem? – Scott Chamberlain Jul 15 '15 at 13:39
  • The problem is that I need to call methods of different objects. Like in the upper example – Morga121 Jul 15 '15 at 13:41
  • It sounds like you've made a design error somewhere earlier in the process that has put you in this position. While this is certainly possible, the need to do it is a strong signal that you want to go back and rework things to avoid needing this in the first place. – Joel Coehoorn Jul 15 '15 at 13:57
  • I'd wager that one or more of inheritance, interfaces, or switch blocks would solve your actual problem (the nature of which remains elusive). – Stephen Kennedy Jul 15 '15 at 14:19
  • This thread at codeproject solved my problem : http://www.codeproject.com/Articles/17269/Reflection-in-C-Tutorial – Morga121 Jul 15 '15 at 14:30
  • I didn't know that every object has its own type information. I taugth object share the type info . – Morga121 Jul 15 '15 at 14:32

0 Answers0