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); }
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) }); }
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 }); }
public static bool IsContainsNullable(this FieldInfo fieldInfo) { NullabilityInfo nullInfo = _nullableContextHandler.Create(fieldInfo); return(IsContainsNullable(nullInfo)); }