2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 11:32:59 +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 namespace System.Runtime.CompilerServices
{ {
[AttributeUsage( [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, Inherited = false)]
AttributeTargets.Class |
AttributeTargets.Event |
AttributeTargets.Field |
AttributeTargets.GenericParameter |
AttributeTargets.Parameter |
AttributeTargets.Property |
AttributeTargets.ReturnValue,
AllowMultiple = false,
Inherited = false)]
public sealed class NullableAttribute : Attribute public sealed class NullableAttribute : Attribute
{ {
public readonly byte[] Flags; /// <summary>Flags specifying metadata related to nullable reference types.</summary>
//public readonly byte Flag; 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; NullableFlags = new[] { value };
Flags = new byte[] { flag };
} }
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 namespace System.Runtime.CompilerServices
{ {
[System.AttributeUsage( [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, Inherited = false)]
AttributeTargets.Class |
AttributeTargets.Delegate |
AttributeTargets.Interface |
AttributeTargets.Method |
AttributeTargets.Struct,
AllowMultiple = false,
Inherited = false)]
public sealed class NullableContextAttribute : Attribute public sealed class NullableContextAttribute : Attribute
{ {
/// <summary>Flag specifying metadata related to nullable reference types.</summary>
public readonly byte Flag; 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> <Copyright>Ahmed Kh. Zamil</Copyright>
<PackageProjectUrl>http://www.esiur.com</PackageProjectUrl> <PackageProjectUrl>http://www.esiur.com</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.4.8</Version> <Version>2.4.9</Version>
<RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl> <RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl>
<Authors>Ahmed Kh. Zamil</Authors> <Authors>Ahmed Kh. Zamil</Authors>
<AssemblyVersion></AssemblyVersion> <AssemblyVersion></AssemblyVersion>
@ -56,14 +56,14 @@
<ItemGroup> <ItemGroup>
<Compile Remove="Data\NullableAttribute.cs" /> <Compile Remove="Data\NullabilityInfo.cs" />
<Compile Remove="Data\NullableContextAttribute.cs" /> <Compile Remove="Data\NullabilityInfoContext.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Data\NullableAttribute.cs" /> <None Include="Data\NullabilityInfo.cs" />
<None Include="Data\NullableContextAttribute.cs" /> <None Include="Data\NullabilityInfoContext.cs" />
<None Include="LICENSE" Pack="true" PackagePath=""></None> <None Include="LICENSE" Pack="true" PackagePath=""></None>
<None Include="README.md" Pack="true" PackagePath="" /> <None Include="README.md" Pack="true" PackagePath="" />
</ItemGroup> </ItemGroup>

View File

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

View File

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

View File

@ -150,8 +150,6 @@ public class PropertyTemplate : MemberTemplate
this.ValueType = valueType; this.ValueType = valueType;
} }
public static PropertyTemplate MakePropertyTemplate(Type type, PropertyInfo pi, byte index = 0, string customName = null, TypeTemplate typeTemplate = null) 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; var genericPropType = pi.PropertyType.IsGenericType ? pi.PropertyType.GetGenericTypeDefinition() : null;
@ -166,32 +164,36 @@ public class PropertyTemplate : MemberTemplate
var annotationAttr = pi.GetCustomAttribute<AnnotationAttribute>(true); var annotationAttr = pi.GetCustomAttribute<AnnotationAttribute>(true);
var storageAttr = pi.GetCustomAttribute<StorageAttribute>(true); var storageAttr = pi.GetCustomAttribute<StorageAttribute>(true);
var nullabilityContext = new NullabilityInfoContext(); //var nullabilityContext = new NullabilityInfoContext();
propType.Nullable = nullabilityContext.Create(pi).ReadState is NullabilityState.Nullable; //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<>)) if (nullableContextAttrFlag == 2)
// flags.RemoveAt(0); {
if (nullableAttrFlags.Count == 1)
// if (nullableContextAttr?.Flag == 2) propType.SetNotNull(nullableAttrFlags.FirstOrDefault());
// { else
// if (flags.Count == 1) propType.SetNotNull(nullableAttrFlags);
// propType.SetNotNull(flags.FirstOrDefault()); }
// else else
// propType.SetNotNull(flags); {
// } if (nullableAttrFlags.Count == 1)
// else propType.SetNull(nullableAttrFlags.FirstOrDefault());
// { else
// if (flags.Count == 1) propType.SetNull(nullableAttrFlags);
// propType.SetNull(flags.FirstOrDefault()); }
// else
// propType.SetNull(flags);
// }
var pt = new PropertyTemplate(typeTemplate, index, customName ?? pi.Name, pi.DeclaringType != type, propType); 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 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 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 }; [Export] public List<int> IntList => new List<int>() { 1, 2, 3, 4, 5 };