nullablereferencetypes

C#'s can't make `notnull` type nullable

亡梦爱人 提交于 2019-12-23 07:55:17
问题 I'm trying to create a type similar to Rust's Result or Haskell's Either and I've got this far: public struct Result<TResult, TError> where TResult : notnull where TError : notnull { private readonly OneOf<TResult, TError> Value; public Result(TResult result) => Value = result; public Result(TError error) => Value = error; public static implicit operator Result<TResult, TError>(TResult result) => new Result<TResult, TError>(result); public static implicit operator Result<TResult, TError>

How to use .NET reflection to check for nullable reference type

百般思念 提交于 2019-12-18 19:20:17
问题 C# 8.0 introduces nullable reference types. Here's a simple class with a nullable property: public class Foo { public String? Bar { get; set; } } Is there a way to check a class property uses a nullable reference type via reflection? 回答1: This appears to work, at least on the types I've tested it with. You need to pass the PropertyInfo for the property you're interested in, and also the Type which that property is defined on ( not a derived or parent type - it has to be the exact type):

How to detect whether a type can be nullable at runtime?

元气小坏坏 提交于 2019-12-05 02:09:20
问题 I'm trying to detect whether a type can be nullable or not at runtime to convert that type to the corresponding GraphQL type so that, for example: With nullable reference types enabled : string is converted to String! string? is converted to String With nullable reference types disabled : string is converted to String NonNull<string> is converted to String! ( NonNull is a custom library type) I'm having trouble adapting the code that detected the nullability of a type: bool isNullable =

How to detect whether a type can be nullable at runtime?

别来无恙 提交于 2019-12-03 21:08:15
I'm trying to detect whether a type can be nullable or not at runtime to convert that type to the corresponding GraphQL type so that, for example: With nullable reference types enabled : string is converted to String! string? is converted to String With nullable reference types disabled : string is converted to String NonNull<string> is converted to String! ( NonNull is a custom library type) I'm having trouble adapting the code that detected the nullability of a type: bool isNullable = !typeInfo.IsValueType; How can I change it so that it works with nullable reference types both enabled and

Non-nullable string type, how to use with Asp.Net Core options

大城市里の小女人 提交于 2019-12-01 01:28:20
问题 MS states Express your design intent more clearly with nullable and non-nullable reference types. My intent is to express, that properties Issuer and Audience in my JwtOptions are never null. Which is very reasonable intent for consumers of these options, is not? These not null values are ensured by Asp.Net Core validation described below. But if JwtOptions has not all properties initialized by the constructor, so C# 8 compiler reports Warning CS8618 Non-nullable property 'Issuer' is

What does null! statement mean?

与世无争的帅哥 提交于 2019-11-28 18:33:01
I've recently seen the following code: public class Person { //line 1 public string FirstName { get; } //line 2 public string LastName { get; } = null!; //assign null is possible public string? MiddleName {get; } = null; public Person(string firstName, string lastName, string middleName) { FirstName = firstName; LastName = lastName; MiddleName = middleName; } public Person(string firstName, string lastName) { FirstName = firstName; LastName = lastName; MiddleName = null; } } Basically I try to dig into new c# 8 features. One of them is NullableReferenceTypes . Actually there're a lot of

How to enable Nullable Reference Types feature of C# 8.0 for the whole project

扶醉桌前 提交于 2019-11-27 12:41:31
问题 According to the C# 8 announcement video the "nullable reference types" feature can be enabled for the whole project. But how to enable it for the project? I did not find any new appropriate option in the Project Properties window in Visual Studio 2019 Preview 1. Can it be enabled for 'legacy' .csproj projects if the C# language version is changed to 8.0? 回答1: In Visual Studio 16.2 (from preview 1) the property name changed to Nullable which is simpler and aligns with the command line

Nullable reference types with generic return type

≡放荡痞女 提交于 2019-11-27 03:03:51
问题 I'm playing around a bit with the new C# 8 nullable reference types feature, and while refactoring my code I came upon this (simplified) method: public T Get<T>(string key) { var wrapper = cacheService.Get(key); return wrapper.HasValue ? Deserialize<T>(wrapper) : default; } Now, this gives a warning Possible null reference return which is logical, since default(T) will give null for all reference types. At first I thought I would change it to the following: public T? Get<T>(string key) But

What does null! statement mean?

我只是一个虾纸丫 提交于 2019-11-26 12:24:43
问题 I\'ve recently seen the following code: public class Person { //line 1 public string FirstName { get; } //line 2 public string LastName { get; } = null!; //assign null is possible public string? MiddleName {get; } = null; public Person(string firstName, string lastName, string middleName) { FirstName = firstName; LastName = lastName; MiddleName = middleName; } public Person(string firstName, string lastName) { FirstName = firstName; LastName = lastName; MiddleName = null; } } Basically I try