diff --git a/Esiur/Net/IIP/DistributedServer.cs b/Esiur/Net/IIP/DistributedServer.cs index e4f00f0..66e8f10 100644 --- a/Esiur/Net/IIP/DistributedServer.cs +++ b/Esiur/Net/IIP/DistributedServer.cs @@ -174,7 +174,7 @@ public class DistributedServer : NetworkServer, IResource public DistributedServer MapCall(string call, Delegate handler) { - var ft = FunctionTemplate.MakeFunctionTemplate(null, handler.Method); + var ft = FunctionTemplate.MakeFunctionTemplate(null, handler.Method, 0, call, null); Calls.Add(call, new CallInfo() { Delegate = handler, Template = ft }); return this; } diff --git a/Esiur/Resource/ExportAttribute.cs b/Esiur/Resource/ExportAttribute.cs index c2a983b..a8331d9 100644 --- a/Esiur/Resource/ExportAttribute.cs +++ b/Esiur/Resource/ExportAttribute.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using System.Xml.Linq; +using static Esiur.Resource.Template.PropertyTemplate; namespace Esiur.Resource; @@ -10,6 +11,7 @@ namespace Esiur.Resource; public class ExportAttribute : Attribute { public string Name { get; private set; } = null; + public PropertyPermission? Permission { get; private set; } public ExportAttribute() { @@ -21,4 +23,17 @@ public class ExportAttribute : Attribute Name = name; } + public ExportAttribute(PropertyPermission permission) + { + Permission = permission; + } + + public ExportAttribute(string name, PropertyPermission permission) + { + Name = name; + Permission = permission; + } + + + } diff --git a/Esiur/Resource/PropertyPermission.cs b/Esiur/Resource/PropertyPermission.cs new file mode 100644 index 0000000..3becb6f --- /dev/null +++ b/Esiur/Resource/PropertyPermission.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Esiur.Resource +{ + public enum PropertyPermission : byte + { + Read = 1, + Write, + ReadWrite, + } +} diff --git a/Esiur/Resource/Template/AttributeTemplate.cs b/Esiur/Resource/Template/AttributeTemplate.cs index c67614a..61cdaff 100644 --- a/Esiur/Resource/Template/AttributeTemplate.cs +++ b/Esiur/Resource/Template/AttributeTemplate.cs @@ -18,13 +18,13 @@ public class AttributeTemplate : MemberTemplate } - public static AttributeTemplate MakeAttributeTemplate(Type type, PropertyInfo pi, byte index = 0, string customName = null, TypeTemplate typeTemplate = null) + public static AttributeTemplate MakeAttributeTemplate(Type type, PropertyInfo pi, byte index, string name, TypeTemplate typeTemplate ) { return new AttributeTemplate() { Index = index, Inherited = pi.DeclaringType != type, - Name = customName, + Name = name, PropertyInfo = pi }; } diff --git a/Esiur/Resource/Template/EventTemplate.cs b/Esiur/Resource/Template/EventTemplate.cs index 9f29451..8431b05 100644 --- a/Esiur/Resource/Template/EventTemplate.cs +++ b/Esiur/Resource/Template/EventTemplate.cs @@ -110,7 +110,7 @@ public class EventTemplate : MemberTemplate // this.ArgumentType = argumentType; //} - public static EventTemplate MakeEventTemplate(Type type, EventInfo ei, byte index = 0, string customName = null, TypeTemplate typeTemplate = null) + public static EventTemplate MakeEventTemplate(Type type, EventInfo ei, byte index, string name, TypeTemplate typeTemplate) { if (!ei.EventHandlerType.IsGenericType) @@ -173,7 +173,7 @@ public class EventTemplate : MemberTemplate return new EventTemplate() { - Name = customName ?? ei.Name, + Name = name, ArgumentType = evtType, Index = index, Inherited = ei.DeclaringType != type, diff --git a/Esiur/Resource/Template/FunctionTemplate.cs b/Esiur/Resource/Template/FunctionTemplate.cs index 5c714a1..550d4e2 100644 --- a/Esiur/Resource/Template/FunctionTemplate.cs +++ b/Esiur/Resource/Template/FunctionTemplate.cs @@ -128,7 +128,7 @@ public class FunctionTemplate : MemberTemplate - public static FunctionTemplate MakeFunctionTemplate(Type type, MethodInfo mi, byte index = 0, string customName = null, TypeTemplate typeTemplate = null) + public static FunctionTemplate MakeFunctionTemplate(Type type, MethodInfo mi, byte index, string name, TypeTemplate typeTemplate) { var genericRtType = mi.ReturnType.IsGenericType ? mi.ReturnType.GetGenericTypeDefinition() : null; @@ -292,7 +292,7 @@ public class FunctionTemplate : MemberTemplate return new FunctionTemplate() { - Name = customName ?? mi.Name, + Name = name, Index = index, Inherited = mi.DeclaringType != type, IsStatic = mi.IsStatic, diff --git a/Esiur/Resource/Template/MemberData.cs b/Esiur/Resource/Template/MemberData.cs index 139ac3d..cb1ea88 100644 --- a/Esiur/Resource/Template/MemberData.cs +++ b/Esiur/Resource/Template/MemberData.cs @@ -16,6 +16,9 @@ public class MemberData public MemberData? Parent; public MemberData? Child; public byte Index; + + public PropertyPermission PropertyPermission; + //public ExportAttribute ExportAttribute; @@ -23,7 +26,35 @@ public class MemberData public MemberData(MemberInfo info, int order) { - this.Name = info.GetCustomAttribute()?.Name ?? info.Name; + var exportAttr = info.GetCustomAttribute(); + + if (info is PropertyInfo pi) + { + if (exportAttr != null && exportAttr.Permission.HasValue) + { + if ((exportAttr.Permission == PropertyPermission.Write + || exportAttr.Permission == PropertyPermission.ReadWrite) && !pi.CanWrite) + { + throw new Exception($"Property '{pi.Name}' does not have a setter, but ExportAttribute specifies it as writable."); + } + + if ((exportAttr.Permission == PropertyPermission.Read + || exportAttr.Permission == PropertyPermission.ReadWrite) && !pi.CanRead) + { + throw new Exception($"Property '{pi.Name}' does not have a getter, but ExportAttribute specifies it as readable."); + } + + this.PropertyPermission = exportAttr.Permission.Value; + } + else + { + this.PropertyPermission = (pi.CanRead && pi.CanWrite) ? PropertyPermission.ReadWrite + : pi.CanWrite ? PropertyPermission.Write + : PropertyPermission.Read; + } + } + + this.Name = exportAttr?.Name ?? info.Name; this.Info = info; this.Order = order; } diff --git a/Esiur/Resource/Template/PropertyTemplate.cs b/Esiur/Resource/Template/PropertyTemplate.cs index d4388f1..611c981 100644 --- a/Esiur/Resource/Template/PropertyTemplate.cs +++ b/Esiur/Resource/Template/PropertyTemplate.cs @@ -14,12 +14,7 @@ public class PropertyTemplate : MemberTemplate { public Map Annotations { get; set; } - public enum PropertyPermission : byte - { - Read = 1, - Write, - ReadWrite - } + public PropertyInfo PropertyInfo @@ -92,7 +87,7 @@ public class PropertyTemplate : MemberTemplate var hasAnnotation = ((data[offset] & 0x8) == 0x8); var recordable = ((data[offset] & 1) == 1); - var permission = (PropertyTemplate.PropertyPermission)((data[offset++] >> 1) & 0x3); + var permission = (PropertyPermission)((data[offset++] >> 1) & 0x3); var name = data.GetString(offset + 1, data[offset]); offset += (uint)data[offset] + 1; @@ -209,7 +204,7 @@ public class PropertyTemplate : MemberTemplate // 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, string name, byte index, PropertyPermission permission, TypeTemplate typeTemplate) { var genericPropType = pi.PropertyType.IsGenericType ? pi.PropertyType.GetGenericTypeDefinition() : null; @@ -269,15 +264,16 @@ public class PropertyTemplate : MemberTemplate annotations.Add("", GetTypeAnnotationName(pi.PropertyType)); } + return new PropertyTemplate() { - Name = customName ?? pi.Name, + Name = name, Index = index, Inherited = pi.DeclaringType != type, ValueType = propType, PropertyInfo = pi, Recordable = storageAttr == null ? false : storageAttr.Mode == StorageMode.Recordable, - Permission = (pi.CanWrite && pi.CanRead) ? PropertyPermission.ReadWrite : (pi.CanWrite ? PropertyPermission.Write : PropertyPermission.Read), + Permission = permission, Annotations = annotations, }; diff --git a/Esiur/Resource/Template/TypeTemplate.cs b/Esiur/Resource/Template/TypeTemplate.cs index 2e354f1..991efdf 100644 --- a/Esiur/Resource/Template/TypeTemplate.cs +++ b/Esiur/Resource/Template/TypeTemplate.cs @@ -427,7 +427,7 @@ public class TypeTemplate foreach (var pd in hierarchy[MemberTypes.Property]) { properties.Add(PropertyTemplate.MakePropertyTemplate - (type, (PropertyInfo)pd.GetMemberInfo(), pd.Index, pd.Name, this)); + (type, (PropertyInfo)pd.GetMemberInfo(), pd.Name, pd.Index, pd.PropertyPermission, this)); } }