unsafe

Unsafe code won't compile on Visual Studio 2015

自古美人都是妖i 提交于 2019-12-01 16:24:33
I'm trying to compile a program on the new DNX4.6 core, but it won't compile due to: error CS0227: Unsafe code may only appear if compiling with /unsafe This is my code: [CompilerGenerated] public unsafe class GrayscaleQuantizer : PaletteQuantizer { I've looked online, and I can't get any source with the same problem as I have. I can't tick the 'Allow Unsafe Code' at the Build tab of the Project Properties, because there is no option to do so... Does anyone know a solution? You need to set "allowUnsafe": true in the build options in project.json . I wasn't smart enough to be able to figure it

Refused to set unsafe header “Connection”

不羁的心 提交于 2019-12-01 09:05:07
I am working on a cross platform application that targets Android and iOS platforms. I am using jQuery 1.9.1, Jquery Mobile 1.3.1 and Phonegap 2.8.0. I want to send an ajax request and set the request headers "Connection" and "Keep-Alive". On Android Phones with OS greater than 4.1 (Whose default browser is Chrome) I get an error which says "Refused to set unsafe header "Connection"". I am able to send such requests on lower end devices and even on iPhones. Can Anyone Please help me out. I have to set these 2 headers in the request. Thanks in advance. Section 4.6.2 of the W3C XMLHttpRequest

C#: Benefit of explicitly stating “unsafe” / compiler option

…衆ロ難τιáo~ 提交于 2019-12-01 03:58:11
I understand pointers and the rare need to use them in C# code. My question is: what is the reasoning behind having to explicitly state "unsafe" in a block of code. Additionally, why must a compiler option be changed to allow "unsafe" code? Bottom Line: What in the CLR (or language specs) makes it so we can't just use pointers whenever we want (much like C and C++) without having to type "unsafe" and change the compiler option? For clarification: I know what "unsafe" and "safe" code is. It's just a question of why must we do all the extra work (ok, not THAT much extra) just to be able to use

C#: Benefit of explicitly stating “unsafe” / compiler option

风格不统一 提交于 2019-12-01 00:58:15
问题 I understand pointers and the rare need to use them in C# code. My question is: what is the reasoning behind having to explicitly state "unsafe" in a block of code. Additionally, why must a compiler option be changed to allow "unsafe" code? Bottom Line: What in the CLR (or language specs) makes it so we can't just use pointers whenever we want (much like C and C++) without having to type "unsafe" and change the compiler option? For clarification: I know what "unsafe" and "safe" code is. It's

C#: convert generic pointer to array

倖福魔咒の 提交于 2019-12-01 00:23:25
I want to convert a byte* to a byte[] , but I also want to have a reusable function to do this: public unsafe static T[] Create<T>(T* ptr, int length) { T[] array = new T[length]; for (int i = 0; i < length; i++) array[i] = ptr[i]; return array; } Unfortunately I get a compiler error because T might be a ".NET managed type" and we can't have pointers to those . Even more frustrating is that there is no generic type constraint which can restrict T to "unmanaged types". Is there a built-in .NET function to do this? Any ideas? The method that could match what you are trying to do is Marshal.Copy

Publish web application with unsafe code

99封情书 提交于 2019-11-30 21:21:26
I'm trying to publish a web application (with VS2012 Web) in which I need to run a vb script. That script currently doesn't run correctly probably because of the lack of permissions. I am currently trying to run it as a user and supply some credentials. The password I have to provide must be in a System.Security.SecureString which needs a char* to be created. When I run my app in debug everything works fine and as expected. But when comes the time to publish the app to the server, it says : 1>C:\SVN\...\Default.aspx.cs(108,21,108,27): error CS0227: Unsafe code may only appear if compiling with

Are ref and out in C# the same a pointers in C++?

有些话、适合烂在心里 提交于 2019-11-30 20:10:53
I just made a Swap routine in C# like this: static void Swap(ref int x, ref int y) { int temp = x; x = y; y = temp; } It does the same thing that this C++ code does: void swap(int *d1, int *d2) { int temp=*d1; *d1=*d2; *d2=temp; } So are the ref and out keywords like pointers for C# without using unsafe code? They're more limited. You can say ++ on a pointer, but not on a ref or out . EDIT Some confusion in the comments, so to be absolutely clear: the point here is to compare with the capabilities of pointers. You can't perform the same operation as ptr++ on a ref / out , i.e. make it address

C#: convert generic pointer to array

六眼飞鱼酱① 提交于 2019-11-30 19:32:54
问题 I want to convert a byte* to a byte[] , but I also want to have a reusable function to do this: public unsafe static T[] Create<T>(T* ptr, int length) { T[] array = new T[length]; for (int i = 0; i < length; i++) array[i] = ptr[i]; return array; } Unfortunately I get a compiler error because T might be a ".NET managed type" and we can't have pointers to those . Even more frustrating is that there is no generic type constraint which can restrict T to "unmanaged types". Is there a built-in .NET

Safe vs Unsafe code

拥有回忆 提交于 2019-11-30 19:04:37
Read this question today about safe and unsafe code I then read about it in MSDN but I still don't understand it. Why would you want to use pointers in C#? Is this purely for speed? There are three reasons to use unsafe code: APIs (as noted by John) Getting actual memory address of data (e.g. access memory-mapped hardware) Most efficient way to access and modify data (time-critical performance requirements) Sometimes you'll need pointers to interface your C# to the underlying operating system or other native code. You're strongly discouraged from doing so, as it is "unsafe" (natch). There will

C# Get type of fixed field in unsafe struct with reflection

本小妞迷上赌 提交于 2019-11-30 18:17:34
I'm trying to get the field types of an unsafe struct using some fixed fields. The fixed fields FieldType do not return the actual type. [StructLayout(LayoutKind.Sequential, Pack = 1)] public unsafe struct MyStruct { public UInt32 Field1; public fixed sbyte Field2[10]; public UInt64 Field3; } void Test() { var theStruct = new MyStruct(); string output = ""; foreach (FieldInfo fi in theStruct.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)) { output += fi.Name + ": " + fi.FieldType.ToString() + "\r\n"; } } Output: Field1: System.UInt32 Field2: TestProjectNS.MyStruct+<Field2>e_