【问题标题】:Cast error between Location and SelectedCustomerLocationLocation 和 SelectedCustomerLocation 之间的转换错误
【发布时间】:2018-08-10 05:06:11
【问题描述】:

我们有一个自定义项,我们在其中针对客户位置运行处理屏幕。当我们尝试更新位置缓存中的位置时,我们收到以下错误:

Unable to cast object of type 
'PX.Data.PXCache`1[PX.Objects.AR.SelectedCustomerLocation]' to type 
'PX.Data.PXCache`1[PX.Objects.CR.Location]'.

这是我们正在使用的代码的简化版本。 ProcessLocation 方法是 PXProcessing 委托:

public class LocationMaint_Extension : PXGraphExtension<LocationMaint>
{
    public virtual void ProcessLocation(Location loc)
    {
        LocationExt locExt = loc.GetExtension<LocationExt>();

        locExt.UsrCustomField = "New Value";
        Base.Caches<Location>().Update(loc);
    }
}

我们知道 SelectedCustomerLocation 派生自 SelectedLocation,而 SelectedLocation 派生自 Location。因此,我们希望上述代码能够成功运行,因为存在继承。

如果我们写如下代码,方法成功,屏幕更新成功。但是,我们对自定义字段所做的更改并没有持久化到数据库中:

public class LocationMaint_Extension : PXGraphExtension<LocationMaint>
{
    public virtual void ProcessLocation(Location loc)
    {
        LocationExt locExt = loc.GetExtension<LocationExt>();

        locExt.UsrCustomField = "New Value";
        Base.Caches<SelectedCustomerLocation>().Update(loc as SelectedCustomerLocation);
        // We had this code in as well, which did not seem to help
        Base.Caches<SelectedCustomerLocation>().Persist(PXDBOperation.Insert);
        Base.Caches<SelectedCustomerLocation>().Persist(PXDBOperation.Update);
    }
}

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: c# acumatica


    【解决方案1】:

    在 C#(通常是 .NET)中,您不能将 G&lt;Derived&gt; 类型的对象强制转换为 G&lt;Base&gt;,其中 G 是泛型类型。这有时是一种痛苦。过去我已经制定了自定义方法来执行此操作,例如 List&lt;T&gt; 您可能会使用这样的方法:

    static List<TBase> Upcast<TBase, TDerived>(this List<TDerived> list) 
        where TDerived : TBase 
    {
        var result = new List<TBase>();
        foreach (var d in list)
        {
            result.Add(d);
        }
        return result;
    
    }
    

    【讨论】:

    • 嗨 JamesFaix,感谢您发布答案。有问题的类型实际上不是泛型类型,它们是LocationSelectedCustomerLocation。我们已经尝试为Update() 方法创建一个新的Location 对象,但它仍在寻找SelectedCustomerLocation。这似乎是 Acumatica 特有的问题,他们正在改变缓存中的类型。
    • 错误是在代码中使用PXCache&lt;T&gt; 与这两个作为通用参数。我不确定PXCache&lt;T&gt; 是什么。
    猜你喜欢
    • 1970-01-01
    • 2013-09-01
    • 2013-03-10
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多