Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Getting value of params using reflection

Tags:

c#

reflection

How can I get the values of parms (in a loop using reflection). In previous question, someone showed me how to loop through the parms using reflection.

static void Main(string[] args) {     ManyParms("a","b","c",10,20,true,"end");     Console.ReadLine();  }  static void ManyParms(string a, string b, string c, int d, short e, bool f, string g) {     var parameters = MethodBase.GetCurrentMethod().GetParameters();     foreach (ParameterInfo parameter in parameters)     {         string parmName = parameter.Name;         Console.WriteLine(parmName);          //Following idea required an object first          //Type t = this.GetType();         //t.GetField(parmName).GetValue(theObject));      } } 

If you must know why I want to do this, please see here: .NET Reflection of all method parameters


Thanks all - it seems like in Python, PERL, PHP this would be easy.
Even though it may not be reflection, if I use reflection to get the field name, seems like there would be an easy dynamic way to get the value based on the name. I haven't tried AOP (Aspect Oriented Programming) the solutions yet. This is one of those things that if I can't do it in an hour or two, I probably won't do it.

like image 482
NealWalters Avatar asked Dec 08 '09 14:12

NealWalters


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.


2 Answers

You can hack your way around this by creating an anonymous type inside your method and taking advantage of projection initialisers. You can then interrogate the anonymous type's properties using reflection. For example:

static void ManyParms(     string a, string b, string c, int d, short e, bool f, string g) {     var hack = new { a, b, c, d, e, f, g };      foreach (PropertyInfo pi in hack.GetType().GetProperties())     {         Console.WriteLine("{0}: {1}", pi.Name, pi.GetValue(hack, null));     } } 
like image 124
LukeH Avatar answered Sep 23 '22 02:09

LukeH


You can't, basically - at least not without hooking into the debugger/profiling API.

In theory there could be some way of the StackFrame class exposing parameter values, but it doesn't - and I suspect that in order to do so, you'd have to remove several optimisations.

like image 27
Jon Skeet Avatar answered Sep 20 '22 02:09

Jon Skeet