c#-9.0

Custom Equality check for C# 9 records

送分小仙女□ 提交于 2021-02-18 21:30:08
问题 From what I understand, records are actually classes that implement their own equality check in a way that your object is value-driven and not reference driven. In short, for the record Foo that is implemented like so: var foo = new Foo { Value = "foo" } and var bar = new Foo { Value = "foo" } , the foo == bar expression will result in True , even though they have a different reference ( ReferenceEquals(foo, bar) // False ). Now with records, even though that in the article posted in .Net

.NET 5.0 Web API won't work with record featuring required properties

不问归期 提交于 2021-02-10 07:12:59
问题 I'm using a C# 9.0 record type as a binding model for a .NET 5.0 Web API project. Some of the properties are required. I'm using the record positional syntax, but am receiving errors. public record Mail( System.Guid? Id, [property: Required] string From, [property: Required] string[] Tos, [property: Required] string Subject, string[]? Ccs, string[]? Bccs, [property: Required] Content[] Contents, Attachment[]? Attachments ); This is then exposed as the binding model for my Index action: public

.NET 5.0 Web API won't work with record featuring required properties

妖精的绣舞 提交于 2021-02-10 07:10:57
问题 I'm using a C# 9.0 record type as a binding model for a .NET 5.0 Web API project. Some of the properties are required. I'm using the record positional syntax, but am receiving errors. public record Mail( System.Guid? Id, [property: Required] string From, [property: Required] string[] Tos, [property: Required] string Subject, string[]? Ccs, string[]? Bccs, [property: Required] Content[] Contents, Attachment[]? Attachments ); This is then exposed as the binding model for my Index action: public

JsonPropertyNameAttribute is not supported record from C#9? [duplicate]

耗尽温柔 提交于 2021-02-05 07:34:45
问题 This question already has an answer here : How do I target attributes for a record class? (1 answer) Closed last month . I want to use record with JsonPropertyName attribute, but it caused an error. This is not supported? Any workaround? public record QuoteResponse([JsonPropertyName("quotes")] IReadOnlyCollection<Quote>? Quotes); Error CS0592 Attribute 'JsonPropertyName' is not valid on this declaration type. It is only valid on 'property, indexer, field' declarations. 回答1: By default

Testing C# 9.0 in VS2019 - CS0518 IsExternalInit is not defined or imported … How do I define/import it?

走远了吗. 提交于 2021-02-04 05:15:45
问题 EDIT [Nov 29 2020] : .NET 5.0 is out now, but the solution below is still required if you're targetting .NET Standard 2.1 C# 9.0 is still under development. There are a couple references which lead me to believe it should be testable now (some of it, anyway). A Microsoft blog by Mr. Awesome himself, introducing the features. https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/ The language tracking page on github: https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature

C# 9.0 Support for Top-level programs in Visual Studio 2019

a 夏天 提交于 2021-01-28 19:51:46
问题 With the coming of C# 9.0 and .NET 5 a new feature is getting introduced called "Top-level programs". This functionality takes away a lot of the boilerplate code necessary to create a simple C# application by not having to wrap your code in the usual namespace/class/Main method, as explained in the Welcome to C# 9.0 blog To create a simple "Hello World" application the only required code for a Top-level program is the following (taken from the blog) using System; Console.WriteLine("Hello

Is there a common base class for records?

百般思念 提交于 2021-01-28 12:37:35
问题 In the .NET type system all reference types derive from System.Object , all value types from System.ValueType I think. Is there also a common base class all record types are derived from? If not, why not? 回答1: Is there also a common base class all record types are derived from? No, at least not anything specific to records. Records are just reference types. They can derive directly from System.Object or from other record types. If not, why not? They don't require special handling by the

Cannot find Main method using reflection with .NET 5 top level calls

一笑奈何 提交于 2020-12-31 05:02:59
问题 var pt = Type.GetType("<Program>$"); var m = pt.GetMethod("<Main>$", BindingFlags.Static); // m is null Okay, I grab the Program class, this works fine. But when I go to grab the Main method, system cannot find it, and it's not in pt.GetMembers() either. What's going on? 回答1: You just need to specify that you want to see non-public members: using System; using System.Reflection; var pt = Type.GetType("<Program>$"); var m = pt.GetMethod("<Main>$", BindingFlags.Static | BindingFlags.NonPublic);

Cannot find Main method using reflection with .NET 5 top level calls

亡梦爱人 提交于 2020-12-31 04:59:16
问题 var pt = Type.GetType("<Program>$"); var m = pt.GetMethod("<Main>$", BindingFlags.Static); // m is null Okay, I grab the Program class, this works fine. But when I go to grab the Main method, system cannot find it, and it's not in pt.GetMembers() either. What's going on? 回答1: You just need to specify that you want to see non-public members: using System; using System.Reflection; var pt = Type.GetType("<Program>$"); var m = pt.GetMethod("<Main>$", BindingFlags.Static | BindingFlags.NonPublic);

Cannot find Main method using reflection with .NET 5 top level calls

瘦欲@ 提交于 2020-12-31 04:57:30
问题 var pt = Type.GetType("<Program>$"); var m = pt.GetMethod("<Main>$", BindingFlags.Static); // m is null Okay, I grab the Program class, this works fine. But when I go to grab the Main method, system cannot find it, and it's not in pt.GetMembers() either. What's going on? 回答1: You just need to specify that you want to see non-public members: using System; using System.Reflection; var pt = Type.GetType("<Program>$"); var m = pt.GetMethod("<Main>$", BindingFlags.Static | BindingFlags.NonPublic);