如何将命名参数的可变数量传递给方法?

时间:2022-04-27 18:02:35

I understand how to get the names of parameters passed to a method, but let's say I have a method declaration as follows:

我知道如何将参数的名称传递给一个方法,但假设我有一个方法声明如下:

static void ParamsTest(string template, params object[] objects)

and I want to use object/property names in my template for substitution with real property values in any of the objects in my `objects' parameter. Let's then say I call this method with:

我想在我的模板中使用对象/属性名来替换我的“对象”参数中的任何对象的实际属性值。假设我用:

ParamsTest("Congrats", customer, purchase);

I will only be able to retrieve two parameter names trying to fill out the template, viz, template, and objects, and the names of the objects in the objects collection are forever lost, or not?

我将只能检索两个参数名称来填充模板,即模板、模板和对象,对象集合中的对象名称是否永远丢失?

I could require a List<object> as my second parameter, but I feel there is somehow a more elegant solution, maybe with a lambda or something. I don't know, I'm not used to using lambdas outside of LINQ.

我可以要求列表作为我的第二个参数,但是我觉得有一个更优雅的解决方案,可能是lambda或其他什么。我不知道,我不习惯在LINQ之外使用lambdas。

3 个解决方案

#1


5  

Inpsired by Mark I can offer an anonymous type answer :

我可以提供一个匿名类型的答案:

using System;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            ParamsTest(new { A = "a", B = "b"});
        }

        public static void ParamsTest(object o)
        {
            if (o == null)
            {
                throw new ArgumentNullException();
            }
            var t = o.GetType();
            var aValue = t.GetProperty("A").GetValue(o, null);
            var bValue = t.GetProperty("B").GetValue(o, null);
        }
    }
}

However this DOES have drawbacks :

但是这也有缺点:

  • No type safety. I can pass in any object
  • 没有类型安全。我可以传入任何对象
  • I can pass in an anonymous type with different members and/or types than those expected
  • 我可以使用不同的成员和/或类型传递匿名类型
  • You should also do more checking than in this short sample
  • 您还应该做更多的检查,而不是在这个简短的样本

#2


0  

You could define a parameters object :

可以定义一个parameters对象:

public class ParamsTestParams {
    public string A { get; set; }
    public string B { get; set; }

    public static readonly ParamsTestParams Empty = new ParamsTestParams();
}

Change the methods signature to :

将方法签名更改为:

ParamsTest(ParamsTestParams parameters)

And call your method like so :

并像这样调用你的方法:

ParamsTest(new ParamsTestParams { A = "abc" });

or :

或者:

ParamsTest(new ParamsTestParams { B = "abc" });

or :

或者:

ParamsTest(new ParamsTestParams { A = "abc", B = "xyz" });

or :

或者:

ParamsTest(ParamsTestParams.Empty); // instead of null so you don't have to do null checks in the method's body

and so on.

等等。

#3


0  

I would probably just do it like this:

我可能会这样做:

static void ParamsTest(string template, object[] objects)

and call it like this:

这样称呼它:

ParamsTest("Congrats", new object[] { customer, purchase });

#1


5  

Inpsired by Mark I can offer an anonymous type answer :

我可以提供一个匿名类型的答案:

using System;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            ParamsTest(new { A = "a", B = "b"});
        }

        public static void ParamsTest(object o)
        {
            if (o == null)
            {
                throw new ArgumentNullException();
            }
            var t = o.GetType();
            var aValue = t.GetProperty("A").GetValue(o, null);
            var bValue = t.GetProperty("B").GetValue(o, null);
        }
    }
}

However this DOES have drawbacks :

但是这也有缺点:

  • No type safety. I can pass in any object
  • 没有类型安全。我可以传入任何对象
  • I can pass in an anonymous type with different members and/or types than those expected
  • 我可以使用不同的成员和/或类型传递匿名类型
  • You should also do more checking than in this short sample
  • 您还应该做更多的检查,而不是在这个简短的样本

#2


0  

You could define a parameters object :

可以定义一个parameters对象:

public class ParamsTestParams {
    public string A { get; set; }
    public string B { get; set; }

    public static readonly ParamsTestParams Empty = new ParamsTestParams();
}

Change the methods signature to :

将方法签名更改为:

ParamsTest(ParamsTestParams parameters)

And call your method like so :

并像这样调用你的方法:

ParamsTest(new ParamsTestParams { A = "abc" });

or :

或者:

ParamsTest(new ParamsTestParams { B = "abc" });

or :

或者:

ParamsTest(new ParamsTestParams { A = "abc", B = "xyz" });

or :

或者:

ParamsTest(ParamsTestParams.Empty); // instead of null so you don't have to do null checks in the method's body

and so on.

等等。

#3


0  

I would probably just do it like this:

我可能会这样做:

static void ParamsTest(string template, object[] objects)

and call it like this:

这样称呼它:

ParamsTest("Congrats", new object[] { customer, purchase });