mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2026-01-27 01:20:39 +00:00
templates
This commit is contained in:
@@ -174,7 +174,7 @@ public class DistributedServer : NetworkServer<DistributedConnection>, 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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
13
Esiur/Resource/PropertyPermission.cs
Normal file
13
Esiur/Resource/PropertyPermission.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Resource
|
||||
{
|
||||
public enum PropertyPermission : byte
|
||||
{
|
||||
Read = 1,
|
||||
Write,
|
||||
ReadWrite,
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<ExportAttribute>()?.Name ?? info.Name;
|
||||
var exportAttr = info.GetCustomAttribute<ExportAttribute>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -14,12 +14,7 @@ public class PropertyTemplate : MemberTemplate
|
||||
{
|
||||
public Map<string, string> 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,
|
||||
};
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user