获取方法参数在C#中的对象[] [英] Getting method parameters as an object[] in C#

查看:167
本文介绍了获取方法参数在C#中的对象[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有给所有的方法参数转换为对象的任何黑暗的,模糊的方式[]?

Is there any dark, obscured way to convert all of the method parameters to an object[]?

在实施使用消息代理两个系统之间的集成,我注意到,大部分由经纪人公开的方法使用了大量的参数。

While implementing the integration between two systems using a message broker, I noticed that most of the methods exposed by the broker uses a lot of parameters.

我想要一个简单的方法来记录每个调用与每个参数的经纪人。是这样的:

I want an easy way to log each call to the broker with every parameter. Something like:

[WebMethod]
public void CreateAccount(string arg1, int arg2, DateTime arg3, ... ) {
    object[] args = GetMethodArgs();
    string log = args.Aggregate("", (current, next) => string.Format("{0}{1};", current, next));
    Logger.Log("Creating new Account: " + args);

    // create account logic
}



我好奇,如果C#提供一些模拟GetMethodArgs();

I'm curious if C# provides something that simulates GetMethodArgs();

推荐答案

您可能只是有2种方法

[WebMethod]
public void CreateAccount(string arg1, int arg2, DateTime arg3)
{ 
    CreateAccountImpl(arg1, arg2, arg3);
}

protected void CreateAccountImpl(params object[] args)
{
    string log = args.Aggregate("", (current, next) => string.Format("{0}{1};", current, next));
    Logger.Log("Creating new Account: " + args);

    // create account logic
}

这篇关于获取方法参数在C#中的对象[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆