2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 03:32:57 +00:00
This commit is contained in:
Ahmed Zamil 2024-11-10 18:17:03 +03:00
parent 4603f66499
commit 0bed03abe3
7 changed files with 136 additions and 115 deletions

View File

@ -1,28 +1,23 @@
namespace System.Runtime.CompilerServices
{
[AttributeUsage(
AttributeTargets.Class |
AttributeTargets.Event |
AttributeTargets.Field |
AttributeTargets.GenericParameter |
AttributeTargets.Parameter |
AttributeTargets.Property |
AttributeTargets.ReturnValue,
AllowMultiple = false,
Inherited = false)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, Inherited = false)]
public sealed class NullableAttribute : Attribute
{
public readonly byte[] Flags;
//public readonly byte Flag;
/// <summary>Flags specifying metadata related to nullable reference types.</summary>
public readonly byte[] NullableFlags;
public NullableAttribute(byte flag)
/// <summary>Initializes the attribute.</summary>
/// <param name="value">The flags value.</param>
public NullableAttribute(byte value)
{
//Flag = flag;
Flags = new byte[] { flag };
NullableFlags = new[] { value };
}
public NullableAttribute(byte[] flags)
/// <summary>Initializes the attribute.</summary>
/// <param name="value">The flags value.</param>
public NullableAttribute(byte[] value)
{
Flags = flags;
NullableFlags = value;
}
}
}

View File

@ -1,19 +1,16 @@
namespace System.Runtime.CompilerServices
{
[System.AttributeUsage(
AttributeTargets.Class |
AttributeTargets.Delegate |
AttributeTargets.Interface |
AttributeTargets.Method |
AttributeTargets.Struct,
AllowMultiple = false,
Inherited = false)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, Inherited = false)]
public sealed class NullableContextAttribute : Attribute
{
/// <summary>Flag specifying metadata related to nullable reference types.</summary>
public readonly byte Flag;
public NullableContextAttribute(byte flag)
/// <summary>Initializes the attribute.</summary>
/// <param name="value">The flag value.</param>
public NullableContextAttribute(byte value)
{
Flag = flag;
Flag = value;
}
}
}

View File

@ -5,7 +5,7 @@
<Copyright>Ahmed Kh. Zamil</Copyright>
<PackageProjectUrl>http://www.esiur.com</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.4.8</Version>
<Version>2.4.9</Version>
<RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl>
<Authors>Ahmed Kh. Zamil</Authors>
<AssemblyVersion></AssemblyVersion>
@ -56,14 +56,14 @@
<ItemGroup>
<Compile Remove="Data\NullableAttribute.cs" />
<Compile Remove="Data\NullableContextAttribute.cs" />
<Compile Remove="Data\NullabilityInfo.cs" />
<Compile Remove="Data\NullabilityInfoContext.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Data\NullableAttribute.cs" />
<None Include="Data\NullableContextAttribute.cs" />
<None Include="Data\NullabilityInfo.cs" />
<None Include="Data\NullabilityInfoContext.cs" />
<None Include="LICENSE" Pack="true" PackagePath=""></None>
<None Include="README.md" Pack="true" PackagePath="" />
</ItemGroup>

View File

@ -83,31 +83,36 @@ public class EventTemplate : MemberTemplate
var annotationAttr = ei.GetCustomAttribute<AnnotationAttribute>(true);
var listenableAttr = ei.GetCustomAttribute<ListenableAttribute>(true);
evtType.Nullable = new NullabilityInfoContext().Create(ei).ReadState is NullabilityState.Nullable;
//evtType.Nullable = new NullabilityInfoContext().Create(ei).ReadState is NullabilityState.Nullable;
//var nullableAttr = ei.GetCustomAttribute<NullableAttribute>(true);
//var nullableContextAttr = ei.GetCustomAttribute<NullableContextAttribute>(true);
var nullableAttr = ei.GetCustomAttributes().FirstOrDefault(x => x.GetType().Name == "System.Runtime.CompilerServices.NullableAttribute");// .GetCustomAttribute<NullableAttribute>(true);
var nullableContextAttr = ei.GetCustomAttributes().FirstOrDefault(x => x.GetType().Name == "System.Runtime.CompilerServices.NullableContextAttribute");// ei.GetCustomAttribute<NullableContextAttribute>(true);
var nullableAttrFlags = (nullableAttr?.GetType().GetField("NullableFlags").GetValue(nullableAttr) as byte[] ?? new byte[0]).ToList();
var nullableContextAttrFlag = (byte)(nullableContextAttr?.GetType().GetField("Flag").GetValue(nullableContextAttr) ?? (byte)0);
//var flags = nullableAttr?.Flags?.ToList() ?? new List<byte>();
//var flags = ((byte[])nullableAttr?.NullableFlags ?? new byte[0]).ToList();
//// skip the eventHandler class
//if (flags.Count > 1)
// flags = flags.Skip(1).ToList();
// skip the eventHandler class
if (nullableAttrFlags.Count > 1)
nullableAttrFlags = nullableAttrFlags.Skip(1).ToList();
//if (nullableContextAttr?.Flag == 2)
//{
// if (flags.Count == 1)
// evtType.SetNotNull(flags.FirstOrDefault());
// else
// evtType.SetNotNull(flags);
//}
//else
//{
// if (flags.Count == 1)
// evtType.SetNull(flags.FirstOrDefault());
// else
// evtType.SetNull(flags);
//}
if (nullableContextAttrFlag == 2)
{
if (nullableAttrFlags.Count == 1)
evtType.SetNotNull(nullableAttrFlags.FirstOrDefault());
else
evtType.SetNotNull(nullableAttrFlags);
}
else
{
if (nullableAttrFlags.Count == 1)
evtType.SetNull(nullableAttrFlags.FirstOrDefault());
else
evtType.SetNull(nullableAttrFlags);
}
var et = new EventTemplate(typeTemplate, index, customName ?? ei.Name, ei.DeclaringType != type, evtType);
et.EventInfo = ei;

View File

@ -91,41 +91,56 @@ public class FunctionTemplate : MemberTemplate
var annotationAttr = mi.GetCustomAttribute<AnnotationAttribute>(true);
var nullabilityInfoContext = new NullabilityInfoContext();
rtType.Nullable = nullabilityInfoContext.Create(mi.ReturnParameter).WriteState is NullabilityState.Nullable;
//var nullabilityInfoContext = new NullabilityInfoContext();
//rtType.Nullable = nullabilityInfoContext.Create(mi.ReturnParameter).WriteState is NullabilityState.Nullable;
//var nullableAttr = mi.GetCustomAttribute<NullableAttribute>(true);
//var nullableContextAttr = mi.GetCustomAttribute<NullableContextAttribute>(true);
var nullableAttr = mi.GetCustomAttributes(true).FirstOrDefault(x => x.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
var nullableContextAttr = mi.GetCustomAttributes(true).FirstOrDefault(x => x.GetType().FullName == "System.Runtime.CompilerServices.NullableContextAttribute");
//var flags = nullableAttr?.Flags?.ToList() ?? new List<byte>();
var nullableAttrFlags = (nullableAttr?.GetType().GetField("NullableFlags").GetValue(nullableAttr) as byte[] ?? new byte[0]).ToList();
var nullableContextAttrFlag = (byte)(nullableContextAttr?.GetType().GetField("Flag").GetValue(nullableContextAttr) ?? (byte)0);
//var flags = ((byte[])nullableAttr?.NullableFlags ?? new byte[0]).ToList();
//var rtNullableAttr = mi.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(NullableAttribute), true).FirstOrDefault() as NullableAttribute;
//var rtNullableContextAttr = mi.ReturnTypeCustomAttributes
var rtNullableAttr = mi.ReturnTypeCustomAttributes.GetCustomAttributes(true).FirstOrDefault(x => x.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
// var rtNullableContextAttr = mi.ReturnTypeCustomAttributes
// .GetCustomAttributes(typeof(NullableContextAttribute), true)
// .FirstOrDefault() as NullableContextAttribute
// .FirstOrDefault() as NullableContextAttribute
// ?? nullableContextAttr;
var rtNullableContextAttr = mi.ReturnTypeCustomAttributes
.GetCustomAttributes(true).FirstOrDefault(x=>x.GetType().Name == "NullableContextAttribute")
?? nullableContextAttr;
var rtNullableAttrFlags = (rtNullableAttr?.GetType().GetField("NullableFlags").GetValue(rtNullableAttr) as byte[] ?? new byte[0]).ToList();
var rtNullableContextAttrFlag = (byte)(rtNullableContextAttr?.GetType().GetField("Flag").GetValue(rtNullableContextAttr) ?? (byte)0);
//var rtFlags = rtNullableAttr?.Flags?.ToList() ?? new List<byte>();
//var rtFlags = ((byte[])rtNullableAttr?.NullableFlags ?? new byte[0]).ToList();
//if (rtFlags.Count > 0 && genericRtType == typeof(AsyncReply<>))
// rtFlags.RemoveAt(0);
if (rtNullableAttrFlags.Count > 0 && genericRtType == typeof(AsyncReply<>))
rtNullableAttrFlags.RemoveAt(0);
//if (rtNullableContextAttr?.Flag == 2)
//{
// if (rtFlags.Count == 1)
// rtType.SetNotNull(rtFlags.FirstOrDefault());
// else
// rtType.SetNotNull(rtFlags);
//}
//else
//{
// if (rtFlags.Count == 1)
// rtType.SetNull(rtFlags.FirstOrDefault());
// else
// rtType.SetNull(rtFlags);
//}
if (rtNullableContextAttrFlag == 2)
{
if (rtNullableAttrFlags.Count == 1)
rtType.SetNotNull(rtNullableAttrFlags.FirstOrDefault());
else
rtType.SetNotNull(rtNullableAttrFlags);
}
else
{
if (rtNullableAttrFlags.Count == 1)
rtType.SetNull(rtNullableAttrFlags.FirstOrDefault());
else
rtType.SetNull(rtNullableAttrFlags);
}
var args = mi.GetParameters();
@ -142,28 +157,34 @@ public class FunctionTemplate : MemberTemplate
if (argType == null)
throw new Exception($"Unsupported type `{x.ParameterType}` in method `{type.Name}.{mi.Name}` parameter `{x.Name}`");
argType.Nullable = nullabilityInfoContext.Create(x).WriteState is NullabilityState.Nullable;
//argType.Nullable = nullabilityInfoContext.Create(x).WriteState is NullabilityState.Nullable;
//var argNullableAttr = x.GetCustomAttribute<NullableAttribute>(true);
//var argNullableContextAttr = x.GetCustomAttribute<NullableContextAttribute>(true) ?? nullableContextAttr;
var argNullableAttr = x.GetCustomAttributes(true).FirstOrDefault(x => x.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
var argNullableContextAttr = x.GetCustomAttributes(true).FirstOrDefault(x => x.GetType().FullName == "System.Runtime.CompilerServices.NullableContextAttr");
//var argFlags = argNullableAttr?.Flags?.ToList() ?? new List<byte>();
//var argFlags = ((byte[])argNullableAttr?.NullableFlags ?? new byte[0]).ToList();
var argNullableAttrFlags = (argNullableAttr?.GetType().GetField("NullableFlags").GetValue(argNullableAttr) as byte[] ?? new byte[0]).ToList();
var argNullableContextAttrFlag = (byte)(argNullableAttr?.GetType().GetField("Flag").GetValue(argNullableAttr) ?? (byte)0);
//if (argNullableContextAttr?.Flag == 2)
//{
// if (argFlags.Count == 1)
// argType.SetNotNull(argFlags.FirstOrDefault());
// else
// argType.SetNotNull(argFlags);
//}
//else
//{
// if (rtFlags.Count == 1)
// argType.SetNull(argFlags.FirstOrDefault());
// else
// argType.SetNull(argFlags);
//}
if (argNullableContextAttrFlag == 2)
{
if (argNullableAttrFlags.Count == 1)
argType.SetNotNull(argNullableAttrFlags.FirstOrDefault());
else
argType.SetNotNull(argNullableAttrFlags);
}
else
{
if (argNullableAttrFlags.Count == 1)
argType.SetNull(argNullableAttrFlags.FirstOrDefault());
else
argType.SetNull(argNullableAttrFlags);
}
return new ArgumentTemplate()
{

View File

@ -150,8 +150,6 @@ public class PropertyTemplate : MemberTemplate
this.ValueType = valueType;
}
public static PropertyTemplate MakePropertyTemplate(Type type, PropertyInfo pi, byte index = 0, string customName = null, TypeTemplate typeTemplate = null)
{
var genericPropType = pi.PropertyType.IsGenericType ? pi.PropertyType.GetGenericTypeDefinition() : null;
@ -166,32 +164,36 @@ public class PropertyTemplate : MemberTemplate
var annotationAttr = pi.GetCustomAttribute<AnnotationAttribute>(true);
var storageAttr = pi.GetCustomAttribute<StorageAttribute>(true);
var nullabilityContext = new NullabilityInfoContext();
propType.Nullable = nullabilityContext.Create(pi).ReadState is NullabilityState.Nullable;
//var nullabilityContext = new NullabilityInfoContext();
//propType.Nullable = nullabilityContext.Create(pi).ReadState is NullabilityState.Nullable;
var nullableAttr = pi.GetCustomAttributes(true).FirstOrDefault(x => x.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
var nullableContextAttr = pi.GetCustomAttributes(true).FirstOrDefault(x => x.GetType().FullName == "System.Runtime.CompilerServices.NullableContextAttribute");
var nullableAttrFlags = (nullableAttr?.GetType().GetField("NullableFlags").GetValue(nullableAttr) as byte[] ?? new byte[0]).ToList();
var nullableContextAttrFlag = (byte)(nullableContextAttr?.GetType().GetField("Flag").GetValue(nullableContextAttr) ?? (byte)0);
// var nullableContextAttr = pi.GetCustomAttribute<NullableContextAttribute>(true);
// var nullableAttr = pi.GetCustomAttribute<NullableAttribute>(true);
//var nullableAttr = pi.GetCustomAttribute<NullableAttribute>(true);
//var flags = ((byte[]) nullableAttr?.NullableFlags ?? new byte[0]).ToList();
// var flags = nullableAttr?.Flags?.ToList() ?? new List<byte>();
if (nullableAttrFlags.Count > 0 && genericPropType == typeof(DistributedPropertyContext<>))
nullableAttrFlags.RemoveAt(0);
// if (flags.Count > 0 && genericPropType == typeof(DistributedPropertyContext<>))
// flags.RemoveAt(0);
// if (nullableContextAttr?.Flag == 2)
// {
// if (flags.Count == 1)
// propType.SetNotNull(flags.FirstOrDefault());
// else
// propType.SetNotNull(flags);
// }
// else
// {
// if (flags.Count == 1)
// propType.SetNull(flags.FirstOrDefault());
// else
// propType.SetNull(flags);
// }
if (nullableContextAttrFlag == 2)
{
if (nullableAttrFlags.Count == 1)
propType.SetNotNull(nullableAttrFlags.FirstOrDefault());
else
propType.SetNotNull(nullableAttrFlags);
}
else
{
if (nullableAttrFlags.Count == 1)
propType.SetNull(nullableAttrFlags.FirstOrDefault());
else
propType.SetNull(nullableAttrFlags);
}
var pt = new PropertyTemplate(typeTemplate, index, customName ?? pi.Name, pi.DeclaringType != type, propType);

View File

@ -120,6 +120,7 @@ public partial class MyService
[Export] public MyRecord Record => new MyRecord() { Id = 33, Name = "Test", Score = 99.33 };
[Export] public MyRecord? RecordNullable => new MyRecord() { Id = 33, Name = "Test Nullable", Score = 99.33 };
[Export] public List<int> IntList => new List<int>() { 1, 2, 3, 4, 5 };