2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 03:32:57 +00:00

null check

This commit is contained in:
Ahmed Zamil 2024-11-25 19:17:14 +03:00
parent 05964ebe4c
commit 6afea08b08
2 changed files with 10 additions and 3 deletions

View File

@ -398,6 +398,9 @@ public class TypeTemplate
public TypeTemplate(Type type, bool addToWarehouse = false) public TypeTemplate(Type type, bool addToWarehouse = false)
{ {
//if (!type.IsPublic)
// throw new Exception("Not public");
if (Codec.ImplementsInterface(type, typeof(IResource))) if (Codec.ImplementsInterface(type, typeof(IResource)))
templateType = TemplateType.Resource; templateType = TemplateType.Resource;
else if (Codec.ImplementsInterface(type, typeof(IRecord))) else if (Codec.ImplementsInterface(type, typeof(IRecord)))
@ -481,7 +484,7 @@ public class TypeTemplate
} }
// add attributes // add attributes
var attrs = type.GetProperties(BindingFlags.Public | BindingFlags.Instance) var attrs = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.GetCustomAttribute<AttributeAttribute>() != null); .Where(x => x.GetCustomAttribute<AttributeAttribute>() != null);
foreach (var attr in attrs) foreach (var attr in attrs)
@ -592,6 +595,7 @@ public class TypeTemplate
if (classIsPublic) if (classIsPublic)
{ {
// get public instance members only.
var mis = type.GetMembers(BindingFlags.Public | BindingFlags.Instance var mis = type.GetMembers(BindingFlags.Public | BindingFlags.Instance
| BindingFlags.DeclaredOnly | BindingFlags.Static) | BindingFlags.DeclaredOnly | BindingFlags.Static)
.Where(x => x.MemberType == MemberTypes.Property || x.MemberType == MemberTypes.Field .Where(x => x.MemberType == MemberTypes.Property || x.MemberType == MemberTypes.Field
@ -618,7 +622,8 @@ public class TypeTemplate
} }
else else
{ {
var mis = type.GetMembers(BindingFlags.Public | BindingFlags.Instance // allow private and public members that are marked with [Export] attribute.
var mis = type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
| BindingFlags.DeclaredOnly | BindingFlags.Static) | BindingFlags.DeclaredOnly | BindingFlags.Static)
.Where(x => x.MemberType == MemberTypes.Property || x.MemberType == MemberTypes.Field .Where(x => x.MemberType == MemberTypes.Property || x.MemberType == MemberTypes.Field
|| x.MemberType == MemberTypes.Event || x.MemberType == MemberTypes.Method) || x.MemberType == MemberTypes.Event || x.MemberType == MemberTypes.Method)

View File

@ -135,7 +135,7 @@ public partial class MyService
[Export] public MyResource? Resource { get; set; } [Export] public MyResource? Resource { get; set; }
[Export] public MyChildResource? ChildResource { get; set; } [Export] public MyChildResource? ChildResource { get; set; }
[Export] public MyChildRecord ChildRecord { get; set; } = new MyChildRecord() { ChildName = "Child", Id = 12, Name = "Parent", Score = 12.2 }; [Export] MyChildRecord ChildRecord { get; set; } = new MyChildRecord() { ChildName = "Child", Id = 12, Name = "Parent", Score = 12.2 };
[Export] public IResource[]? Resources { get; set; } [Export] public IResource[]? Resources { get; set; }
@ -194,4 +194,6 @@ public partial class MyService
[Export] public const double PI = Math.PI; [Export] public const double PI = Math.PI;
[Export] public MyService Me => this; [Export] public MyService Me => this;
[Export] int PrivateInt32 { get; set; } = 99;
} }