有没有办法从myFunc的distingish myFunc的(1,2,3)(新INT [] {1,2,3})? [英] Is there a way to distingish myFunc(1, 2, 3) from myFunc(new int[] { 1, 2, 3 })?

查看:142
本文介绍了有没有办法从myFunc的distingish myFunc的(1,2,3)(新INT [] {1,2,3})?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给你所有的C#向导的问题。我有一个方法,调用它myFunc的,它需要可变​​长度/类型参数列表。 myFunc的本身的说法签名是 myFunc的(params对象[]参数)和我使用反射的列表(想到这有点像printf的,例如)。



我要治疗 myFunc的(1,2,3)不同> myFunc的(新INT [] {1,2,3})。也就是说,myFunc的体内,我想历数我的论点的类型,并愿与{INT,INT,INT}而不是INT []结束了。现在我得到了后者:实际上,我不能区分这两种情况下,他们都进来为int []。



我曾希望前者会显示为OBS []。长度= 3,用OBS [0] = 1,等等。



和我预想后者显示为OBS []。长度= 1,与OBS [0] = {INT [3]}



< ?p>可以这样做,还是我要求不可能


解决方案

嗯,这将做到这一点:

 使用系统; 

类节目
{
静态无效的主要(字串[] args)
{
Console.WriteLine(第一呼叫);
美孚(1,2,3);
Console.WriteLine(第二个电话);
美孚(新INT [] {1,2,3});
}

静态无效美孚(params对象[]值)
{
的foreach(中值对象x)
{
控制台。的WriteLine(x.GetType()名称。);
}
}
}



另外,如果你使用 DynamicObject 您可以使用动态类型实现了类似的结果:

 使用系统; 
使用System.Dynamic;

类节目
{
静态无效的主要(字串[] args)
{
动态D =新ArgumentDumper();
Console.WriteLine(第一呼叫);
d.Foo(1,2,3);
Console.WriteLine(第二个电话);
d.Bar(新INT [] {1,2,3});
}
}

类ArgumentDumper:DynamicObject
{
公众覆盖布尔TryInvokeMember
(InvokeMemberBinder粘结剂,
对象[] ARGS,
OUT对象result)
{
结果= NULL;
的foreach(在args对象x)
{
Console.WriteLine(x.GetType()名称。);
}
返回真;
}
}



这两个程序的输出:

 首先调用
的Int32
的Int32
的Int32
第二调用
的Int32 []

现在给上面的输出,目前还不清楚在哪里你的问题真的来了......从不过如果你用' D中给出美孚(1,2,3) VS 美孚(新的String [] {1,2 3})那么这将是一个不同的问题 - 因为的String [] 对象兼容[] ,但 INT [] 不是。如果是的真正的已经给你的问题​​的情况,然后看动态版本 - 这将在两种情况下工作。


A question to all of you C# wizards. I have a method, call it myFunc, and it takes variable length/type argument lists. The argument signature of myFunc itself is myFunc(params object[] args) and I use reflection on the lists (think of this a bit like printf, for example).

I want to treat myFunc(1, 2, 3) differently from myFunc(new int[] { 1, 2, 3 }). That is, within the body of myFunc, I would like to enumerate the types of my arguments, and would like to end up with { int, int, int} rather than int[]. Right now I get the latter: in effect, I can't distinguish the two cases, and they both come in as int[].

I had wished the former would show up as obs[].Length=3, with obs[0]=1, etc.

And I had expected the latter to show up as obs[].Length=1, with obs[0]={ int[3] }

Can this be done, or am I asking the impossible?

解决方案

Well this will do it:

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("First call");
        Foo(1, 2, 3);
        Console.WriteLine("Second call");
        Foo(new int[] { 1, 2, 3 });
    }

    static void Foo(params object[] values)
    {
        foreach (object x in values)
        {
            Console.WriteLine(x.GetType().Name);
        }
    }
}

Alternatively, if you use DynamicObject you can use dynamic typing to achieve a similar result:

using System;
using System.Dynamic;

class Program
{
    static void Main(string[] args)
    {
        dynamic d = new ArgumentDumper();
        Console.WriteLine("First call");
        d.Foo(1, 2, 3);
        Console.WriteLine("Second call");
        d.Bar(new int[] { 1, 2, 3 });
    }
}

class ArgumentDumper : DynamicObject
{
    public override bool TryInvokeMember
        (InvokeMemberBinder binder,
         Object[] args,
         out Object result)
    {
        result = null;
        foreach (object x in args)
        {
            Console.WriteLine(x.GetType().Name);
        }
        return true;
    }
}

Output of both programs:

First call
Int32
Int32
Int32
Second call
Int32[]

Now given the output above, it's not clear where your question has really come from... although if you'd given Foo("1", "2", "3") vs Foo(new string[] { "1", "2", "3" }) then that would be a different matter - because string[] is compatible with object[], but int[] isn't. If that's the real situation which has been giving you problems, then look at the dynamic version - which will work in both cases.

这篇关于有没有办法从myFunc的distingish myFunc的(1,2,3)(新INT [] {1,2,3})?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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