Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a way to get method arguments inside a method body as an array of objects?

For example:
C#
public void main()
{
   dosomething(2, "a");
}

public void dosomething(int a, string b)
{
     // get 2, "a" as object[] here
}

EDIT:
Some ascii art to explain the context.
--------                              --------
|client|     --- serialize call -->   |server|
--------                              --------
dosomething(2,"a");   --->            dosomething(2,"a");

Create a proxy on the client which will send the method name and args to the server and return it's value to the client.
Posted
Updated 31-May-13 5:49am
v2
Comments
Sergey Alexandrovich Kryukov 31-May-13 11:22am    
May I ask why? In any case, this is not what Reflection does. Reflections gives you metadata, not data. And it gives you the possibility to initialize an object.
—SA
Mehdi Gholam 31-May-13 11:26am    
I need this to create automatic proxies instead of writing the code yourself.
You can get the method name with MethodBase.GetCurrentMethod(), I was wondering you could get more and type less.
Sergey Alexandrovich Kryukov 31-May-13 12:23pm    
Now it tells me something.

Such things are already done in marshaling of the method call — in particular, in Service Contract technique used in WCF services. Why not trying to embrace it?
—SA

 
Share this answer
 
Comments
Mehdi Gholam 31-May-13 11:32am    
I was wading through stackoverflow, you got there sooner :) 5'ed
Sergey Alexandrovich Kryukov 31-May-13 11:34am    
Oops! Mehdi, you betrayed me :-) I was sure you knew all that; please see my comment...
Then I'll have to vote 5.

However, your original question is much more difficult: how to get all parameters in an array, no matter how are they declared. Reflection is no help here...

—SA
Mehdi Gholam 31-May-13 11:37am    
There is a problem with this... see my comment below, although the approach is interesting...
Sergey Alexandrovich Kryukov 31-May-13 11:33am    
I doubt that Mehdi is unfamiliar with this, as he is an experienced developer. He asks a very different thing.
—SA
Sergey Alexandrovich Kryukov 31-May-13 11:35am    
5d after OP's response. See my comments though.
—SA
You can do

public void dosomething(params object[] args)

but I wouldn't without really good reason.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-May-13 11:36am    
You are right, as soon as the parameter is object. There are some reasons though: string.Format, Console.WriteLine...
My 5 anyway.
—SA
[After OP's clarification added to the question]

After clarification, the problem started to resemble Service Contracts, as they are used in WFC. I think it can be a good food for thought:
http://en.wikipedia.org/wiki/Service-oriented_architecture#Programmatic_service_contract[^],
http://msdn.microsoft.com/en-us/library/ms733070.aspx[^],
http://msdn.microsoft.com/en-us/library/ms734686.aspx[^],
http://msdn.microsoft.com/en-us/library/system.servicemodel.servicecontractattribute.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900