Tag: c#

C#中的动态类创建

是否有可能在运行时从DataTable创建一个类,其中ColumnName将是动态类属性?

如果打开关闭表格,如何检查表格是否打开?

如何检查表单是否已打开,以及表单是否打开以关闭表单? 我尝试了下面的代码,测试了一些代码,但它仍然说即使我知道这个代码是不正常的: foreach(Form a in Application.OpenForms) { if (a is YouLikeHits_Settings) { // About form is open MessageBox.Show(“form open”); break; } // About form is not open… MessageBox.Show(“form not open”); break; }

当提供的字符串不为null时,string.IsNullOrEmpty返回true

我有一个unit testing,调用传递字符串的对象上的方法。 被调用方法的第一件事就是检查字符串是否为null或为空。 但是,无论filePath的值是什么,对string.IsNullOrEmpty的调用都为true。 见下图。 我在这里错过了什么吗? 编辑: 检查null和string.Empty分别按预期工作: 编辑2: 我已经清理了解决方案,通过文件系统删除了bin目录,并且在重建之后,调试器显示应该抛出ArgumentNullException,尽管它实际上没有被抛出 。

C# – FtpWebRequest – 通过同一连接/登录的多个请求

我想循环一个FTP文件夹,以检查文件是否已到达 我做: FtpWebRequest request = (FtpWebRequest)WebRequest.Create(“ftp://localhost:8080”); request.Credentials = new NetworkCredential(“anonymous”, “”); request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; while(true) { using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) using (Stream responseStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream)) { Console.WriteLine(reader.ReadToEnd()); reader.Close(); response.Close(); } } 但是在第二次迭代中我得到一个例外: 无法读取流

通用约束忽略协方差

假设我们有一个类似的界面 public interface IEnumerable { /*…*/ } 这是T 共变体 。 然后我们有另一个接口和一个实现它的类: public interface ISomeInterface {} public class SomeClass : ISomeInterface {} 现在,协方差允许我们执行以下操作 IEnumerable e = Enumerable.Empty(); 因此, IEnumerable 可分配给IEnumerable类型的变量(或方法参数)。 但是如果我们在通用方法中尝试这个: public void GenericMethod(IEnumerable p) where T : ISomeInterface { IEnumerable e = p; // or TestMethod(p); } public void TestMethod(IEnumerable x) {} 我们得到编译器错误CS0266告诉我们IEnumerable无法转换为IEnumerable 。 约束清楚地表明T是从ISomeInterface派生的,并且由于IEnumerable在T是共变量,因此该赋值应该起作用(如上所示)。 […]

在richtextbox中更改文本字体

我的文本位于richtextbox中: First text of parag1. Second text of parag1. First text of parag2. Second text of parag2. First text of parag3. Second text of parag3. First text of parag4. Second text of parag4. 我想要更改文本的颜色字体和文本颜色: 1-对于标签 – > font name = Tahoma,size = 10,color = red 示例: 或 2-对于标签之间的文本,标签的级别不是页脚 – >字体名称= Arial,大小= 12,颜色=黑色 示例: First text […]

如何以编程方式安全地关闭Google Chrome

如何通过C#安全地关闭谷歌浏览器? 我可以杀死Chrome进程,但在这种情况下,Google Chrome会在下次运行时报告appcrash。

为什么List 不是IEnumerable ?

[编辑:我的道歉……最初的问题措辞含糊不清,我没有得到我正在寻找的回复] 对于从类Yinheritance的任何类X, new List() is IEnumerable是真的。 但是,这不适用于结构: new List() is IEnumerable是false。 我的问题是:为什么? 这是一个示例程序: class Program { class Y { } class X : Y { } struct Z { } static void Main(string[] args) { Test(new List()); Test(new List()); Test(new List()); Test(new List()); Test(“blah”); Test(1); Console.ReadLine(); } static void Test(object o) { if (o is […]

从Web服务器加载html页面时,HtmlAgilityPACK显示错误“不支持给定路径的格式”

我正在使用我的本地Apache服务器,其地址是127.0.0.1。 我尝试使用HTML Agility PACk从该服务器加载html页面到C#程序,但它的显示 错误:不支持给定路径的格式。 HtmlAgilityPack.HtmlDocument docHtml = new HtmlAgilityPack.HtmlDocument(); docHtml.Load(@”htttp://127.0.0.1/2.htm”); // <— error pointer showing here foreach(HtmlNode link in docHtml.DocumentNode.SelectNodes("//a[@href]")) { link.Attributes.Append("class","personal_info"); } docHtml.Save("testHTML.html"); } 非常感谢@Slaks在你提出建议之后我改变了我的COED并且工作正常 HtmlAgilityPack.HtmlDocument docHtml = new HtmlAgilityPack.HtmlDocument(); HtmlAgilityPack.HtmlWeb docHFile = new HtmlWeb(); docHtml = docHFile.Load(“http://127.0.0.1/2.html”);

为什么viewbag值没有传回视图?

直截了当的问题,似乎无法让我的viewBag值显示在完成表单后用户指向的视图中。 请指教..谢谢 我的索引ActionResult简单返回模型数据.. public ActionResult Index() { var source = _repository.GetByUserID(_applicationUser.ID); var model = new RefModel { test1 = source.test1, }; return View(model); } 我的获取编辑“ActionResult,只使用与索引相同的模型数据。 我的post“编辑”ActionResult,将新值分配给模型并重定向到索引页面,但索引页面不显示ViewBag值? [HttpPost] public ActionResult Edit(RefModell model) { if (ModelState.IsValid) { var source = _repository.GetByUserID(_applicationUser.ID); if (source == null) return View(model); source.test1 = model.test1; _uow.SaveChanges(); @ViewBag.Message = “Profile Updated Successfully”; return […]