小编pra*_*rab的帖子

仅包含Serializable对象的字典

我正在寻找一种方法来确保只有可序列化的对象存储在C#中的Dictionary中.

更具体地说,我希望做类似的事情:

Dictionary<String, ISerializable> serialDict = new Dictionary<String, ISerializable>();
Run Code Online (Sandbox Code Playgroud)

这个问题是我不能存储整数类型,如整数,布尔值或字符串.

有没有办法确保我的词典只包含可以序列化的对象?

c# generics serialization dictionary

6
推荐指数
1
解决办法
568
查看次数

二进制搜索算法出现错误 - 使用未分配的局部变量

我正在学习如何从头开始创建二进制搜索算法的教程.但是我收到错误"使用未分配的局部变量'Pivot'".我是这门语言的新手,以前只尝试过更简单的语言.

我为缺乏内部文档和使用空白区域而道歉.

错误是在使用"//"标记的代码底部附近

这是程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Binary_Search_2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[10];

            Random rnd = new Random();

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(1, 10);
            }

            Array.Sort(arr);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write("{0},", arr[i]);
            }
            int Start = 0;
            int End = arr.Length;
            int Center = Start + End / 2;

            int Pivot; …
Run Code Online (Sandbox Code Playgroud)

c# algorithm variables unsigned

6
推荐指数
1
解决办法
984
查看次数

System.Net.Mail.SmtpException:系统存储不足.服务器响应如下:4.3.1系统资源不足

我最近在C#中设计了一个程序,它将从SQL数据库中提取信息,编写带有结果的HTML页面,并自动发送电子邮件.我一切都在工作[偶尔],我遇到的问题是我似乎正在崩溃我们公司的交换服务器.在程序的前几次成功运行之后,我将开始获得此异常:

基本异常:System.Net.Mail.SmtpException:系统存储不足.服务器响应如下:4.3.1系统资源不足

我想知道我是否应该在我的邮件中调用某种类似Dispose()的方法?或者,如果有任何其他明显的原因导致邮件系统停止响应?这会影响我们公司的所有客户,而不仅仅是我的代码.

这是Exchange 2010,我的代码是针对.NET 3.5编译的.我的附件通常是27kb.如果我登录到Exchange服务器,似乎消息只是无限期地挂在队列中.清除队列(删除而不发送NDR)并重新启动服务器将重新启动它.

邮件部分如下所示(用户名,密码和地址已更改):

public void doFinalEmail()
{
     List<string> distList = new List<string>();
     string distListPath = Environment.CurrentDirectory + "\\DistList.txt";
     string aLine;

     logThat("Attempting email distribution of the generated report.");

     if (File.Exists(distListPath))
     {
         FileInfo distFile = new FileInfo(distListPath);
         StreamReader distReader = distFile.OpenText();

         while (!String.IsNullOrEmpty(aLine = distReader.ReadLine()))
         {
             distList.Add(aLine);
         }
     }
     else
     {
         logThat("[[ERROR]]: Distribution List DOES NOT EXIST! Path: " + distListPath);
     }

     MailMessage msg = new MailMessage();
     MailAddress fromAddress = new MailAddress("emailaddresshere");
     msg.From = fromAddress;

     logThat("Recipients: ");
     foreach …
Run Code Online (Sandbox Code Playgroud)

c# exchange-server system.net.mail smtpexception

6
推荐指数
1
解决办法
2万
查看次数

LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)'方法

我试图将一个应用程序转换为EntityFrameWork codefirst.我现在的代码是

 string sFrom  ="26/12/2013";
 select * FROM Trans where  CONVERT(datetime, Date, 105) >=   CONVERT(datetime,'" + sFrom + "',105) 
Run Code Online (Sandbox Code Playgroud)

我试过了

 DateTime dtFrom = Convert.ToDateTime(sFrom );
TransRepository.Entities.Where(x  =>Convert.ToDateTime(x.Date) >= dtFrom) 
Run Code Online (Sandbox Code Playgroud)

但是我得到了这样的错误

LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)'方法

请提前帮助,谢谢

c# linq-to-entities entity-framework

6
推荐指数
1
解决办法
3万
查看次数

System.FormatException:将字符串转换为十进制时,输入字符串的格式不正确.

我对ASP.NET和C#有一点问题.这是我的错误代码:

mscorlib.dll中出现"System.FormatException"类型的异常,但未在>用户代码中处理

附加信息:输入字符串的格式不正确.

protected void Page_Load(object sender, EventArgs e)
{
    if(this.IsPostBack == false)
    {
        Currency.Items.Add(new ListItem("Euro", "0.85"));
        Currency.Items.Add(new ListItem("Yen", "11.30"));
        Currency.Items.Add(new ListItem("PLN", "4.20"));
        Currency.Items.Add(new ListItem("GBP", "5.62"));
    }
}

protected void Convert_Click(object sender, EventArgs e)
{
    decimal oldAmount;
    bool succes = Decimal.TryParse(TextBox.Value, out oldAmount);

    if(succes)
    {
        ListItem item = Currency.Items[Currency.SelectedIndex];
        decimal newAmount = oldAmount * decimal.Parse(item.Value);
        Result.InnerText = "Result: " + newAmount;
    }

}
Run Code Online (Sandbox Code Playgroud)

我试过Decimal.Parse,Decimal.TryParse和其他奇怪的组合.现在我确定问题是字符串并将它们解析为十进制.当我创建String变量时 - 解析时出现了同样的错误.那么有人可以告诉我该怎么做才能将String转换为十进制?

c# asp.net string decimal formatexception

6
推荐指数
1
解决办法
7万
查看次数

节流应用程序的CPU使用率

我们公司想出租一款新的游戏服务器.然而,游戏开发者没有创建任何类型的休眠模式来关闭没有玩家连接的物理,所以一个空的服务器正在吃30%左右的CPU.

我发现这个游戏面板插件限制了应用程序的CPU使用率.

我在C#.NET中为我们公司编写了一些小应用程序,以帮助改进我们的服务,我想知道如何创建这样的东西.可能吗?

.net c# cpu throttling

5
推荐指数
1
解决办法
2335
查看次数

按列名字符串对列表进行排序

假设我有一个这样的课程

public class model
{
    public int id{get;set;}
    public string name{get;set;}
    public string department{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

我有一个类型模型列表

List<model> modelList = List<model>();
Run Code Online (Sandbox Code Playgroud)

如何使用排序方向对列名进行排序?

我试过的方法:

public List<model> sortModelList(string columnName, SortDirection direction)
{
   //Method 1:
   //The below code was unable to sort by column and unable to set the sort direction
   return modelList.Sort();

   //Method 2:
   //The below code was unable to sort by the columnName parameter and unable to set the sort direction
   return modelList.OrderBy(a=>a.name)

  //What I can do in order to sort the …
Run Code Online (Sandbox Code Playgroud)

c# sorting list

5
推荐指数
2
解决办法
8831
查看次数

如何获取收件箱文件夹实例?

如果我想知道收件箱中有多少项,我使用TotalCount属性,但如何首先获取收件箱文件夹?如果我使用FindFoldersResults findFolders = service.FindFolders()方法,收件箱的父文件夹名称是什么?谢谢.

c# asp.net exchangewebservices

4
推荐指数
1
解决办法
1720
查看次数

不保持循环的while循环

对于初学者练习,我试图创建一个简单的循环,它接受来自用户的单个字符,将该字符打印到控制台并继续这样做,直到用户输入“R”。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            char cplayerSelection = 'R';

            while(cplayerSelection == 'R')
            {
                Console.WriteLine("Enter R, P, or S:");
            cplayerSelection = (char)Console.Read();
            Console.WriteLine(cplayerSelection);
            }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

无论用户输入什么,它只会循环一次结束然后退出。我需要更改什么才能继续循环?

c# while-loop

3
推荐指数
1
解决办法
317
查看次数

面板背景图像闪烁

我有一个填写父表格的小组.
我使用Timer来捕获屏幕,
并将屏幕截图设置为Panel的背景图像.

然而,它会遇到疯狂的闪烁.我该怎么做才能解决它?

//Part of code
 public partial class Form1 : Form
    {

        DxScreenCapture sc = new DxScreenCapture();

        public Form1()
        {
            InitializeComponent();

            panelMain.BackgroundImageLayout = ImageLayout.Zoom;
         }

        private void Form1_Load(object sender, EventArgs e)
        {
        }



        void RefreshScreen()
        {
            Surface s = sc.CaptureScreen();

            DataStream ds = Surface.ToStream(s, ImageFileFormat.Bmp);
            panelMain.BackgroundImage = Image.FromStream(ds);

            s.Dispose();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            RefreshScreen();

        }
    }
Run Code Online (Sandbox Code Playgroud)

c# refresh panel flicker background-image

3
推荐指数
3
解决办法
7385
查看次数