Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 949951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:29:11+00:00 2026-05-15T23:29:11+00:00

Say I have a method: public void SomeMethod(String p1, String p2, int p3) {

  • 0

Say I have a method:

 public void SomeMethod(String p1, String p2, int p3)
 {

 #if DEBUG
    object[] args = GetArguments();
    LogParamaters(args);
 #endif

     // Do Normal stuff in the method
 }

Is there a way to retrieve an array of the arguments passed into the method, so that they can be logged?

I have a large number of methods and want to avoid manually passing the arguments by name to the logger, as human error will inevitably creep in.

I’m guessing it will involve reflection in some form – which is fine, as it will only be used for debugging purposes.

Update

A little more information:

I can’t change the method signature of SomeMethod, as it is exposed as a WebMethod and has to replicate the legacy system it is impersonating.

The legacy system already logs the arguments that are passed in. To start with the new implementation will wrap the legacy system, so I’m looking to log the parameters coming into the C# version, so that I can verify the right parameters are passed in in the right order.

I’m just looking to log the argument values and order, not their names.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:29:12+00:00Added an answer on May 15, 2026 at 11:29 pm

    Here’s what I came up with as a solution:

    PostSharp or another AOP solution wasn’t really practical in this situation, so unfortunately I had to abandon that idea.

    It appears that while it is possible to parameter names and types using reflection, the only way to access the runtime values is with a debugger attached.

    See here for more info:

    StackOverflow

    microsoft.public.dotnet.framework

    So that still left me with the problem of ~50 methods that needed this logging adding by hand.

    Reflection to the rescue…

    public String GetMethodParameterArray()
        {
            var output = new StringBuilder();
            output.AppendLine();
    
            Type t = typeof(API);
            foreach (var mi in t.GetMethods())
            {
                    var argsLine = new StringBuilder();
                    bool isFirst = true;
                    argsLine.Append("object[] args = {");
                    var args = mi.GetParameters();
    
                    foreach (var pi in args)
                    {
                        if (isFirst)
                        {
                            isFirst = false;
                        }
                        else
                        {
                            argsLine.Append(", ");
                        }
                        argsLine.AppendFormat("{0}", pi.Name);
                    }
                    argsLine.AppendLine("};"); //close object[] initialiser
    
                    output.AppendLine(argsLine.ToString());
                    output.AppendFormat("Log(\"{0}\",args);", mi.Name);
                    output.AppendLine();
                    output.AppendLine();
                }
            return output.ToString();
        }
    

    This code snippet loops through the methods on a class and outputs an object[] array initialised with the arguments passed into the method and a Log call containing the arguments and the method name.

    Example output:

    object[] args = {username, password, name, startDate, endDate, cost};
    Log("GetAwesomeData",args);
    

    This block can then be pasted into the top of the method to achieve the required effect.

    It is more manual than I would have liked, but it is a lot better than having to type the parameters by hand and far less error prone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 488k
  • Answers 488k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I recommend you try NewRelic RPM. The free version: RPM… May 16, 2026 at 8:44 am
  • Editorial Team
    Editorial Team added an answer Don't do it. There's no point to doing this since:… May 16, 2026 at 8:44 am
  • Editorial Team
    Editorial Team added an answer var checkBoxes = from x in FindAll<CheckBox>() let recordType =… May 16, 2026 at 8:44 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Say I have a method public void PrintStuff(string stuff, Color color, PageDimensions dimensions) {
Let's say I have a method public void Foo(string bar) that the caller should
Let's say I have a C# method public void CheckXYZ(int xyz) { // do
Say I have this class : public class BaseJob{ String name; public void setName(String
Let's say I have a class: class Aggregate { public: int x; int y;
Well lets say we have this c# code: public override void Write(XDRDestination destination) {
Say, I have a reference to a Class object with SomeType having a static
Say I have some code like namespace Portal { public class Author { public
Say I have a class: public class MyClass { ... } and a webservice
Say I have a method A.Do(Arg arg) which assigns some properties of arg (class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.