不能隐式地将类型'object'转换为'system.guid'?存在显式转换(您是否错过了演员?)错误 [英] cannot implicitly convert type 'object' to 'system.guid'? An Explicit conversion exists (are you missing a cast?) error

查看:563
本文介绍了不能隐式地将类型'object'转换为'system.guid'?存在显式转换(您是否错过了演员?)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器

您好我无法将类型对象隐式转换为system.guid?此行中存在显式转换(您是否错过了演员?)错误 taxinfotaxfiled.TaxFieldID = i.Value; 特别是在i中我给出了值(i.Value)但仍然显示相同错误。请任何人建议我的解决方案?



My Controller
Hi I got cannot implicitly convert type object to system.guid? An Explicit conversion exists (are you missing a cast?) error in this line taxinfotaxfiled.TaxFieldID = i.Value; Especially in "i" I gave "Value" (i.Value) but still showing same error. please any one suggest me the solution?

public ActionResult Create(TaxInfoTaxFiled taxinfotaxfiled)
        {
           ArrayList Alist = new ArrayList();
            {
                Alist.Add("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4");
                Alist.Add("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4");
                Alist.Add("64B512E7-46AE-4989-A049-A446118099C4");
                Alist.Add("376D45C8-659D-4ACE-B249-CFBF4F231915");
                Alist.Add("59A2449A-C5C6-45B5-AA00-F535D83AD48B");
                Alist.Add("03ADA903-D09A-4F53-8B67-7347A08EDAB1");
                Alist.Add("2F405521-06A0-427C-B9A3-56B8931CFC57");
            }

            if (ModelState.IsValid)
            {
                taxinfotaxfiled.TaxInfoTaxFieldID = Guid.NewGuid();

                foreach (var i in Alist)
                {
                    taxinfotaxfiled.TaxFieldID = i.Value;
                   }

                db.TaxInfoTaxFileds.Add(taxinfotaxfiled);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

return View(taxinfotaxfiled);
        }







我的型号






My Model

public partial class TaxInfoTaxFiled
    {
        public System.Guid TaxInfoTaxFieldID { get; set; }
        public Nullable<System.Guid> TaxInfoID { get; set; }
        public Nullable<System.Guid> TaxFieldID { get; set; }
        public Nullable<System.Guid> FieldTypeID { get; set; }
        public string FieldValue { get; set; }
}
  public partial class TaxField
    {
        public System.Guid TaxFieldID { get; set; }
        public string DisplayName { get; set; }
        public string PrintName { get; set; }
}

推荐答案

为什么还要烦恼ArrayList?它已经很老了,几乎所有使用它的代码都可以使用List< t>。取而代之的是


你得到错误的原因是因为ArrayList总是返回一个Object,不一定是你输入的类型。在你的情况下,一个字符串看起来像一个GUID。将ListList替换为List< guid>你将解决问题。

Why are you even bothering with ArrayList? It's pretty old and just about every piece of code that uses it can use a List<t> in its place.

The reason you get the error is because ArrayList always returns an Object, not necessarily the type you put in. In your case, a string that looks like a GUID. Replace the ArrayList with a List<guid> and you'll solve the problem.
List<guid> AList = new List<guid>();
AList.Add(new Guid("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4"));
AList.Add(new Guid("64B512E7-46AE-4989-A049-A446118099C4"));
...</guid></guid>


为了回答这个问题,arraylist可以包含任何内容,以便您添加字符串(字符串可以表示为GUID,但这是无关紧要的,该字符串可以包含Hello world),它们存储为对象引用。要获得原始类型,您需要将arraylist中的对象转换为添加时的类型,因此您需要将其转换为字符串。一旦返回为字符串,您需要使用Guid.Parse(或TryParse)从字符串中的数据创建GUID。所以你的代码应该是这样的;



To answer the question, arraylist can contain anything so you are adding strings (the string could be represented as a GUID, but that's irrelevant, the string could contain "Hello world") which are stored as object references. To get the original type back you need to cast the object in the arraylist to whatever type it was when added, so you would need to cast it to a string. Once back as a string you need to use Guid.Parse (or TryParse) to create a GUID from the data that is in the string. So your code should be like this;

foreach (var i in Alist)
{
    taxinfotaxfiled.TaxFieldID = Guid.Parse((string)i);
}





另一种方法是将GUID存储在ArrayList中,这样您就不需要费心解析了;





An alternative would be to store GUIDs in the ArrayList so you don't need to bother parsing;

ArrayList Alist = new ArrayList();
{
    Alist.Add(new Guid("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4"));
    Alist.Add(new Guid("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4"));
    // ....
}

foreach (var i in Alist)
{
    taxinfotaxfiled.TaxFieldID = (Guid)i;
}





正如其他解决方案所暗示的那样,使用List< Guid>等类型化List对象要好得多。因为ArrayList在仿制药出现之前已经很久了,让我们做一些强类型的东西。



下一个问题是你循环遍历arraylist中的每个项目并使用每个项目覆盖TaxFieldID属性,因此总体效果是TaxFieldID将成为列表中的最后一个值,因此可能不是您期望的行为。



As the other solution suggests, though, it is far better to use typed List objects like List<Guid> as ArrayList is old from before generics came along and let us do this stuff strongly-typed.

The next issue is that you're looping through each item in the arraylist and overwriting the TaxFieldID property with each item, so the overall effect is that the TaxFieldID is going to be the last value in the list, so that probably isn't the behaviour you are expecting.


这篇关于不能隐式地将类型'object'转换为'system.guid'?存在显式转换(您是否错过了演员?)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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