【发布时间】:2021-02-03 15:30:19
【问题描述】:
我是 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();
}
}
}
为什么没有看到执行对象?
【问题讨论】:
-
呃,因为它在嵌套类中?你首先需要一个
MacroInfo,然后你可以调用mi.ExecuteObject...或者声明函数static然后你可以说MacroInfo.ExecuteObject当你覆盖它时`= new List()`有什么意义在接下来的林中 -
我显然是新手。我认为 excuteobject 是我班级中的一种方法。在我缺乏知识的情况下,我尝试让每件作品都有自己的类别,将每件作品放在主要部分,我显然不了解基本......
-
没关系。看起来你需要把它设为
static,不知道为什么它是通用的,因为你没有提供代码,它可以创建MacroInfos 并返回列表。如果它没有做任何与MacroInfo相关的事情,它就不应该在那个类中。 -
你能得到的最好的帮助是学习和理解你显然需要的 OOP 的基础知识。你可以在网上找到大量的资源。这只是其中之一c-sharpcorner.com/UploadFile/mkagrahari/…
标签: c# ienumerable instantiation