为什么我的代码说我的对象没有定义?

时间:2021-02-03 15:30:19

标签: c# ienumerable instantiation

我是 C# 新手,无法弄清楚为什么我的类对象在当前内容中“不存在”。我尝试了多种方法来重新组织和调用我的对象,但仍然得到“当前上下文中不存在名称‘ExcuteObject’。

namespace DBTest4
{
    class Program
    {
        class MacroInfo
        {
            public int MsgSN { get; set; }
            public int FormID { get; set; }
            public int Leg { get; set; }
            public int Stop { get; set; }

            public MacroInfo(DataRow row)
            {
                this.MsgSN = Convert.ToInt32(row["MsgSN"]);
                this.FormID = Convert.ToInt32(row["FormID"]);
                this.Leg = Convert.ToInt32(row["Leg"]);
                this.Stop = Convert.ToInt32(row["Stop"]);
            }
            public DataTable Ch(string commandsqlstring, bool isStoredProcedure = false)
            {
                DataTable dataTable = new DataTable();
                ......
                        return dataTable;
            }
            public IEnumerable<T> ExcuteObject<T>(string storedProcedureorCommandText, bool isStoredProcedure = true)
            {
                List<T> items = new List<T>();
                      .....
                return items;
            }

        }
        static void Main(string[] args)
        {          
            string commandsqlstring = "Select top 10 MsgSN,FormID,Leg,Stop from tmail.MacroSendReceiveHistory order by ID desc";
            bool SP = false;
            List<MacroInfo> macroInfos = new List<MacroInfo>();
            macroInfos = ExcuteObject<MacroInfo>(commandsqlstring, SP).ToList();
            Console.ReadLine();
        }
    }
}

为什么看不到 Excute Object?

1 个答案:

答案 0 :(得分:0)

ExcuteObject 是类 MacroInfo 的方法而不是类 Program 的方法。如果 ExcuteObject 方法是静态的并且是“Program”类的方法,则您当前的实现会起作用。

为了解决这个问题。您可以执行以下任一操作:

  1. ExcuteObject 方法移至 Program 类并使其成为静态。
  2. ExcuteObject 保留在 MacroInfo 类中,但将 ExcuteObject 方法设为静态并在 Program 类中将其调用为 MacroInfo.ExcuteObject(...)
  3. 创建一个 MacroInfo 类的实例 并在 MacroInfo 类的实例上调用该方法。