添加可空判断方法

This commit is contained in:
Kimdiego2098
2024-01-25 14:02:37 +08:00
parent d34361b6dd
commit ff68030231
2 changed files with 109 additions and 7 deletions

View File

@@ -7,6 +7,8 @@
@using ThingsGateway.Core.Extension.Json
@using ThingsGateway.Gateway.Application
@using TouchSocket.Core
@using ThingsGateway.Foundation.TypeExtension
@namespace ThingsGateway.Gateway.Blazor
@inject UserResoures UserResoures
@inherits BaseComponentBase
@@ -18,7 +20,7 @@
<MTooltip Disabled=@(string.IsNullOrEmpty(Remark)) Bottom Context="tip">
<ActivatorContent>
<MSelect Class="" Items=@(new List<bool>(){true,false})
Value=@Value ValueChanged=@ValueChanged Label=@(Description) ValueExpression="()=>Value" Required=!Type.IsNullableType() Clearable=Type.IsNullableType()
Value=@Value ValueChanged=@ValueChanged Label=@(Description) ValueExpression="()=>Value" Required=!Type.IsNullable() Clearable=Type.IsNullable()
MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
ItemText=@((u) =>u.ToString()) @attributes="@tip.Attrs"
ItemValue=@(u =>u.ToString())
@@ -41,7 +43,7 @@
TimeFormat="TimeFormat.Hr24"
ViewType="DateTimePickerViewType.Compact">
<ActivatorContent Context="dt">
<MTextField Value=@Value ValueChanged=@ValueChanged Label=@Description Required=!Type.IsNullableType() Clearable=Type.IsNullableType()
<MTextField Value=@Value ValueChanged=@ValueChanged Label=@Description Required=!Type.IsNullable() Clearable=Type.IsNullable()
Class="my-1 mx-2 " ValueExpression="()=>Value"
Filled Outlined
Readonly Dense
@@ -60,7 +62,7 @@
<MTooltip Disabled=@(string.IsNullOrEmpty(Remark)) Bottom Context="tip">
<ActivatorContent>
<MSelect Class="" Items=@(Type.PropertyType.GetEnumList())
Value=@Value ValueChanged=@ValueChanged Label=@(Description) ValueExpression="()=>Value" Required=!Type.IsNullableType() Clearable=Type.IsNullableType()
Value=@Value ValueChanged=@ValueChanged Label=@(Description) ValueExpression="()=>Value" Required=!Type.IsNullable() Clearable=Type.IsNullable()
MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
ItemText=@(u =>u.Description) @attributes="@tip.Attrs"
ItemValue=@(u =>u.Name)
@@ -78,7 +80,7 @@
<ActivatorContent>
<MTextField Dense @attributes="@tip.Attrs"
Type="number" NumberProps=@(prop => { prop.Step = 1; })
Outlined HideDetails="@("auto")" Color="primary" ValueExpression="()=>Value" Required=!Type.IsNullableType() Clearable=Type.IsNullableType()
Outlined HideDetails="@("auto")" Color="primary" ValueExpression="()=>Value" Required=!Type.IsNullable() Clearable=Type.IsNullable()
Value=@Value ValueChanged=@ValueChanged Label="@(Description)" />
</ActivatorContent>
@@ -93,7 +95,7 @@
<ActivatorContent>
<MTextField Dense @attributes="@tip.Attrs"
Type="number" NumberProps=@(prop => { prop.Step = 0.1m; })
Outlined HideDetails="@("auto")" Color="primary" ValueExpression="()=>Value" Required=!Type.IsNullableType() Clearable=Type.IsNullableType()
Outlined HideDetails="@("auto")" Color="primary" ValueExpression="()=>Value" Required=!Type.IsNullable() Clearable=Type.IsNullable()
Value=@Value ValueChanged=@ValueChanged Label="@(Description)" />
</ActivatorContent>
@@ -111,7 +113,7 @@
<MTooltip Disabled=@(string.IsNullOrEmpty(Remark)) Bottom Context="tip">
<ActivatorContent>
<MTextarea Dense @attributes="@tip.Attrs" AutoGrow
Outlined HideDetails="@("auto")" Color="primary" ValueExpression="()=>Value" Required=!Type.IsNullableType() Clearable=Type.IsNullableType()
Outlined HideDetails="@("auto")" Color="primary" ValueExpression="()=>Value" Required=!Type.IsNullable() Clearable=Type.IsNullable()
Value=@Value ValueChanged=@ValueChanged Label="@(Description)" />
</ActivatorContent>
@@ -133,7 +135,7 @@
<MTooltip Disabled=@(string.IsNullOrEmpty(Remark)) Bottom Context="tip">
<ActivatorContent>
<MTextField Dense @attributes="@tip.Attrs"
Outlined HideDetails="@("auto")" Color="primary" ValueExpression="()=>Value" Required=!Type.IsNullableType() Clearable=Type.IsNullableType()
Outlined HideDetails="@("auto")" Color="primary" ValueExpression="()=>Value" Required=!Type.IsNullable() Clearable=Type.IsNullable()
Value=@Value ValueChanged=@ValueChanged Label="@(Description)" />
</ActivatorContent>

View File

@@ -0,0 +1,100 @@
#region copyright
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
// 使用文档https://diego2098.gitee.io/thingsgateway-docs/
// QQ群605534569
//------------------------------------------------------------------------------
#endregion
using System.Collections.ObjectModel;
using System.Reflection;
namespace ThingsGateway.Foundation.TypeExtension;
/// <summary>
/// TypeExtension
/// </summary>
public static class TypeExtension
{
/// <summary>
/// IsNullable
/// </summary>
public static bool IsNullable(this PropertyInfo property)
{
#if NET6_0_OR_GREATER
return new NullabilityInfoContext().Create(property).WriteState is NullabilityState.Nullable;
#else
return IsNullableHelper(property.PropertyType, property.DeclaringType, property.CustomAttributes);
#endif
}
/// <summary>
/// IsNullable
/// </summary>
public static bool IsNullable(this FieldInfo field)
{
#if NET6_0_OR_GREATER
return new NullabilityInfoContext().Create(field).WriteState is NullabilityState.Nullable;
#else
return IsNullableHelper(field.FieldType, field.DeclaringType, field.CustomAttributes);
#endif
}
/// <summary>
/// IsNullable
/// </summary>
public static bool IsNullable(this ParameterInfo parameter)
{
#if NET6_0_OR_GREATER
return new NullabilityInfoContext().Create(parameter).WriteState is NullabilityState.Nullable;
#else
return IsNullableHelper(parameter.ParameterType, parameter.Member, parameter.CustomAttributes);
#endif
}
private static bool IsNullableHelper(Type memberType, MemberInfo? declaringType, IEnumerable<CustomAttributeData> customAttributes)
{
if (memberType.IsValueType)
return Nullable.GetUnderlyingType(memberType) != null;
var nullable = customAttributes
.FirstOrDefault(x => x.AttributeType.FullName == "System.Runtime.CompilerServices.NullableAttribute");
if (nullable != null && nullable.ConstructorArguments.Count == 1)
{
var attributeArgument = nullable.ConstructorArguments[0];
if (attributeArgument.ArgumentType == typeof(byte[]))
{
var args = (ReadOnlyCollection<CustomAttributeTypedArgument>)attributeArgument.Value!;
if (args.Count > 0 && args[0].ArgumentType == typeof(byte))
{
return (byte)args[0].Value! == 2;
}
}
else if (attributeArgument.ArgumentType == typeof(byte))
{
return (byte)attributeArgument.Value! == 2;
}
}
for (var type = declaringType; type != null; type = type.DeclaringType)
{
var context = type.CustomAttributes
.FirstOrDefault(x => x.AttributeType.FullName == "System.Runtime.CompilerServices.NullableContextAttribute");
if (context != null &&
context.ConstructorArguments.Count == 1 &&
context.ConstructorArguments[0].ArgumentType == typeof(byte))
{
return (byte)context.ConstructorArguments[0].Value! == 2;
}
}
// Couldn't find a suitable attribute
return false;
}
}