【问题标题】:implementing a c# interface [closed]实现一个c#接口[关闭]
【发布时间】:2014-01-18 16:38:17
【问题描述】:

我有一个名为 myResult 的数据表,它有两列 Process 和 ParentProcess,我正在尝试将其放入 Ienumerable 类型,以便我可以使用 Json.net 对其进行序列化。

Original post is here:

现在我到这一步了,我有这个界面:

namespace myFirstCProgramIn2013
    {
        public interface Interface1 : IEnumerable
        {
            string Process { get; set; }
            string ParentProcess { get; set; }
        }
    }

我已经在这个类中实现了:

public class myArray : Interface1
{

    public string Process
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public string ParentProcess
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public System.Collections.IEnumerator GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

然后我尝试使用这个类来使用下面的代码将 myResult 分配给它:

   List<Interface1> recordList = new List<Interface1>();
   if (myResult.Rows.Count > 0)
   {
       foreach (DataRow row in myResult.Rows)
       {
           var myArray = new myArray();
           myArray.Process = Convert.ToString(row["Process"]);
           myArray.ParentProcess = Convert.ToString(row["ParentProcess"]);

       }
   }

我收到此错误:

“System.NotImplementedException”类型的异常发生在 App_Web_hi4zxnmq.dll 但未在用户代码中处理

在 myArray 类的 Process 属性的 set 子句中抛出。

我在哪里做错了? 谢谢。

【问题讨论】:

  • 你在开玩笑吗?你的代码明显抛出了这个异常
  • 您到处都有throw new NotImplementedException(),因此收到该错误应该不会让您感到惊讶。您是否尝试过在调试模式下运行代码?
  • 您的代码完全按照您的指示行事。你不明白什么?
  • 您的代码完全按照您的指示行事。你不明白什么? - 很可能我想在这里学习!异常是在我实现接口时由 vs13 自动添加的。

标签: c# interface datatable


【解决方案1】:

您在属性的设置器上抛出 NotInprementedException。如果你想要自动属性替换

    get
    {
        throw new NotImplementedException();
    }
    set
    {
        throw new NotImplementedException();
    }

    get; set;

【讨论】:

  • 谢谢 - 因为例外是自动添加的,我认为它们是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-08
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 2012-12-15
  • 2018-07-03
相关资源
最近更新 更多