Tag: C#的

中止,终止或退出?

这三者之间有什么区别,如果我不能正确处理exception,我该如何结束程序?

int运算符!=和==比较时为零

我发现!=和==不是testing零或非零的最快方法。 bool nonZero1 = integer != 0; xor eax, eax test ecx, ecx setne al bool nonZero2 = integer < 0 || integer > 0; test ecx, ecx setne al bool zero1 = integer == 0; xor eax, eax test ecx, ecx sete al bool zero2 = !(integer < 0 || integer > 0); test […]

WPF DataGrid – 列中的button,从Click事件处理程序中获取它所在的行

我已经将WPF Datagrid的itemsource设置为从我的DAL返回的对象列表。 我还添加了一个包含button的额外列,下面是xaml。 <toolkit:DataGridTemplateColumn MinWidth="100" Header="View"> <toolkit:DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Click="Button_Click">View Details</Button> </DataTemplate> </toolkit:DataGridTemplateColumn.CellTemplate> </toolkit:DataGridTemplateColumn> 这呈现罚款。 然而,在Button_Click方法,有没有什么办法可以得到button所在的数据网格上的行? 更具体地说,我的对象的属性之一是“ID”,我希望能够传递到事件处理程序中的另一种forms的构造函数。 private void Button_Click(object sender, RoutedEventArgs e) { //I need to know which row this button is on so I can retrieve the "id" } 也许我需要在xaml中额外添加一些东西,或者我可以用迂回的方式来解决这个问题? 任何帮助/build议表示赞赏。

在C ++中设置int为无穷大

我有一个int a ,它需要等于“infinity”。 这意味着如果 int b = anyValue; a>b总是如此。 有没有C ++的任何function,可以使这成为可能?

常量string与静态只读string在C#

在C#中,有什么区别 static readonly string MyStr; 和 const string MyStr;

如何在一个generics扩展方法中使用string列名在IQueryable上应用OrderBy?

public static IQueryable<TResult> ApplySortFilter<T, TResult>(this IQueryable<T> query, string columnName) where T : EntityObject { var param = Expression.Parameter(typeof(T), "o"); var body = Expression.PropertyOrField(param,columnName); var sortExpression = Expression.Lambda(body, param); return query.OrderBy(sortExpression); } 因为OrderBy的types不是从sortExpression推断的,我需要在运行时指定它: var sortExpression = Expression.Lambda<T, TSortColumn>(body, param); 要么 return query.OrderBy<T, TSortColumn>(sortExpression); 我不认为这是可能的,但是TSortColumn只能在运行时确定。 有没有解决的办法?

将POST中的json发送到Web API服务时出错

我正在使用Web API创build一个Web服务。 我实现了一个简单的类 public class ActivityResult { public String code; public int indexValue; public int primaryCodeReference; } 然后我在我的控制器里面实现 [HttpPost] public HttpResponseMessage Post(ActivityResult ar) { return new HttpResponseMessage(HttpStatusCode.OK); } 但是当我调用在POST传递的API文件json: {"code":"XXX-542","indexValue":"3","primaryCodeReference":"7"} 我获得以下错误信息: { "Message": "The request entity's media type 'text/plain' is not supported for this resource.", "ExceptionMessage": "No MediaTypeFormatter is available to read an object of […]

为什么Assert.AreEqual(T obj1,Tobj2)以相同的字节数组失败

在下面的代码段中有两个相同的字节数组: /// <summary> ///A test for Bytes ///</summary> [TestMethod()] public void BytesTest() { byte[] bytes = Encoding.UTF8.GetBytes(Properties.Resources.ExpectedPacketData); TransferEventArgs target = new TransferEventArgs(bytes); byte[] expected = Encoding.UTF8.GetBytes(Properties.Resources.ExpectedPacketValue); byte[] actual; actual = target.Bytes; Assert.AreEqual(expected, actual); } 两个数组都是相同的,直到字节。 在这种情况下,为什么Assert.AreEqual失败?

Asp Net Web API 2.1获取客户端IP地址

你好,我需要得到客户端的IP请求一些方法在Web API,我试图从这里使用这个代码,但它总是返回服务器本地IP,如何得到正确的方式? HttpContext.Current.Request.UserHostAddress; 从其他问题: public static class HttpRequestMessageExtensions { private const string HttpContext = "MS_HttpContext"; private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty"; public static string GetClientIpAddress(this HttpRequestMessage request) { if (request.Properties.ContainsKey(HttpContext)) { dynamic ctx = request.Properties[HttpContext]; if (ctx != null) { return ctx.Request.UserHostAddress; } } if (request.Properties.ContainsKey(RemoteEndpointMessage)) { dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage]; if (remoteEndpoint != null) […]

帮助C#generics错误 – “types'T'必须是一个不可为空的值types”

我是C#的新手,不明白为什么下面的代码不起作用。 public static Nullable<T> CoalesceMax<T>(Nullable<T> a, Nullable<T> b) where T : IComparable { if (a.HasValue && b.HasValue) return a.Value.CompareTo(b.Value) < 0 ? b : a; else if (a.HasValue) return a; else return b; } // Sample usage: public DateTime? CalculateDate(DataRow row) { DateTime? result = null; if (!(row["EXPIRATION_DATE"] is DBNull)) result = DateTime.Parse((string)row["EXPIRATION_DATE"]); if (!(row["SHIPPING_DATE"] […]