static int Main(string[] args) { MethodInfo testMethod = typeof(TestType).GetMethod("TestMethod") !; NullabilityInfoContext nullabilityContext = new NullabilityInfoContext(); NullabilityInfo nullability = nullabilityContext.Create(testMethod.ReturnParameter); if (nullability.ReadState != NullabilityState.Nullable) { return(-1); } return(100); }
internal static bool IsRequired(ValidationMetadataProviderContext context) { var nullabilityContext = new NullabilityInfoContext(); var nullability = context.Key.MetadataKind switch { ModelMetadataKind.Parameter => nullabilityContext.Create(context.Key.ParameterInfo!), ModelMetadataKind.Property => nullabilityContext.Create(context.Key.PropertyInfo!), _ => null }; var isOptional = nullability != null && nullability.ReadState != NullabilityState.NotNull; return !isOptional; } }
private void InitializeTypeInformation() { Debug.Assert(ModelType != null); IsComplexType = !TypeDescriptor.GetConverter(ModelType).CanConvertFrom(typeof(string)); IsNullableValueType = Nullable.GetUnderlyingType(ModelType) != null; IsReferenceOrNullableType = !ModelType.IsValueType || IsNullableValueType; UnderlyingOrModelType = Nullable.GetUnderlyingType(ModelType) ?? ModelType; var collectionType = ClosedGenericMatcher.ExtractGenericInterface(ModelType, typeof(ICollection <>)); IsCollectionType = collectionType != null; var nullabilityContext = new NullabilityInfoContext(); var nullability = MetadataKind switch { ModelMetadataKind.Parameter => Identity.ParameterInfo != null?nullabilityContext.Create(Identity.ParameterInfo !) : null, ModelMetadataKind.Property => Identity.PropertyInfo != null?nullabilityContext.Create(Identity.PropertyInfo !) : null, _ => null }; NullabilityState = nullability?.ReadState ?? NullabilityState.Unknown; if (ModelType == typeof(string) || !typeof(IEnumerable).IsAssignableFrom(ModelType)) { // Do nothing, not Enumerable. } else if (ModelType.IsArray) { IsEnumerableType = true; ElementType = ModelType.GetElementType() !; } else { IsEnumerableType = true; var enumerableType = ClosedGenericMatcher.ExtractGenericInterface(ModelType, typeof(IEnumerable <>)); ElementType = enumerableType?.GenericTypeArguments[0] !; if (ElementType == null) { // ModelType implements IEnumerable but not IEnumerable<T>. ElementType = typeof(object); } Debug.Assert( ElementType != null, $"Unable to find element type for '{ModelType.FullName}' though IsEnumerableType is true."); } }
static int Main(string[] args) { MethodInfo testMethod = typeof(TestType).GetMethod("TestMethod") !; NullabilityInfoContext nullabilityContext = new NullabilityInfoContext(); try { nullabilityContext.Create(testMethod.ReturnParameter); return(-1); } catch (InvalidOperationException) { return(100); } }
private static CsTypeMember Create(FieldInfo fieldInfo) { CsLiteral?GetLiteral(Type fieldType) { if (!fieldInfo.IsLiteral) { return(null); } var constantValue = fieldInfo.GetRawConstantValue(); if (fieldType == typeof(string) || fieldType == typeof(int) || fieldType == typeof(bool) || fieldType.IsEnum) { return(new CsLiteral { Value = constantValue, Type = new CsType(fieldType) }); } return(null); } var context = new NullabilityInfoContext(); var info = context.Create(fieldInfo); return(new CsField { Name = fieldInfo.Name, IsStatic = fieldInfo.IsStatic, IsInherited = fieldInfo.DeclaringType != fieldInfo.ReflectedType, AccessModifier = GetAccessModifier(fieldInfo.IsPrivate, fieldInfo.IsFamily, fieldInfo.IsPublic, fieldInfo.IsAssembly), Attributes = fieldInfo.CustomAttributes .Select(x => new CsAttribute { Name = x.AttributeType.Name, OriginalType = x.AttributeType }) .ToArray(), Type = new CsType(fieldInfo.FieldType), Value = GetLiteral(fieldInfo.FieldType) }); }
public PropertyAsParameterInfo(PropertyInfo propertyInfo, NullabilityInfoContext?nullabilityContext = null) { Debug.Assert(null != propertyInfo); AttrsImpl = (ParameterAttributes)propertyInfo.Attributes; NameImpl = propertyInfo.Name; MemberImpl = propertyInfo; ClassImpl = propertyInfo.PropertyType; // It is not a real parameter in the delegate, so, // not defining a real position. PositionImpl = -1; _nullabilityContext = nullabilityContext ?? new NullabilityInfoContext(); _underlyingProperty = propertyInfo; }
private static CsProperty Create(PropertyInfo propertyInfo) { var getMethod = propertyInfo.GetGetMethod(); if (getMethod == null) { return(null); } // ignoring overridden props if (getMethod != getMethod.GetBaseDefinition()) { return(null); } var context = new NullabilityInfoContext(); var info = context.Create(propertyInfo); return(new CsProperty { Name = propertyInfo.Name, IsStatic = getMethod.IsStatic, IsInherited = propertyInfo.DeclaringType != propertyInfo.ReflectedType, AccessModifier = GetAccessModifier(getMethod.IsPrivate, getMethod.IsFamily, getMethod.IsPublic, getMethod.IsAssembly), Attributes = propertyInfo.CustomAttributes .Select(x => new CsAttribute { Name = x.AttributeType.Name, OriginalType = x.AttributeType }) .ToArray(), Type = new CsType(propertyInfo.PropertyType), IsNullable = info.ReadState == NullabilityState.Nullable }); }
static NullableNatashaExtension() { _nullableContextHandler = new(); }
static NullableMemberReverser() { _nullableContextHandler = new(); }