简体   繁体   English

无法将“ System.Object []”转换为类型“ System.String []”

[英]Cannot convert 'System.Object[]' to the type 'System.String[]'

Have you got another idea to get this list of data? 您是否有其他想法来获取此数据列表?

I would like to show all my code because I think this can be much more easier for you. 我想展示我的所有代码,因为我认为这对您来说更容易。 In this code you can see how I connect with role file. 在此代码中,您可以看到我如何与角色文件连接。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Interop.Security.AzRoles;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Collections.Generic;




namespace Authman
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {   
            // See the following table for other sample connection strings.
            string connectionString = @"msxml://c:\plik.xml";

            AzAuthorizationStoreClass azStore = new AzAuthorizationStoreClass();
            azStore.Initialize(0, connectionString, null);

            IAzApplication2 azApplication = azStore.OpenApplication2("SatheeshApp", null);

            IAzClientContext3 clientContext = (IAzClientContext3)azApplication.InitializeClientContextFromToken((ulong)WindowsIdentity.GetCurrent().Token, null);


            // Use the default application scope.
            string[] roles = (string[])clientContext.GetRoles("");


            foreach (string role in roles)
            {
                Span3.InnerHtml += role.ToString() + "</br>";
            }



        }
    }
}

在此处输入图片说明

Use the Cast<string>() method. 使用Cast<string>()方法。 This is related to covariance/contravariance. 这与协方差/相反。 You need to cast each item in the array (which is what the Cast method does) rather than the entire array. 您需要转换数组中的每个项目(这是Cast方法的作用),而不是整个数组。 If you really need to convert it into another string array you can use the ToArray method, but since here you're just using a foreach on it there is no need. 如果确实需要将其转换为另一个字符串数组,则可以使用ToArray方法,但是由于这里只使用了foreach ,因此不需要。

foreach(string role in clientContext.GetRoles("").Cast<string>())
{
  //use role
}

You'll need to add a using System.Linq to get the Cast extension method. 您需要添加using System.Linq以获取Cast扩展方法。

string[] roles = clientContext.GetRoles("").Cast<string>().ToArray();
using System.Linq;

string[] roles = someObjectArray.OfType<object>().Select(o => o.ToString()).ToArray();

Hope this helps. 希望这可以帮助。

object[] roles=clientContext.GetRoles("");
foreach(object role in roles)
{
   Span3.InnerHtml + = role.ToString() + "</br>";
}

Why you need to cast your array to string[] ? 为什么需要将数组强制转换为string[] You can use it without casting at all. 您可以使用它而无需投射。

You can just cast to the object[] the error indicates it to be instead; 您可以将其强制转换为object [],但错误表明它已被替代。

object[] roles = (object[])clientContext.GetRoles("");

foreach(object role in roles)
{
    Span3.InnerHtml += role.ToString() + "</br>";
}

As others have pointed out, clientContext.GetRoles("").Cast<string>().ToArray() is the work around. 正如其他人指出的那样, clientContext.GetRoles("").Cast<string>().ToArray()可以解决。

Lets say what happens if the conversion is legal. 假设转换合法时会发生什么。 you have a List listOfAnimals. 您有一个list listOfAnimals。

List<Cow> listOfCows = GetListOfCows();

listOfAnimals = listOfCows;

//After some lines of code

List<Tiger> listOfTiger = GetListOfTigers();

listOfAnimals.Add(listOfTiger); //Epic fail.

You might have accidentally added Tiger to a list of Cow . 您可能不小心将Tiger添加到Cow列表中。 Which is wrong and dangerous. 这是错误和危险的。 The CLR does not allow you to introduce such bugs in to the code. CLR不允许您在代码中引入此类错误。

暂无
暂无

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

相关问题 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object' Asp.net WebService 无法将“System.Int32”类型的 object 转换为 ty...neric.IDictionary`2[System.String,System.Object]' - Asp.net WebService Cannot convert object of type 'System.Int32' to ty…neric.IDictionary`2[System.String,System.Object]' 无法将System.String转换为类类型 - Cannot convert System.String to class type LINQ to Entities无法识别方法&#39;System.Object Parse(System.Type,System.String)&#39; - LINQ to Entities does not recognize the method 'System.Object Parse(System.Type, System.String)' 多维数组转换运行时错误---&gt;无法将类型为&#39;System.Object [,]&#39;的对象转换为&#39;System.String类型 - Multidimensional Array Casting Runtime Error ---> unable to cast object of type 'System.Object[,]' to type 'System.String 无法将类型为&#39;System.Object []&#39;的对象强制转换为&#39;System.String []&#39; - Unable to cast object of type 'System.Object[]' to type 'System.String[]' 反序列化对象时,无法将类型为System.String的对象转换为类型为System.Byte [] - Cannot convert object of type System.String to type System.Byte[] when deserializing object System.Object[] 不能转换为 System.String[] - System.Object[] can't be converted to System.String[] JSON.NET将System.Object属性的类型自动更改为System.String - JSON.NET changes type of a System.Object property automatically to System.String 无法将类型为“ System.String”的对象转换为类型为“ System.Collections.Generic.List`1” - Cannot convert object of type 'System.String' to type 'System.Collections.Generic.List`1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM