代码契约:我们是否必须在委托方法中冗余地指定Contract.Requires(…)语句?

我打算使用新的.NET 4 Code Contractsfunction进行未来的开发。 这让我想知道是否必须在一系列方法中冗余地指定等效的Contract.Requires(…)语句。 我认为一个代码示例胜过千言万语: public bool CrushGodzilla(string weapon, int velocity) { Contract.Requires(weapon != null); // long code return false; } public bool CrushGodzilla(string weapon) { Contract.Requires(weapon != null); // specify contract requirement here // as well??? return this.CrushGodzilla(weapon, int.MaxValue); } 对于运行时检查它并不重要,因为我们最终总是会遇到需求检查,如果失败我们会收到错误。 但是,当我们再次在第二次超载中没有指定合同要求时,这被认为是不好的做法吗? 此外,还将具有编译时检查的function ,并且还可能设计代码合同的时间检查 。 在Visual Studio 2010中,它似乎尚不适用于C#,但我认为有一些类似Spec#的语言已经可以使用。 当我们编写代码来调用这样的方法并且我们的参数当前可以或将为null时,这些引擎可能会给我们提示。 所以我想知道这些引擎是否总是会分析一个调用堆栈,直到找到一个目前不满意的合同方法? 此外, 在这里我了解了Contract.Requires(…)和Contract.Assume(…)之间的区别 。 […]

将sql server rowversion转换为long或ulong?

rowversion(timestamp)数据类型的正确类型是什么? 我知道这是8个字节,但我找不到MSDN中的链接,它告诉它是有符号还是无符号长。 我应该使用哪些代码,它是否重要? byte[] SqlTimeStamp; long longConversion; longConversion = BitConverter.ToInt64(SqlTimeStamp,0); TimeStamp = BitConverter.GetBytes(longConversion); ulong ulongConversion; ulongConversion = BitConverter.ToUInt64(SqlTimeStamp,0); TimeStamp = BitConverter.GetBytes(ulongConversion);

什么是强命名,我如何强名二进制?

在我发布之前,我听说过某些地方需要强烈命名我的二进制文件。 这是什么想法?

Process.Start()和PATH环境变量

我有以下简单的C#应用​​程序,它只是尝试启动“jconsole.exe”,它在我的机器上位于C:\ Programs \ jdk16 \ bin中。 using System; using System.Diagnostics; namespace dnet { public class dnet { static void Main( string[] args ) { try { Process.Start(“jconsole.exe”); Console.WriteLine(“Success!”); } catch (Exception e) { Console.WriteLine(“{0} Exception caught.”, e); } } } } 如果我的PATH环境变量设置为 c:\windows;c:\windows\sytem32;c:\programs\jdk16\bin 它完美地运作。 但是,如果PATH环境变量设置为 c:\windows;c:\windows\sytem32;c:\\programs\jdk16\bin (注意“c:”和“program”之间的两个反斜杠),它以win32exception失败。 System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file […]

名称空间’System.Management’中不存在’ManagementClass’

嗨,我正在使用此方法获取mac地址 public string GetMACAddress() { System.Management.ManagementClass mc = default(System.Management.ManagementClass); ManagementObject mo = default(ManagementObject); mc = new ManagementClass(“Win32_NetworkAdapterConfiguration”); ManagementObjectCollection moc = mc.GetInstances(); foreach (var mo in moc) { if (mo.Item(“IPEnabled”) == true) { return mo.Item(“MacAddress”).ToString(); }else return null; } } 但我收到这个错误 Compiler Error Message: CS0234: The type or namespace name ‘ManagementClass’ does not exist in the […]

LINQ to Entities无法识别方法’Int32

public ActionResult ReadXMLDevices(int groupID) { var query = from k in XMLEntities.unassigneditems where k.DevOrAcc == true && k.Group == groupID select k; var view_query = from i in query select new GetFreeDevices { MArticleNumber = i.ArticleNumber, MFirmware = i.Firmware, MGroup = i.Group, MName = i.Name, MSoftware = i.SoftwareVersion, SA = GetNumberOfDevices(i.ArticleNumber,2), STH = GetNumberOfDevices(i.ArticleNumber,3), SASTH […]

LINQ vs foreach vs性能测试结果

有人可以解释这些结果吗? 我知道有一些重复的问题,但我还没有找到一个与我的结果得出相同结论的问题:o using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SpeedTest { class Person { public Person(string name) { this.Name = name; } public string Name { get; set; } } class Program { static void Main(string[] args) { var people = new List(); AddTwins(“FRANCISCO”, people); var stopwatch = new […]

将角度转换为矢量

我正在做一些游戏编程。 FWIW我正在使用XNA,但我怀疑这是否相关。 我想将度数转换为幅度为1的方向向量(即X和Y)。 我的起源(0,0)位于左上角。 所以我想要0度转换为[0,-1] 我认为最好的方法是采用我的North / Up定义并使用矩阵旋转它,但这似乎不起作用。 这是守则…… public class Conversion { public static Vector2 GetDirectionVectorFromDegrees(float Degrees) { Vector2 North= new Vector2(0, -1); float Radians = MathHelper.ToRadians(Degrees); var RotationMatrix = Matrix.CreateRotationZ(Radians); return Vector2.Transform(North, RotationMatrix); } } ……这是我的unit testing…… [TestFixture] public class Turning_Tests { [Test] public void Degrees0_Tests() { Vector2 result = Conversion.GetDirectionVectorFromDegrees(0); Assert.AreEqual(0, result.X); […]

在包含类,使用属性或字段?

在包含它们的类中编写代码时,使用私有字段或属性是一种好习惯吗? 例如,如果我有这个字段/属性对,则此类之外的类必须使用该属性。 课堂内的代码怎么样? 它应该使用私人领域,还是应该通过财产? private string _foo; protected string Foo { get { return this._foo; } } private void SomeMethod() { string dummyVariable = “snuh” + this._foo; // So, this… string dummyVariable = “snuh” + this.Foo; // … or this? } 在这里使用属性的一个优点是,如果getter中有任何逻辑,它仍然会被执行。 我很想知道这里是否有最佳实践政策。

是否有“DisplayMember”和“ValueMember”,如CheckedListBox控件的属性? C#winforms

我有这个DataTable具有以下结构: ID | VALUE —————- 1 | Item 1 2 | Item 2 3 | Item 3 我通过将每行添加为项目,将DataTable的值显示到CheckedListBox控件中。 但是如何包含ID? 是否有“DisplayMember”和“ValueMember”,如CheckedListBox控件的属性?