博文

目前显示的是标签为“C#”的博文

Enumerable.Distinct Method in C#

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(System.Linq.Enumerable.Distinct%60%601);k(SolutionItemsProject);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.6.1);k(DevLang-csharp)%26rd%3Dtrue&view=netframework-4.8 public class Product { public string Name { get ; set ; } public int Code { get ; set ; } } // Custom comparer for the Product class class ProductComparer : IEqualityComparer < Product > { // Products are equal if their names and product numbers are equal. public bool Equals ( Product x, Product y ) { //Check whether the compared objects reference the same data. if (Object.ReferenceEquals(x, y)) return true ; //Check whether any of the compared objects is null. if (Object.ReferenceEquals(x, null ) || Object.ReferenceEquals(y, null )) ...

Write log in ActionFilterAttribute in C#

ActionFilterAttribute public class JinFilter : System.Web.Http.Filters.ActionFilterAttribute {     public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)     {               WriteLog(actionContext);         return base.OnActionExecutingAsync(actionContext, cancellationToken);     }     private async void WriteLog(HttpActionContext actionContext)     {         try         {             System.Text.StringBuilder sb = new System.Text.StringBuilder();             sb.Append(actionContext.Request.Method.ToString() + " ");             sb.AppendLine(actionContext.Request.RequestUri.OriginalString);             sb.AppendLine(Convert.ToString(actionContext.Reque...

unsafe C#

        unsafe void ShowBytes(byte* start, int len)         {             for (int i = 0; i < len; i++)             {                 Console.Write("{0}", start[i].ToString("X2"));             }             Console.WriteLine();         }         unsafe void ShowShort(short x)         {             ShowBytes((byte*)&x, sizeof(short));         }         unsafe void ShowShort(int x)         {             ShowBytes((byte*)&x, sizeof(int));         }         unsafe void ShowShort(uint x)         {         ...

JSON & C#

1. System.Text.Json.Utf8JsonReader ref: https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0  https://github.com/ysharplanguage/FastJsonParser#Overview 2. Newtonsoft.Json library  using NewTonsoft.Json; //Using Manage NuGet Packages installs Newtonsoft.Json .  Cat cat2 = new Cat(){name="hik",age=3};  string s1= Newtonsoft.Json.JsonConvert . SerializeObject (cat2);   Cat   cat3 = JsonConvert . DeserializeObject <Cat>(JSONstr);   string outStr2 = JsonConvert.SerializeObject(cat2); private string CapitalFirstLetter(string s)  {             try             {                 string rs = char.ToUpper(s[0]) + s.Substring(1);                 return r...

log ClosedXML

Microsoft Visual Studio 1. NuGet Package Manager installs ClosedXML that depends DocumentFormat.OpenXml, ExcelNumberFormat, and WindowsBase. 2.using ClosedXML.Excel; using System; using System.Data; using System.Data.OleDb; using System.IO; 3. read .xlsx file using (var workbook = new XLWorkbook(xlsxfile)) { ...   var ws = workbook.Worksheet(1);   foreach (var row in ws.Rows())   {if (!row.IsEmpty()){string cell1 = row.Cell(1).GetValue<string>();                                      object value =row.Cell(1).Value; }   } } 4. OpenFileDialog             using (OpenFileDialog openFileDialog = new OpenFileDialog())             {       ...

Assembly in C#

1. using System.Reflection.Assembly System.Reflection.AssemblyName name = System.Reflection.Assembly.GetExecutingAssembly().GetName(); //name is {AssemblyNameInProperties, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null} byte[]  key =  name.GetPublicKey(); 2. reflection Assembly assem = typeof (ExampleClass).Assembly; // Create an object from the assembly, passing in the correct number // and type of arguments for the constructor. Object o = assem.CreateInstance( "Example" , false , BindingFlags.ExactBinding, null , new Object[] { 2 }, null , null ); // Make a late-bound call to an instance method of the object. MethodInfo m = assem.GetType( "Example" ).GetMethod( "SampleMethod" ); Object ret = m.Invoke(o, new Object[] { 42 }); Console.WriteLine( "SampleMethod returned {0}." , ret); reference: docs.microsoft.com/en-us/dotnet/api/system.re...