.net

Cannot take the address of, get the size of, or declare a pointer to a managed type ('T')

£可爱£侵袭症+ 提交于 2021-02-07 11:52:08
问题 Why oh why is this not allowed: private static unsafe byte[] ConvertStruct<T>(T str) where T: struct { T* ptr = &str; int size = Marshal.SizeOf(str); var arr = new byte[size]; Marshal.Copy((IntPtr) ptr, arr, 0, size); return arr; } I get the compile error "Cannot take the address of, get the size of, or declare a pointer to a managed type ('T')" Or is the "struct" constraint not enough? In the sense that it could still be an managed struct? Do I understand correctly that my struct might not

AutoFixture and interfaces

此生再无相见时 提交于 2021-02-07 11:22:33
问题 Let's say I have interface: public interface IFoo { int Bar1 { get; set; } int Bar2 { get; set; } } If IFoo was class, I could write: fixture.CreateAnonymous<IFoo>(); and the result will have numbers set for Bar1 and Bar2 . But how to do this with interface? I tried to use AutoMoqCustomization but this seems to be for properties with interface type and not interfaces itself. I am looking for automated way like CreateAnonymous is for classes. Currenlty I am creating interface mock and setuping

HttpClient Instancing Per Service-Endpoint

谁说胖子不能爱 提交于 2021-02-07 11:19:20
问题 When instancing an HttpClient, the one common piece of advice is: Use a singleton, do not dispose after each use. However, based on this link I see commentary which I think implies another rule: The HttpClient class instance acts as a session to send HTTP requests. An HttpClient instance is a collection of settings applied to all requests executed by that instance. In addition, every HttpClient instance uses its own connection pool, isolating its requests from requests executed by other

How can I control the name of a generic WCF Message Contract

风格不统一 提交于 2021-02-07 11:19:15
问题 I'm generating a WCF service using the message contract model. I have created a generic request message contract like so: [MessageContract] public Request<T> { [MessageBodyMember] public T Details { get; set; } } I'm used to using [DataContract(Name="Contract{0}")] to produce readable names for generic data contracts, but this approach does not seem to work for me using message contracts. Is there a way to achieve the same behaviour using the message contract model? 回答1: It seems like a lot

How do you cast an object to a Tuple?

徘徊边缘 提交于 2021-02-07 11:18:21
问题 I create my Tuple and add it to a combo box: comboBox1.Items.Add(new Tuple<string, string>(service, method)); Now I wish to cast the item as a Tuple, but this does not work: Tuple<string, string> selectedTuple = Tuple<string, string>(comboBox1.SelectedItem); How can I accomplish this? 回答1: Don't forget the () when you cast: Tuple<string, string> selectedTuple = (Tuple<string, string>)comboBox1.SelectedItem; 回答2: Your syntax is wrong. It should be: Tuple<string, string> selectedTuple = (Tuple

Connecting a client to a TCP server using TLS 1.2

别等时光非礼了梦想. 提交于 2021-02-07 11:17:42
问题 I'm trying with no luck to connect a device to a .Net (4.5.2) server. It's a TCP connection opened by the device, that uses TLS 1.2. On the server side, I have a standard .Net implementation of a TCP Server: SslStream wrapped through DotNetty I cannot change anything on the device Any .Net client can successfully connect to my server using a secured TLS connection. It's working when trying with CURL too, so I've concluded my TCP server works fine. So I've compared (using Wireshark) what was

Why do primitive data types work without including the System namespace?

…衆ロ難τιáo~ 提交于 2021-02-07 11:17:01
问题 I read that all primitives fall under the System namespace. If I comment out using System , I would expect there to be a build error in my program. However, it is running successfully. Why is this? 回答1: It's because int is an alias for System.Int32 , and since the "Int32" is already prefixed with its namespace (ie. "fully qualified"), the syntax is legal without having to specify using System; at the top of your code. The MSDN snippet below describes this concept- Most C# applications begin

Is it safe to call Type.GetType with an untrusted type name?

六月ゝ 毕业季﹏ 提交于 2021-02-07 11:16:43
问题 I came across the following in a code review: Type type = Type.GetType(typeName); if (type == typeof(SomeKnownType)) DoSomething(...); // does not use type or typeName typeName originates from an AJAX request and is not validated. Does this pose any potential security issues? For example, is it possible for unexpected code to be executed, or for the entire application to crash (denial of service), as the result of loading arbitrary types from arbitrary assemblies? (I suppose some joker could

Is it safe to call Type.GetType with an untrusted type name?

元气小坏坏 提交于 2021-02-07 11:16:09
问题 I came across the following in a code review: Type type = Type.GetType(typeName); if (type == typeof(SomeKnownType)) DoSomething(...); // does not use type or typeName typeName originates from an AJAX request and is not validated. Does this pose any potential security issues? For example, is it possible for unexpected code to be executed, or for the entire application to crash (denial of service), as the result of loading arbitrary types from arbitrary assemblies? (I suppose some joker could

In ETAS INCA, is there a way to quickly retrieve an item from an arbitrary path in an ASAP2Project?

折月煮酒 提交于 2021-02-07 11:12:53
问题 I'm trying to connect to INCA through the .NET API in order to navigate the folder structure of an ASAP2 project. Specifically, I want to get an object that represents the "Target" folder that I've highlighted below. This is proving pretty tricky--The API provides a ton of classes that have the name "Folder" in them: Folder , Asap2ProjectFolder , and IncaFolder . So what do I need to know in order to retrieve the "Target" folder? 回答1: Your first impulse might be to think of the Target folder