c#-9.0

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

牧云@^-^@ 提交于 2020-12-31 04:56:09
问题 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);

How do I target attributes for a record class?

隐身守侯 提交于 2020-12-27 06:31:51
问题 When defining a record class, how do I target the attributes to the parameter, field or property? For instance, I would like to use JsonIgnore but this doesn't compile as it has an attribute usage restriction to the field or property: record Person(string FirstName, string LastName, [JsonIgnore] int Age); 回答1: To target the various parts of the expanded class, use the appropriate attribute target. For instance: // Target the property, use `property` record Person(string FirstName, string

How do I target attributes for a record class?

孤人 提交于 2020-12-27 06:30:39
问题 When defining a record class, how do I target the attributes to the parameter, field or property? For instance, I would like to use JsonIgnore but this doesn't compile as it has an attribute usage restriction to the field or property: record Person(string FirstName, string LastName, [JsonIgnore] int Age); 回答1: To target the various parts of the expanded class, use the appropriate attribute target. For instance: // Target the property, use `property` record Person(string FirstName, string

How do I target attributes for a record class?

对着背影说爱祢 提交于 2020-12-27 06:29:26
问题 When defining a record class, how do I target the attributes to the parameter, field or property? For instance, I would like to use JsonIgnore but this doesn't compile as it has an attribute usage restriction to the field or property: record Person(string FirstName, string LastName, [JsonIgnore] int Age); 回答1: To target the various parts of the expanded class, use the appropriate attribute target. For instance: // Target the property, use `property` record Person(string FirstName, string

What is the right way to add comments with C#9 record?

℡╲_俬逩灬. 提交于 2020-12-13 03:47:46
问题 What is the right way to add comments with C# 9 record with Nullable enabled? If I try public record Person { /// <summary> /// Gets the first name. /// </summary> public string FirstName { get; init; } /// <summary> /// Gets the last name. /// </summary> public string LastName { get; init; } } I get warning. Non-nullable property 'FirstName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. If I try public record Person(string firstName,

What is the right way to add comments with C#9 record?

拥有回忆 提交于 2020-12-13 03:47:30
问题 What is the right way to add comments with C# 9 record with Nullable enabled? If I try public record Person { /// <summary> /// Gets the first name. /// </summary> public string FirstName { get; init; } /// <summary> /// Gets the last name. /// </summary> public string LastName { get; init; } } I get warning. Non-nullable property 'FirstName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. If I try public record Person(string firstName,

Init + private set accessors on the same property?

穿精又带淫゛_ 提交于 2020-12-12 06:49:54
问题 Is it possible to use a public init accessor and a private setter on the same property? Currently I get error CS1007 "Property accessor already defined". public record Stuff { public int MyProperty { get; init; private set; } // Error public void SetMyProperty(int value) => MyProperty = value; } var stuff = new Stuff { MyProperty = 3, // Using the init accessor }; stuff.SetMyProperty(4); // Using the private setter (indirectly) My best guess would be to use a private member variable, a

How do I get the Reflection TypeInfo of a C# 9 program that use Top-level statements?

∥☆過路亽.° 提交于 2020-12-12 05:39:42
问题 Assume I have a simple script writing in C# 9 like this: using System; using System.IO; // What to put in the ??? var exeFolder = Path.GetDirectoryName(typeof(???).Assembly.Location); Before, with the full program, we can use the Main class as an "indicator" class. this and this.GetType() is not available because technically it's inside a static method. How do I get it now? A workaround I thought of while typing the question is Assembly.GetCallingAssembly() : var exeFolder = Path

Some C# 9 features not available after upgrading Asp.Net Core 3.1 app to .Net 5

烂漫一生 提交于 2020-12-12 05:39:32
问题 I've upgraded an Asp.Net Core 3.1 (MVC) to .Net 5 by modifying the corresponding *.csproj file to this: <TargetFramework>net5.0</TargetFramework> <LangVersion>9.0</LangVersion> Now I can use the C# 9 target typing feature... string s = new('c', 3); // compiles fine ...but I can't create a record class: public data class User { // IDE1007 The name 'data' does not exist in the current context. } Am I missing something here? 回答1: According to record type specs, you should use public record User

Some C# 9 features not available after upgrading Asp.Net Core 3.1 app to .Net 5

你离开我真会死。 提交于 2020-12-12 05:38:21
问题 I've upgraded an Asp.Net Core 3.1 (MVC) to .Net 5 by modifying the corresponding *.csproj file to this: <TargetFramework>net5.0</TargetFramework> <LangVersion>9.0</LangVersion> Now I can use the C# 9 target typing feature... string s = new('c', 3); // compiles fine ...but I can't create a record class: public data class User { // IDE1007 The name 'data' does not exist in the current context. } Am I missing something here? 回答1: According to record type specs, you should use public record User