English 中文(简体)
采用带束弹的方法
原标题:Calling method with params from method with params

下面是:

void SomeMethod ()
{
  ParamMethod1 (1,2,3);
}

void ParamMethod1 (params object[] arg)
{
   ParamMethod2 (0, arg); //How to call ParamMethod2 in order to it works 
   //as I want
}

void ParamMethod2 (params object[] arg)
{
  //I want  arg  contains 0,1,2,3 instead of 0, System.object[]
}

Thanks for any help.

问题回答

你们可以与LINQ打交道,但仍然需要TArray(签名)

ParamMethod2(new object [] { 0 }.Concat(arg).ToArray());

Edited to Add:

根据关于业绩的评论,我对建议的方法进行了一些非常基本的业绩比较。

For 1 million calls

80ms -- Direct call: ParamMethod2(0,1,2,3);
280ms -- Using CopyTo
400ms -- Using LINQ
590ms -- Using AddRange

Of course if you changed object to int and a few other things you will improve performance further. Personally I d use LINQ because it is fast enough for 99% of cases and in the other case I wouldn t use params in the first place.

一种方式虽然不一定是理想,但只能是:

void ParamMethod1 (params object[] arg)
{
    var args = new List<object>();
    args.Add(0);
    if (arg != null)
    {
        args.AddRange(arg);
    }        
    ParamMethod2(args.ToArray());
}

That is to say, regardless of how this is done, we need to combine 0 and the contents of arg into a single collection - there will doubtless be a more efficient way, and perhaps even a simpler approach that I can t see for lack of coffee this morning, but this will do the trick, so to speak.

Change this method

void ParamMethod2 (params object[] arg) { //I want arg contains 0,1,2,3 instead of 0, System.object[] }

To

void ParamMethod2 (int firstValue, params object[] arg) { //Now, firstvalue always will have 0 and remaining passed variables will have 1,2,3, etc. }

希望是你们的要求。

这里请注意,如果你想用一种只用灯塔作为论据的方法,你会遇到问题,因为你们的汇编者总是将这种明确的第一论点称作签字,而不是只用灯塔。

void ParamMethod1 (params object[] arg)
{
   if(arg == null || arg.Length == 0)
   {
      ParamMethod2(0);
      return;
   }
   var newArray = new int[arg.Length + 1];
   newArray[0] = 0;
   arg.CopyTo(newArray,1);
   ParamMethod2 (newArray); 
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签