【问题标题】:Cannot call an abstract base member无法调用抽象基成员
【发布时间】:2014-01-20 16:26:57
【问题描述】:

我正在尝试为 Spread.NET 创建自定义单元格类型。我得到的错误是

不能调用抽象基成员: 'FarPoint.Web.Spread.BaseCellType.PaintCell(字符串, System.Web.UI.WebControls.TableCell,FarPoint.Web.Spread.Appearance, FarPoint.Web.Spread.Inset, object, bool)'

这是代码

[Serializable()]
public class BarcodeCellType : FarPoint.Web.Spread.BaseCellType
{
    public override Control PaintCell(string id, TableCell parent, Appearance style, Inset margin, object value, bool upperLevel)
    {
        parent.Attributes.Add("FpCellType", "BarcodeCellType");

        if (value != null)
        {
            try
            {
                MemoryStream ms = GenerateBarCode(value.ToString());
                var img = Bitmap.FromStream(ms);
                value = img;
            }
            catch (Exception ex)
            {
                value = ex.ToString();
            }
        }

        return base.PaintCell(id, parent, style, margin, value, upperLevel); //ERROR HERE
    }

    private MemoryStream GenerateBarCode(string codeInfo)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            BarCodeBuilder bb = new BarCodeBuilder();
            bb.CodeText = codeInfo;
            bb.SymbologyType = Symbology.Code128;
            bb.BarCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms;
        }
    }
}

【问题讨论】:

  • 基本方法是虚拟的并且与您的覆盖具有相同的签名吗?
  • 请注意,您有内存泄漏。您的 MemoryStream 和 Bitmap 实例应在完成后调用 Dispose() 或将它们包装在“使用”块中进行清理
  • 嗨 mjmarsh - 我更新了我的代码..你的意思是做这样的事情吗?

标签: c# asp.net spread


【解决方案1】:

这是因为在您的抽象类“FarPoint.Web.Spread.BaseCellType”中,您可能将 PaintCell 方法定义为抽象方法,并且抽象方法声明引入了一个新的虚拟方法,但没有提供该方法的实现。相反,非抽象派生类(“BarcodeCellType”)需要通过覆盖该方法来提供它们自己的实现。因为抽象方法没有提供实际的实现。

【讨论】:

    【解决方案2】:

    PaintCell 被声明为 abstract 而不是 virtual 所以你不能调用base.PaintCell。由您的代码创建 Control 对象并返回它。

    如果您不想创建Control,您可能希望从比BaseCellType 更派生的类继承并覆盖该派生类PaintCell 方法。

    【讨论】:

      【解决方案3】:

      基本成员是抽象的,这意味着没有实现。删除对base.PaintCell 的调用将允许代码编译,但我不确定是否会得到您必须工作的代码。

      【讨论】:

      • 它不会编译,因为他依赖于创建 Control 的方法,这将从他的覆盖中返回,我认为他需要做很多工作才能得到它工作。
      【解决方案4】:

      An abstract method doesn't come with an implementation;子类必须提供一个实现,这正是您正在做的。

      只需省略调用即可。

      【讨论】:

        【解决方案5】:

        你不能调用抽象方法。需要在派生类中定义。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-07-30
          • 1970-01-01
          • 2017-06-19
          • 2013-06-29
          • 1970-01-01
          • 2019-12-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多