简体   繁体   English

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

[英]How can I pass a variable number of named parameters to a method?

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. 我想在模板中使用对象/属性名称来替换“ objects”参数中任何对象中的不动产值。 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? 我将只能检索试图填充模板的两个参数名称,即viz, templateobjects ,而objects集合中的objects名称会永远丢失,还是不会?

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. 我可能需要将List<object>作为第二个参数,但是我觉得有某种更优雅的解决方案,可能是使用lambda之类的东西。 I don't know, I'm not used to using lambdas outside of LINQ. 我不知道,我不习惯在LINQ之外使用lambda。

Inpsired by Mark I can offer an anonymous type answer : 由Mark推举,我可以提供匿名类型的答案:

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 您还应该比此简短示例中进行更多检查

You could define a parameters object : 您可以定义一个参数对象:

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. 等等。

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 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM