c#-4.0

C++/CLI delegate as function pointer (System.AccessViolationException)

五迷三道 提交于 2021-02-06 15:11:27
问题 I have been experimenting with C++/CLI delegates (as I am trying to make a .NET reference library), and I have been having the following problem. I define a delegate in C++/CLI, and then create an instance of the delegate in C#, and then call the instance of the delegate through unmanaged C++ via a function pointer. This all works as expected. Code to illustrate this (first my C#) using System; namespace TestProgram { class Program { static void Main(string[] args) { Library.Test

How to efficiently test if action is decorated with an attribute (AuthorizeAttribute)?

…衆ロ難τιáo~ 提交于 2021-02-05 20:28:37
问题 I'm using MVC and have a situation where in my OnActionExecuting() I need to determine if the Action method that is about to execute is decorated with an attribute, the AuthorizeAttribute in particular. I'm not asking if authorization succeeded/failed, instead I'm asking does the method require authorization. For non-mvc people filterContext.ActionDescriptor.ActionName is the method name I'm looking for. It is not, however, the currently executing method; rather, it is a method that will be

How to efficiently test if action is decorated with an attribute (AuthorizeAttribute)?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-05 20:27:40
问题 I'm using MVC and have a situation where in my OnActionExecuting() I need to determine if the Action method that is about to execute is decorated with an attribute, the AuthorizeAttribute in particular. I'm not asking if authorization succeeded/failed, instead I'm asking does the method require authorization. For non-mvc people filterContext.ActionDescriptor.ActionName is the method name I'm looking for. It is not, however, the currently executing method; rather, it is a method that will be

How to efficiently test if action is decorated with an attribute (AuthorizeAttribute)?

那年仲夏 提交于 2021-02-05 20:27:38
问题 I'm using MVC and have a situation where in my OnActionExecuting() I need to determine if the Action method that is about to execute is decorated with an attribute, the AuthorizeAttribute in particular. I'm not asking if authorization succeeded/failed, instead I'm asking does the method require authorization. For non-mvc people filterContext.ActionDescriptor.ActionName is the method name I'm looking for. It is not, however, the currently executing method; rather, it is a method that will be

How to compare 2 List<string> objects to get the missing value from the List<string>

人盡茶涼 提交于 2021-02-05 08:13:38
问题 How do I use the "NOT IN" to get the missing data, to be added to "foo" List. var accessories = new List<string>(); var foo = new List<string>(); accessories.Add("Engine"); accessories.Add("Tranny"); accessories.Add("Drivetrain"); accessories.Add("Power Window"); foo.Add("Engine"); foo.Add("Tranny"); foo.Add("Power Window"); foreach(var v in foo.Where(x => x??).???) { foo.Add(v); //Add the missing "Drivetrain" to it... } 回答1: Use List.Except: foo.AddRange(accessories.Except(foo)); From MSDN:

How to compare 2 List<string> objects to get the missing value from the List<string>

…衆ロ難τιáo~ 提交于 2021-02-05 08:12:49
问题 How do I use the "NOT IN" to get the missing data, to be added to "foo" List. var accessories = new List<string>(); var foo = new List<string>(); accessories.Add("Engine"); accessories.Add("Tranny"); accessories.Add("Drivetrain"); accessories.Add("Power Window"); foo.Add("Engine"); foo.Add("Tranny"); foo.Add("Power Window"); foreach(var v in foo.Where(x => x??).???) { foo.Add(v); //Add the missing "Drivetrain" to it... } 回答1: Use List.Except: foo.AddRange(accessories.Except(foo)); From MSDN:

Can two arrays which contain the same elements not be equal?

岁酱吖の 提交于 2021-02-05 07:58:24
问题 I encountered a stunning problem today where I'm trying to find if an object is contained in an List collection. The problem is that the list doesn't find the object and returns index as -1 when I can see it right there already. I then created a custom Index Finder to look for the object by comparing the properties rather than a direct equality where I discovered that one of the object's properties, a ushort array which was identical was returning false when compared, but they contain exactly

A keyword to reference the current object as a generic type

自作多情 提交于 2021-02-05 06:09:54
问题 I have a simple generic delegate: delegate void CommandFinishedCallback<TCommand>(TCommand command) where TCommand : CommandBase; I use it in the following abstract class: public abstract class CommandBase { public CommandBase() { } public void ExecuteAsync<TCommand>(CommandFinishedCallback<TCommand> callback) where TCommand : CommandBase { // Async stuff happens here callback.Invoke(this as TCommand); } } While this does work, I have no way of forcing the TCommand passed into Execute to be

Creating a database instance in C#

雨燕双飞 提交于 2021-02-05 04:55:41
问题 Is it possible to create a sql database instance with C# code. I have no problem adding a database to an existing SQL instance, for example WONEA\SQLEXPRESS but for creating another SQL instance such as WONEA\SQLEXPRESSTEST I'm a little stumped. Help! 回答1: The short answer is you can't do this using C# code. SQL server instances are essentially installations of SQL server - in order to create a new SQL server instance you need to run the SQL Server installer, and ask it to install one (which

C# HttpWebRequest ignore HTTP 500 error

放肆的年华 提交于 2021-02-04 13:32:47
问题 I'm trying to download a page using the WebRequest class in C#4.0. For some reason this page returns all the content correctly, but with a HTTP 500 internal error code. Request.EndGetResponse(ar); When the page returns HTTP 500 or 404, this method throws a WebException. How can I ignore this? I know it returns 500 but I still want to read the contents of the page / response. 回答1: You can a try / catch block to catch the exception and do additional processing in case of http 404 or 500 errors