.net

How do I use reflection to call a generic method?

久未见 提交于 2021-02-05 11:47:15
问题 What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime? Consider the following sample code - inside the Example() method, what's the most concise way to invoke GenericMethod<T>() using the Type stored in the myType variable? public class Sample { public void Example(string typeName) { Type myType = FindType(typeName); // What goes here to call GenericMethod<T>()? GenericMethod<myType>(); // This doesn't

Increment a string value

别等时光非礼了梦想. 提交于 2021-02-05 11:31:06
问题 How can I increment a string value? For example: string RECONCILIATION_COUNT; if (thing happens) { RECONCILIATION_COUNT++; } This is normally wont work since it is not possible to increment a string variable in the same way used for int values. RECONCILIATION_COUNT is always a number from 0-5. I chose to declare it as a string since I am getting this value from database and i am not finding a way how to get an int value from sql. RECONCILIATION_COUNT = Rows["RECONCILIATION_COUNT"].toString();

Awaiting a sequence of tasks to run sequentially

梦想与她 提交于 2021-02-05 11:28:26
问题 I would like to create a generic method to await for a sequence of tasks to finish sequentially, retrieving the result of each one. This is the code I've created: public static class TaskMixin { public static async Task<IEnumerable<T>> AwaitAll<T>(this IEnumerable<Task<T>> tasks) { var results = new List<T>(); foreach (var t in tasks) { results.Add(await t); } return results; } } Is there a better or built-in way to do it? Notice Before writing the above method I tried with the built-in Task

How to pin memory allocated by Marshal.AllocHGlobal() in C#?

 ̄綄美尐妖づ 提交于 2021-02-05 11:22:10
问题 How do you pin memory allocated by Marshal.AllocHGlobal()? My first attempt was the following: int bytes = 10; IntPtr ip = Marshal.AllocHGlobal(bytes); GCHandle iph = GCHandle.Alloc(ip, GCHandleType.Pinned); Although I think this only pins the IntPtr and not the block of memory referred to by the IntPtr . 回答1: The memory allocated by AllocHGlobal is already pinned. the IntPtr that is returned is the address of the pinned location. UPDATE: To be pedantic you can't actually "pin" the memory

Unable to print whole list to console

佐手、 提交于 2021-02-05 11:22:08
问题 Keep getting the following message when printing a list on console. System.Collections.Generic.List`1[System.Int32] This is the console code. It is designed to generate a Fibonacci sequence of a given length. I have tried using the ToString() method, but that doesn't work either. I have built the algorithm in Java so i know that the issue, is fundamentally a C# problem. The issue is resolved if print i print the list elements individually, but i can't print the whole list. class Program {

Visual Studio 2017 does not show collapse icon for js file

家住魔仙堡 提交于 2021-02-05 11:14:53
问题 Visual studio 2017 does not show function collapse icon for JS files. However, it highlights function and variable keywords. 回答1: You can enable 'outling' (as it's called) for the current document by selecting the "Outlining" item from the "Edit" menu, and choosing "Start Automatic Outlining," as shown below: In theory, there should be a "global" setting for this, like there is for other languages (e.g. Options -> Text Editor -> C# -> Advanced then "Enter Outlining mode when files open") but,

How to access my variables in a web method?

∥☆過路亽.° 提交于 2021-02-05 11:14:07
问题 I am using ASP.NET Webforms and in one page I want to make an AJAX call to a web method in the code behind. The problem is that web methods are static and I can't access page variables. I need to be able to have Ninject inject a dependency. Is there a way to do this in a web method? public partial class Default : Ninject.Web.PageBase { [Inject] public ISecurityController SecurityController { get; set; } [WebMethod] public static string DoSomething() { SecurityController.WriteToLog(); // Can't

Visual Studio 2017 does not show collapse icon for js file

南楼画角 提交于 2021-02-05 11:14:05
问题 Visual studio 2017 does not show function collapse icon for JS files. However, it highlights function and variable keywords. 回答1: You can enable 'outling' (as it's called) for the current document by selecting the "Outlining" item from the "Edit" menu, and choosing "Start Automatic Outlining," as shown below: In theory, there should be a "global" setting for this, like there is for other languages (e.g. Options -> Text Editor -> C# -> Advanced then "Enter Outlining mode when files open") but,

C# .NET app's product version not updating in Control Panel -> Program -> Uninstall a program

拥有回忆 提交于 2021-02-05 11:13:09
问题 I'm having trouble updating a C# application's product number. I updated the [assembly: AssemblyVersion("0.0.4.4")] and [assembly: AssemblyFileVersion("0.0.4.4")] in AssemblyInfo.cs. Then build a new MSI setup file. Then I proceed with uninstalling & installing the new version of the app. However, in Control Panel's Uninstall a program, my app keeps showing its product version as 0.4.3. Meanwhile, right-click & bring up properties of the app's EXE file shows the correct version number (4.4).

C# .NET app's product version not updating in Control Panel -> Program -> Uninstall a program

夙愿已清 提交于 2021-02-05 11:05:59
问题 I'm having trouble updating a C# application's product number. I updated the [assembly: AssemblyVersion("0.0.4.4")] and [assembly: AssemblyFileVersion("0.0.4.4")] in AssemblyInfo.cs. Then build a new MSI setup file. Then I proceed with uninstalling & installing the new version of the app. However, in Control Panel's Uninstall a program, my app keeps showing its product version as 0.4.3. Meanwhile, right-click & bring up properties of the app's EXE file shows the correct version number (4.4).