c#在属性上实现接口

时间:2015-10-28 17:53:59

标签: c# vb.net interface implements

我正在使用一些VB代码。我在c#

中重写原始VB代码中的一些东西

在我的VB类中:

    Implements actionBaseClass

    Private m_config_url As String
    Private m_config_unc As String
    Private m_connection_string As String
    Private m_xml_doc_path_or_data As String
    Private m_fpid As String
    Private m_cancelled As Boolean
    Private m_xml_doc_element As Xml.XmlElement
    Private m_removing As Boolean
    Private m_xml_doc As New Xml.XmlDocument


    Property XMLDocPathOrData() As String Implements actionBaseClass.XMLDocPathOrData
        Get
            XMLDocPathOrData = m_xml_doc_path_or_data
        End Get
        Set(ByVal Value As String)
            m_xml_doc_path_or_data = Value
        End Set
    End Property

    WriteOnly Property FPId() As String Implements actionBaseClass.FPId
        Set(ByVal Value As String)
            m_fpid = Value
        End Set
    End Property

我转换的C#界面:

interface actionBaseClass
{

    public bool UseStringData { get; }
    string XMLDocPathOrData { get; set; }
    string FPId { set; }
    string ConfigURL { set; }
    string ConfigUNC { set; }
    string ConnectionString { set; }
    bool Cancelled { get; }
    void ActionMe();
    void DisposeMe();

}

到目前为止我转换的C#类:

 public partial class Form1 : Form, actionBaseClass
    {


        #region Public Implements from base class

        private string m_config_url, m_config_unc, m_connection_string, m_xml_doc_path_or_data, m_fpid;
        private bool m_cancelled, m_removing;
        private XmlElement m_xml_doc_element;
        private new XmlDocument m_xml_doc;

        public bool UseStringData
        {
            get { return false; }
        }


        public string XMLDocPathOrData

        {
            get { return actionInterface.XMLDocPathOrData = m_xml_doc_path_or_data; }
            set { m_xml_doc_path_or_data = value; }
        }

        public string FPId
        {
            set { m_fpid = value; }
        }

如何在C#中使用上述VB代码中的属性声明中的Implements?或者在使用Implements <interface_name>

之后不必使用C#

2 个答案:

答案 0 :(得分:2)

对于从接口实现某些内容的每个属性和方法,您不必在C#中指定Implements关键字。只要签名匹配,编译器就知道该属性或方法是来自接口的实现。

您的代码还有两个注释:

  1. 惯例是接口名称以大写I开头,如IAction
  2. 将PascalCase用于类和接口名称
  3. 如果它是一个界面,那就没有意义,它被称为“BaseClass”
  4. 您不必在接口中的属性和方法中指定public(请参阅UseStringData)

答案 1 :(得分:1)

如果您已经在实现了与您的类的接口,那么具有相同签名的属性将自动隐式实现该接口。如果您想显式实现界面,您也可以这样做。

GetUserID()