// /** // * File: ManagedObjectDescriptor.cs // * Author: haraldwolff // * // * This file and it's content is copyrighted by the Author and / or copyright holder. // * Any use wihtout proper permission is illegal and may lead to legal actions. // * // * // **/ using System; using System.Reflection; using ln.json; using ln.types.net; using ln.json.attributes; namespace ln.manage { public class PropertyDescriptorAttribute : Attribute { public string Group { get; set; } public string Alias { get; set; } public bool ReadOnly { get; set; } public string LookupPath { get; set; } public bool Identity { get; set; } } public class PropertyDescriptor { public IManagedContainer Container { get; } public string PropertyGroup { get; } public string PropertyName { get; } public Type PropertyType { get; } public bool ReadOnly { get; } public string LookupPath { get; } public bool Identity { get; } public PropertyDescriptor(IManagedContainer container, string propertyGoup, string propertyName, Type propertyType, bool readOnly, string lookupPath, bool identity) { Container = container; PropertyGroup = propertyGoup; PropertyName = propertyName; PropertyType = propertyType; ReadOnly = readOnly; LookupPath = lookupPath; Identity = identity; } public PropertyDescriptor(IManagedContainer container, FieldInfo fieldInfo) : this(container, fieldInfo.GetCustomAttribute()?.Group, fieldInfo.GetCustomAttribute()?.Alias ?? fieldInfo.Name, fieldInfo.FieldType, fieldInfo.GetCustomAttribute()?.ReadOnly ?? fieldInfo.IsInitOnly, fieldInfo.GetCustomAttribute()?.LookupPath, fieldInfo.GetCustomAttribute()?.Identity ?? false) { } public PropertyDescriptor(IManagedContainer container, PropertyInfo propertyInfo) : this(container, propertyInfo.GetCustomAttribute()?.Group, propertyInfo.GetCustomAttribute()?.Alias ?? propertyInfo.Name, propertyInfo.PropertyType, propertyInfo.GetCustomAttribute()?.ReadOnly ?? !propertyInfo.CanWrite, propertyInfo.GetCustomAttribute()?.LookupPath, propertyInfo.GetCustomAttribute()?.Identity ?? false) { } public JSONObject CreateJsonDescriptor() { JSONObject descriptor = new JSONObject(); descriptor["name"] = PropertyName; descriptor["readonly"] = ReadOnly; if (typeof(string).Equals(PropertyType)) { descriptor["type"] = "string"; } else if (typeof(int).Equals(PropertyType)) { descriptor["type"] = "int"; } else if (typeof(double).Equals(PropertyType)) { descriptor["type"] = "double"; } else if (typeof(IPv4).Equals(PropertyType)) { descriptor["type"] = "ip4"; } else if (typeof(IPv6).Equals(PropertyType)) { descriptor["type"] = "ip6"; } else if (typeof(MAC).Equals(PropertyType)) { descriptor["type"] = "mac"; } else if (PropertyType.IsEnum) { if (PropertyType.GetCustomAttribute() != null) { JSONObject type = new JSONObject(); string[] names = Enum.GetNames(PropertyType); int[] values = (int[])Enum.GetValues(PropertyType); for (int n = 0; n < names.Length; n++) type[names[n]] = values[n]; descriptor["type"] = type; } else { JSONArray type = new JSONArray(); string[] symbols = Enum.GetNames(PropertyType); foreach (string symbol in symbols) type.Add(symbol); descriptor["type"] = type; } } else if (LookupPath != null) { // ToDo: Implement Referenced Objects throw new NotImplementedException(); } else { throw new NotSupportedException(); } return descriptor; } } }