Get the number of parameters passed to the method?

Is it possible to get the number of parameters passed to the method at runtime, and if so, how?

For example, if it were possible, I could use the database interaction method as follows:

public void AddSomethingToDatabase(string parameter1, string parameter2)
{
   ...
   foreach(param in parameters)
   {
      sp.AddParameter(GetName(param),param));
   }
   conn.Execute(...);
}

I try not to add / change a line in the code every time I change the parameters of the stored procedure, instead changing the method signature with the correct parameters of the stored procedure. In this case parameter1, parameter2they will be the actual names of the parameters of the stored procedure. Any ideas?

+3
source share
7 answers

Can you pass parameters as a collection? Thus, it is unlimited and easy to use. This is how I do it for my own projects.

public void AddSomethingToDatabase(Dictionary<string, object> parameters)
{
   foreach(KeyValuePair<string, object> param in parameters)
   {
      string paramname = param.Key;
      object paramvalue= param.Value;
      sp.AddParameter(paramname, paramvalue);
   }
   conn.Execute(...);
}

EDIT: , .

, . , , , DTO

public void AddSomethingToDatabase(string param1, int param2)
{
   Dictionary<string, object> parameters = new Dictionary<string, object>();
   parameters.Add("pID", param1);
   parameters.Add("pName", param2);

   ModifyDatabase(parameters, "update_myTable");
}

public void ModifyDatabase(Dictionary<string, object> parameters, string procedure)
{
   // Do necessary checks on parameters here
   // Check database availability
   // And many other checks that would be recurring for every database transaction
   // ... that why I have them all in one place. Executing Queries is the same
   // ... every time. Why would you write the error handling twice? :-)

   // Loop parameters and fill procedure parameters

   // Execute the lot
}
+3

:

public int AddSomethingToDatabase(params object[] parameters)
+4
Console.WriteLine(MethodInfo.GetCurrentMethod().GetParameters()[0].Name);

.

, , .

+1
public class NameValue
{
    public NameValue(string name, object value)
    {
        Name = name;
        Value = value;
    }
    public string Name { get; set; }
    public object Value { get; set; }
}

private void DoSomething(params NameValue[] args)
{
    foreach (var nameValue in args)
    {
        //sp.AddParameter(nameValue.Name, nameValue.Value);
    }
}

private void GenerateTable(Table table)
{
    DoSomething(new NameValue("name", "Jonas"), new NameValue("Age", 99));
}
+1

, BLToolkit , .

BLT sproc ( sproc ).

[SprocName("sp_MySproc")]
public abstract void CallMySproc(string @parameter1, string @parameter2); 

.

BLT , Mono.

: BLT , . , , , .

+1

I do not think that you can get parameter values, even with reflection. If you do not refer to them specifically in the body of the method, the compiler simply optimizes them. See These messages, for example: Can I get parameter names / values ​​procedurally from the current executable function?

+1
source
public class SPParamCollection : List<SPParams>{ }
public struct SPParams
{
    public string Name { get; set; }
    public object   Value { get; set; }
    public SqlDbType SqlDbType { get; set; }
}
public void AddSomethingToDatabase(SPParamCollection arrParam)
{
    foreach (SPParams param in arrParam)
        cmd.Parameters.Add(param.Name, param.Value);
}
0
source

All Articles