如何将Int32转换为对象[]
问题描述:
请帮助:怎么做-
Pls help: how to do this-
Int32 UserID;
object[] parameters = new[] { UserID };
---
---
Error: Cannot implicitly convert type 'int[]' to 'object[]'
答
VS从创建数组时使用的变量类型(在本例中为userID)派生数组的类型,有两种解决方法:
VS is deriving the type of the array from the type of the variable used in its creation (in this case userID), there are 2 ways around this, either:
object[] parameters = new[] { (object)UserID };
或
or
object[] parameters = new object[] { UserID };
就个人而言,我更喜欢第二个选项.
Personally, i prefer the 2nd option.
为您提供一些建议:
private Int32 UserID;
private Int32 AdminID;
private List<Int32> ListOInt32s;
private List<object> ListOObjects;
private void MakeIDS(Int32 uID, Int32 aID)
{
UserID = uID;
AdminID = aID;
// anonymous type
// exists only in the scope of this method call
var IDS = new {UserID, AdminID};
// generic strongly typed
ListOInt32s = new List<Int32> {UserID, AdminID};
// generic typed as Object
ListOObjects = new List<object> {UserID, AdminID};
}
private void button1_Click(object sender, EventArgs e)
{
MakeIDS(4,5);
}
这可能会有所帮助,
It might be helpful,
static void Main(string[] args)
{
Int32 one = 1, two = 2;
object[] myArray = { one, two };
}
:)