Tag: .net

如何使用Mono和mkbundle将简单的.Net控制台项目转换为可移植的exe?

我想将我简单的纯.Net 2.0控制台实用程序转换为便携式exe文件,我可以将其放在USB记忆棒上运行,而不必担心CLR和框架库是否安装在特定的机器上。 Stackoverflow已经有一些关于使用Mono和Mkbundle从.Net-projects创建自包含Windows exes的信息,但我想要的是一个紧凑但有用的HOWTO。 从简单的C#Visual Studio项目实现可移植性的最小步骤是什么?

有没有更好的方法在C#中创建深度和浅层克隆?

我一直在为项目创建对象,有些实例我必须为这些对象创建一个深层副本我已经想到了使用C#的内置函数,即MemberwiseClone()。 困扰我的问题是每当我创建一个新类时,我都必须编写一个类似下面的代码的函数来进行浅拷贝。有人请帮助我改进这部分并给我一个更好的浅拷贝比第二行代码。 谢谢 :) SHALLOW COPY: public static RoomType CreateTwin(RoomType roomType) { return (roomType.MemberwiseClone() as RoomType); } 深度复制: public static T CreateDeepClone(T source) { if (!typeof(T).IsSerializable) { throw new ArgumentException(“The type must be serializable.”, “source”); } if (Object.ReferenceEquals(source, null)) { return default(T); } IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); using (stream) […]

.Net中的计时器和循环是否准确?

在开发一个程序来计算由555定时器IC产生的脉冲的频率和脉冲宽度时,通过PC并行端口进入PC。 我注意到每次运行代码时都显示不同的值,所以我开始测试循环和定时器的准确性。 我运行了以下代码,并指出它们不准确(我可能错了,请纠正我,如果我是!): 对于计时器: int sec = 0; private void button2_Click(object sender, EventArgs e) { sec = DateTime.Now.Second; i = 0; timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { if (sec == DateTime.Now.Second) { i++; } else { timer1.Enabled = false; MessageBox.Show(i.ToString(),”Timer Output”); } } 输出:应该相同,但是: 对于LOOP: private void button1_Click(object sender, EventArgs […]

Asmx web服务如何返回JSON而不是XML?

我的服务方式: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string getDataFromTrainingMaster() { List results = new DAL().GetDataFromTrainingMaster(); JavaScriptSerializer js = new JavaScriptSerializer(); return js.Serialize(results).ToString(); } 我的.net Web服务返回包含在XML中的JSON,如下所示: [{“Training_Code”:”1234 “,”Training_Duration”:”2hrs “,”Training_Startdate”:”2/14/2013 3:00:00 PM”,”Training_Enddate”:”2/14/2013 5:00:00 PM”,”Trainer_ID”:1,”Training_Location”:”B-Wing Training room-4″,”Comments”:”C# training”,”Keyword”:”C#1234″,”NumberofDays”:1},{“Training_Code”:”4321 “,”Training_Duration”:”16 “,”Training_Startdate”:”2/17/2013 10:30:00 AM”,”Training_Enddate”:”2/17/2013 5:30:00 PM”,”Trainer_ID”:2,”Training_Location”:”A-Wing Training Room-6″,”Comments”:”Objective-C”,”Keyword”:”Obj-C4321″,”NumberofDays”:2}] 我需要它采用以下格式: “Training”:[{“Training_Code”:”1234 “,”Training_Duration”:”2hrs “,”Training_Startdate”:”2/14/2013 3:00:00 PM”,”Training_Enddate”:”2/14/2013 5:00:00 PM”,”Trainer_ID”:1,”Training_Location”:”B-Wing Training room-4″,”Comments”:”C# training”,”Keyword”:”C#1234″,”NumberofDays”:1},{“Training_Code”:”4321 “,”Training_Duration”:”16 “,”Training_Startdate”:”2/17/2013 […]

如何抑制由我​​无法更改的代码显示的对话框?

我有来自第三方的Inproc COM服务器。 如果它捕获特定类型的错误,我调用的函数之一将显示错误消息对话框。 问题是我正在尝试批量处理数据,而我使用的数据源导致错误对话框弹出很多。 如果它产生1000个对话框,这不会是一个问题,而是它会阻塞,直到您按OK才会返回该function。 如何禁止显示对话框,或以编程方式按OK? 这是一个调用堆栈的副本,​​因为它正在等待我按OK [管理到原生过渡] > System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID,int reason,int pvLoopData)Line 2198 + 0x1e bytes C# System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason,System.Windows.Forms.ApplicationContext context)Line 3422 + 0x1b bytes C# System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason,System.Windows.Forms.ApplicationContext context)Line 3306 + 0xc bytes C# System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm)第1495行+ 0x31字节C# UniversalDataImporter.exe!UniversalDataImporter.Program.Main()第18行+ 0x1d字节C# [原生于管理过渡] [管理到原生过渡] mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile,System.Security.Policy.Evidence assemblySecurity,string [] args)2023行+ 0x18字节C# Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()+ 0x27 bytes mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态)第68行+ 0x27字节C# mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object […]

使用System.IO.Packaging生成ZIP文件

我知道通常建议使用DotNetZip或SharpZipLib库来创建.net语言的ZIP文件(在我的例子中是C#),但使用System.IO.Packaging生成ZIP文件并非不可能 。 我认为尝试在C#中开发一个可以执行此操作的例程可能会很好,而无需下载任何外部库。 有没有人有一个方法或方法的好例子,将使用System.IO.Packaging生成一个ZIP文件?

以编程方式获取所有可用语言(在卫星assembly中)

我正在使用.resx文件设计一个多语言应用程序。 我有一些文件,如GlobalStrings.resx,GlobalStrings.es.resx,GlobalStrings.en.resx等。当我想使用它时,我只需要设置Thread.CurrentThread.CurrentCulture。 问题:我有一个包含所有可用语言的combobox,但我手动加载: comboLanguage.Items.Add(CultureInfo.GetCultureInfo(“en”)); comboLanguage.Items.Add(CultureInfo.GetCultureInfo(“es”)); 我试过了 cmbLanguage.Items.AddRange(CultureInfo.GetCultures(CultureTypes.UserCustomCulture)); 没有任何成功。 还尝试了CultureTypes中的所有元素,但是我只获得了一个包含更多我不使用的语言的大列表,或者是一个空列表。 有没有办法只获得支持的语言?

代表们的优势是什么?

使用代表有什么好处/好处? 有人能提供任何简单的例子吗?

如何将包含可变大小数组的结构编组到C#?

我如何编组这个C ++类型? ABS_DATA结构用于将任意长的数据块与长度信息相关联。 Data数组的声明长度为1,但实际长度由Length成员给出。 typedef struct abs_data { ABS_DWORD Length; ABS_BYTE Data[ABS_VARLEN]; } ABS_DATA; 我尝试了以下代码,但它不起作用。 数据变量总是空的,我确定它有数据。 [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi)] public struct abs_data { /// ABS_DWORD->unsigned int public uint Length; /// ABS_BYTE[1] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 1)] public string Data; }

在.NET中使用令人难以置信的大数字

我正在尝试解决projecteuler.net上的问题,但我一直遇到一些问题。 第一个问题是在List中存储大量元素。 在列表中存储大量数据时,我一直收到OutOfMemoryException。 现在我承认我可能没有以最好的方式做这些事情但是,有没有办法定义应用程序可以消耗多少内存? 当我满足100,000,000个元素时,它通常会崩溃:S 其次,一些问题需要增加大量数字。 我使用ulong数据类型,我认为这个数字会变得非常大,但我仍然设法绕过最大的受支持的int并进入负数。 你有任何使用难以置信的大数字的技巧吗?