【问题标题】:Access to WriteOnly Property访问 WriteOnly 属性
【发布时间】:2013-07-11 09:12:41
【问题描述】:

我有一个用 VB.net 编写的基类。这个基类有一个属性。我有一个从该 vb 类驱动的 C# 类。我需要在这里设置属性,所以我可以给它一个值。 这是我在 vb.net 类中的属性:

  Public MustInherit Class FTPDownloaderBase

   Public WriteOnly Property FilesToDownload() As ArrayList
    Set(ByVal Value As ArrayList)
        _filesToDownload = Value
    End Set
  End Property

这是我的 C# 类:

 public class FTPDownloaderClass:FTPDownloader.FTPDownloaderBase
{



  public Array FilesToDownloadC
  {
      get
      {
          return base.FilesToDownload;
      }


  }

}

但是我得到这个错误就行了:return base.FilesToDownload; 属性或索引器 'FTPDownloded.FtpDownloderbase.FilesToDownload' 不能在此上下文中使用,因为它缺少 get 访问器。

如何在我的 C# 代码中访问该属性并为其赋值?

解决方案: 我在 VB 基类中添加了一个 Get,现在可以像这样访问它:

  Public Property FilesToDownload() As ArrayList
    Get
        Return _filesToDownload
    End Get
    Set(ByVal Value As ArrayList)
        _filesToDownload = Value
    End Set
End Property

【问题讨论】:

    标签: c# properties writeonly


    【解决方案1】:

    您必须修改基类并添加一个 get 访问器,或者添加一个方法来返回该属性。

    【讨论】:

      【解决方案2】:

      如果属性没有get 访问器,则您无法访问该属性,除非您使用反射。

      最简单的解决方案是将 Get 访问器添加到 VB 类,或将 _filesToDownload 设为受保护而不是私有。

      如果不能更改基类:

      return typeof(FTPDownloader.FTPDownloaderBase).GetFields(BindingFlags.NonPublic).Single(f => f.Name == "_filesToDownload").GetValue(this);
      

      【讨论】:

      • 感谢 charpler,我认为我无法将 get 添加到基类中,我已添加 Public Property FilesToDownload() As ArrayList Get Return _filesToDownload End Get Set(ByVal Value As ArrayList) _filesToDownload = Value结束设置结束属性,它可以工作,谢谢:)
      猜你喜欢
      • 1970-01-01
      • 2010-09-24
      • 2014-12-04
      • 2013-03-04
      • 2015-11-03
      • 2017-03-16
      • 2013-12-23
      • 2011-07-12
      • 1970-01-01
      相关资源
      最近更新 更多