【问题标题】:Cast an object with Type T to Type T<System.Guid>将具有类型 T 的对象转换为类型 T<System.Guid>
【发布时间】:2009-05-29 12:11:37
【问题描述】:

我继承了一个带有 Windows 移动部件的项目。长话短说,我的问题是:

[DBPropertyUpdate("CustomerId")]
[DBPropertyRetrieve("CustomerId")]
public CustomerBase<T> Customer
{
    get { return _customer; }
    set { _customer = SetProperty(_customer, value); }
}

抛出异常。

在监视窗口中,我有以下内容:

> NAME         VALUE                           TYPE

_customer   {Pss.Common.Mia.Customer}   Pss.Common.Mia.CustomerBase<System.Guid> {Pss.Common.Mia.Customer}
(Pss.Common.Mia.CustomerBase<System.Guid>)_customer Cannot convert type 'Pss.Common.Mia.CustomerBase<T>' to 'Pss.Common.Mia.CustomerBase<System.Guid>'  

我不熟悉这段代码,但希望有一些简单的方法可以转换'Pss.Common.Mia.CustomerBase&lt;T&gt;' to 'Pss.Common.Mia.CustomerBase&lt;System.Guid&gt;' 第二个 Watch 条目是我的尝试,如您所见,它失败了。

【问题讨论】:

  • _customer 定义为什么?我认为我们没有足够的信息来给你一个完整的答案......
  • _customer = {Pss.Common.Mia.Customer}

标签: c# properties dynamic-typing


【解决方案1】:

类型为 CustomerBase 的变量 _customer 不可能转换为 CustomerBase,因为 T 未知。您还必须将 _customer 键入为 CustomerBase 才能正常工作。

【讨论】:

    【解决方案2】:

    通过将 CustomerBase&lt;Guid&gt; 作为类型传递给构建客户对象的函数来使其工作

    【讨论】:

      猜你喜欢
      • 2011-05-06
      • 2010-10-19
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2013-08-29
      • 1970-01-01
      相关资源
      最近更新 更多