c#-4.0

Loop through attribute of a class and get a count of how many properties that are not null

匆匆过客 提交于 2021-02-19 08:03:21
问题 I was wondering if there was a simpler way to do something like this? public int NonNullPropertiesCount(object entity) { if (entity == null) throw new ArgumentNullException("A null object was passed in"); int nonNullPropertiesCount = 0; Type entityType = entity.GetType(); foreach (var property in entityType.GetProperties()) { if (property.GetValue(entity, null) != null) nonNullPropertiesCount = nonNullPropertiesCount+ 1; } return nonNullPropertiesCount; } 回答1: How about: public int

Accessing a single value from a json file in c#

老子叫甜甜 提交于 2021-02-19 04:26:07
问题 My json file looks something like this { lab :[ { "name": "blah", "branch": "root", "buildno": "2019" }] } so, i need to access the value of the buildno (2019) and assaign it to a variable in my program. This is my class public class lab { public string name { get; set; } public string branch { get; set; } public string buildno { get; set; } } I tried this method using Newtonsoft.json using (StreamReader r = new StreamReader(@"ba.json")) { string json2 = r.ReadToEnd(); lab item = JsonConvert

My custom Windows Service is not writing to my custom Event Log

此生再无相见时 提交于 2021-02-19 03:46:08
问题 I have written a custom Windows Service that writes data to a custom Event Log (in the Windows Event Viewer). For dev'ing the biz logic that the service uses, I created a Windows Form which simulates the Start/Stop methods of the Windows Service. When executing the biz logic via the Windows Forms, info is successfully written to my custom Event Log. However, when I run the same biz logic from the custom Windows Service, information is failing to be written to the Event Log. To be clear, I

Using FileSystemMonitoring for reading changes in app.config and writing to app.config realtime

白昼怎懂夜的黑 提交于 2021-02-19 03:16:56
问题 I am using FileSystemWatcher to monitor any changes in the app.config file. And also, writing to the config file. Here is my code : MyApplication is the main project and DataCache is a clas library referenced in MyApplication. using System; using System.Threading; using System.IO; namespace MyApplication { public class Program { public static string rootFolderPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); public static string configFilePath = Path

Using FileSystemMonitoring for reading changes in app.config and writing to app.config realtime

萝らか妹 提交于 2021-02-19 03:15:06
问题 I am using FileSystemWatcher to monitor any changes in the app.config file. And also, writing to the config file. Here is my code : MyApplication is the main project and DataCache is a clas library referenced in MyApplication. using System; using System.Threading; using System.IO; namespace MyApplication { public class Program { public static string rootFolderPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); public static string configFilePath = Path

Why can lambdas convert function calls into Actions?

只愿长相守 提交于 2021-02-18 20:32:35
问题 In this code fragment: List<String> names = new List<String>(); names.Add("Bruce"); names.Add("Tom"); names.Add("Tim"); names.Add("Richard"); names.ForEach(x => Print(x)); private static string Print(string s) { Console.WriteLine(s); return s; } Print is not an Action for sure since it is returning string ; however x=> Print(x) is, why? 回答1: The type of the lambda expression x => Print(x) is determined based on its context. Since the compiler knows that the lambda is assigned to Action<string

Why can lambdas convert function calls into Actions?

北城以北 提交于 2021-02-18 20:32:12
问题 In this code fragment: List<String> names = new List<String>(); names.Add("Bruce"); names.Add("Tom"); names.Add("Tim"); names.Add("Richard"); names.ForEach(x => Print(x)); private static string Print(string s) { Console.WriteLine(s); return s; } Print is not an Action for sure since it is returning string ; however x=> Print(x) is, why? 回答1: The type of the lambda expression x => Print(x) is determined based on its context. Since the compiler knows that the lambda is assigned to Action<string

How to look at Bitmap objects in Visual Studio debugger?

隐身守侯 提交于 2021-02-18 19:53:07
问题 I am building a C# app that creates many bitmaps (System.Drawing.Image). Having the bitmaps seen in the debugger as pictures, would be of enormous help. The debugger has native support for XML files. Is there a way to see the pictures? 回答1: There is no debugger visualizer by default for Bitmap, so you might want to give this one a try: http://imagedebugvisualizer.codeplex.com/ 回答2: Another open source image and bitmap visualizer which works in Visual Studio 2019: https://github.com/Jaex

How to look at Bitmap objects in Visual Studio debugger?

孤人 提交于 2021-02-18 19:50:01
问题 I am building a C# app that creates many bitmaps (System.Drawing.Image). Having the bitmaps seen in the debugger as pictures, would be of enormous help. The debugger has native support for XML files. Is there a way to see the pictures? 回答1: There is no debugger visualizer by default for Bitmap, so you might want to give this one a try: http://imagedebugvisualizer.codeplex.com/ 回答2: Another open source image and bitmap visualizer which works in Visual Studio 2019: https://github.com/Jaex

Signing a string with HMAC-MD5 with C#

[亡魂溺海] 提交于 2021-02-18 12:35:09
问题 I got the following HMAC key (in hexadecimal format): 52320e181a481f5e19507a75b3cae4d74d5cfbc328f7f2b738e9fb06b2e05b55b632c1c3d331dcf3baacae8d3000594f839d770f2080910b52b7b8beb3458c08 I need to sign this string: 1100002842850CHF91827364 The result should be this (in hexadecimal format): 2ad2f79111afd818c1dc0916d824b0a1 I have the following code: string key = "52320e181a481f5e19507a75b3cae4d74d5cfbc328f7f2b738e9fb06b2e05b55b632c1c3d331dcf3baacae8d3000594f839d770f2080910b52b7b8beb3458c08";