.NET

Unit Test Non-Public Methods in C# Unit Tests

To expose non-public methods to the test project, you need to mark the assembly with an attribute called InternalsVisibleTo in the asemblyinfo.cs file?? For example: [assembly: InternalsVisibleTo(“testProjectName”)] You need to mark the methods as Internal, and they will available for you to test in the test project.

Replace a Web API Response with a Custom JSON

Use StringContent?in the Web API response to return a custom JSON. See below for an example: StringContent responseContent = new StringContent ( ” { ‘handled’ : ‘true’ } “, Encoding.UTF8, ‘application/json’);r.Content = responseContent;return r;

Extract Faces Using Amazon Rekognition

AmazonRekognitionClient amazonRekognitionClient = new AmazonRekognitionClient(Amazon.RegionEndpoint.);byte[] imageData = System.IO.File.ReadAllBytes(inputImageFile);DetectFacesRequest facesRequest = new DetectFacesRequest();facesRequest.Image = new Amazon.Rekognition.Model.Image{Bytes = new MemoryStream(imageData)};DetectFacesResponse facesResponse = amazonRekognitionClient.DetectFaces(facesRequest);//If faces are detected, extract themif (facesResponse.FaceDetails.Count > 0){foreach (var face in facesResponse.FaceDetails){//save them to bitmap by cropping with bounds of the face object}}

Convert a String to a Byte Array

Explore how to convert a string to a byte array. String stringToBeConverted = ” ? ” ;int stringLength = stringToBeConverted.Length;byte[] bytes = new byte[stringLength / 2];for (int counter = 0; counter < stringLength; counter += 2)bytes[counter / 2] = Convert.ToByte(stringToBeConverted.Substring(counter, 2), 16);

Split a Concatenated String with a Delimiter

Here is how to split a concatenated string with a delimiter and remove empty entries. public static void Test() { string concatenatedString = “,ONE,,TWO,,,THREE,,”; char[] charSeparator = new char[] {‘,’}; string[] splitArray= concatenatedString.Split(charSeparator, StringSplitOptions.RemoveEmptyEntries);}

Use BitConverter Class to Convert a Byte Array

BitConverter’s ToString?method can be used to convert a byte array to a string. See below for an example: byte[] byteArray = ?string convertedString = BitConverter.ToString(byteArray).Replace(“-“,””);

Using SoapHexBinary Class for Byte and String Conversions

See below for a code sample of how to perform byte and string conversions: using System.Runtime.Remoting.Metadata.W3cXsd2001;public static byte[] GetStringToBytes(string stringValue){ SoapHexBinary result = SoapHexBinary.Parse(stringValue); return result.Value;}public static string GetBytesToString(byte[] byteArray){ SoapHexBinary result = new SoapHexBinary(byteArray); return result.ToString();}

Enumerate All the Values in an Enum

See an easy way to enumerate all the values. public enum WorkTypes{Remote = 0,Office = 1,Exempted = 2}foreach (WorkTypes workType in Enum.GetValues(typeof(WorkTypes)))Console.WriteLine(workType);

No more posts to show