简体   繁体   English

C#:将特定 Enum 类型的数组作为 params object[] 参数传递

[英]C#: passing in array of a specific Enum type as params object[] argument

When attempting to pass in an array of a specific enum type as a function argument via params object[], I am unable to pass in an actual array without casting to object[] first.当尝试通过 params object[] 将特定enum类型的数组作为函数参数传递时,我无法在不首先转换为 object[] 的情况下传递实际数组。 But if I do so I do not have access to the original enum type.但是如果我这样做,我将无法访问原始enum类型。

Given the enum and function shown below, this works fine:鉴于下面显示的enum和函数,这可以正常工作:

var works = SomeFunction(Season.Fall, Season.Winter);

However, this fails because it cannot implicitly convert to object, so it passes array as first argument as opposed to individual arguments:但是,这失败了,因为它不能隐式转换为对象,因此它将数组作为第一个参数而不是单个参数传递:

var seasons = new Season[] {Season.Fall, Season.Winter};
var fail = SomeFunction(seasons);

It also cannot cast to object[] because then later it is unable to determine the original type.它也不能转换为 object[],因为之后它无法确定原始类型。 How can I use a predetermined array to pass it in?如何使用预先确定的数组将其传入?

[Flags]
public enum Season : byte
{
    None = 0,
    Spring = 1,
    Summer = 2,
    Fall = 4,
    Winter = 8
}

... ...

public Something SomeFunction(params object[] values) 
{
     ...
     foreach (var value in values)
     {
          var type = value.GetType();
          ...
     }
}

UPDATE: it works to convert the Season[] array to object[], but that seems hokey:更新:它可以将 Season[] 数组转换为 object[],但这似乎很奇怪:

var seasons = new object[original.Length];
for (int i = 0; i < original.Length; i++)
{
       seasons[i] = original[i];
}

It's as simple as converting an object[] containing the enums as opposed to casting the array:它就像转换包含枚举的 object[] 而不是转换数组一样简单:

var original = new Season[] {Season.Fall, Season.Winter};

var selectedSeasons = new object[original.Length];
for (int i = 0; i < original.Length; i++)
{
       selectedSeasons[i] = original[i];
}

var works = SomeFunctionWithObjectParamsArgs(selectedSeasons);

Yes, ultimately changing the function signature to be generic would be best, but doesn't apply in my case (third-party API).是的,最终将函数签名更改为通用将是最好的,但不适用于我的情况(第三方 API)。

This code compiles in .NET 3.5, 4.0, and 4.5.此代码在 .NET 3.5、4.0 和 4.5 中编译。 Are you getting a compiler error when you build?构建时是否遇到编译器错误?

class Program
{
    static void Main(string[] args)
    {
        var p = new Program();
        p.SampleFunc(Seasons.Winter, Seasons.Fall);
    }

    void SampleFunc(params object[] elements)
    {
        foreach(var element in elements)
        {
            if (element is Seasons)
            {
                // do something.
            }
        }
    }

    enum Seasons
    {
        Winter, Spring, Summer, Fall
    }
}

I think this does it...我认为这样做...

public enum MyEnum
{
    EnumValue1,
    EnumValue2,
    EnumValue3
}

class Program
{
    static void Main(string[] args)
    {
        var TheEnumList =(from list in Enum.GetValues(typeof (MyEnum)).Cast<int()
                          select new {EnumValueInstance = (MyEnum) list})
                         .ToList();
        TheEnumList.ForEach(enumItem => Console.WriteLine(enumItem.EnumValueInstance));
        var result = SomeFunction(TheEnumList);
        Console.WriteLine(result);
        Console.ReadKey();
    }

    public static MyEnum SomeFunction(params object[] values)
    {
        dynamic obj = values[0];
        return obj[0].EnumValueInstance;
    }
}

Consider using the Cast<>() LINQ Method instead of a for loop, this looks cleaner:考虑使用Cast<>() LINQ 方法而不是 for 循环,这看起来更清晰:

var seasons = SomeFunction(seasons.Cast<object>().ToArray());

There's no other way you can cast an array of value types to an array of objects , you can't leverage array covariance in this case because it only works between arrays of compatible reference types.没有其他方法可以将值类型数组转换为对象数组,在这种情况下不能利用数组协方差,因为它仅适用于兼容引用类型的数组。

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

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