如何在c#中执行对象类型[*]?

时间:2022-09-01 14:27:15

I'm fairly new to C# but have searched the web for an hour and no joy...

我对c#相当陌生,但我在网上搜索了一个小时,没有任何乐趣……

I need to ascertain whether an object is a non-zero index array, i.e. object[*]

我需要确定一个对象是否是一个非零索引数组,即object[*]

I've tried:

我试过了:

if(v != null && v.GetType() == typeof(object[*]))

and if(v is object[*])

如果(v是对象[*])

as well as overloaded methods Method(object v) and Method(object[*] v)

以及重载方法方法(object v)和方法(object[*] v)

All result in compilation errors.

所有这些都会导致编译错误。

As I can't cast object[*] to object[] and then test GetLowerBound(0) how the hell can I test this type?

因为我不能将对象[*]转换为对象[],然后测试GetLowerBound(0)我怎么能测试这个类型呢?

(Please don't tell me this bad code/design, it's coming from Excel so I obviously cannot change that).

(请不要告诉我这个糟糕的代码/设计,它来自Excel,所以我显然不能改变它)。

4 个解决方案

#1


2  

Try Type.IsArray, Type.GetArrayRank and Type.GetElementType if necessary.

试类型。IsArray,类型。GetArrayRank和类型。GetElementType如果必要的话。

If you need to call GetLowerBound you can safely cast the object to System.Array.

如果需要调用GetLowerBound,可以将对象转换为System.Array。

#2


4  

I think you want:

我认为你想要的:

Array array = v as Array;
if(array != null && array.GetLowerBound(0) != 0)
{
    ...
}

Test:

测试:

var array = Array.CreateInstance(typeof(object), new[] { 3 }, new[] { 1 });
Console.WriteLine(array.GetType());
Console.WriteLine(array.GetLowerBound(0));

Output:

输出:

System.Object[*]
1

#3


0  

If the object is really typed as object[] and not as string[] or some other array then this will do

如果对象是作为对象[]而不是作为字符串[]或其他数组进行类型化,那么这就可以了

typeof(object[])

A placeholder for the array size is not required, since this is the normal way of declaring arrays

不需要为数组大小设置占位符,因为这是声明数组的常规方式

object[] myObjectArray;

EDIT:

编辑:

In your case, you could simplify the statement to

在你的例子中,你可以化简成

if(v is object[])

since

((object[])null) is object[]

yields false

产生错误的

#4


0  

 Object [] v = new Object[2];

 if (!ReferenceEquals(v, null))
          {
              if ((v.GetType() is System.Object)
                  && (v.GetType().IsArray))
              {

              }
          }

but just 'v' is initialize not your objects inside !!!

但是仅仅v是初始化而不是你里面的对象!!!

#1


2  

Try Type.IsArray, Type.GetArrayRank and Type.GetElementType if necessary.

试类型。IsArray,类型。GetArrayRank和类型。GetElementType如果必要的话。

If you need to call GetLowerBound you can safely cast the object to System.Array.

如果需要调用GetLowerBound,可以将对象转换为System.Array。

#2


4  

I think you want:

我认为你想要的:

Array array = v as Array;
if(array != null && array.GetLowerBound(0) != 0)
{
    ...
}

Test:

测试:

var array = Array.CreateInstance(typeof(object), new[] { 3 }, new[] { 1 });
Console.WriteLine(array.GetType());
Console.WriteLine(array.GetLowerBound(0));

Output:

输出:

System.Object[*]
1

#3


0  

If the object is really typed as object[] and not as string[] or some other array then this will do

如果对象是作为对象[]而不是作为字符串[]或其他数组进行类型化,那么这就可以了

typeof(object[])

A placeholder for the array size is not required, since this is the normal way of declaring arrays

不需要为数组大小设置占位符,因为这是声明数组的常规方式

object[] myObjectArray;

EDIT:

编辑:

In your case, you could simplify the statement to

在你的例子中,你可以化简成

if(v is object[])

since

((object[])null) is object[]

yields false

产生错误的

#4


0  

 Object [] v = new Object[2];

 if (!ReferenceEquals(v, null))
          {
              if ((v.GetType() is System.Object)
                  && (v.GetType().IsArray))
              {

              }
          }

but just 'v' is initialize not your objects inside !!!

但是仅仅v是初始化而不是你里面的对象!!!