// /** // * 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; namespace ln.manage { public class ManagedObjectPropertyDescriptor { public ManagedObjectProvider Provider { get; } public Type ManagedType => Provider.ManagedType; public string PropertyName { get; } public Type PropertyType { get; } public bool ReadOnly { get; } public string LookupPath { get; } Action setter; Func getter; public ManagedObjectPropertyDescriptor(ManagedObjectProvider objectProvider, string propertyName, Type propertyType, bool readOnly, string lookupPath, Func getter, Action setter) { Provider = objectProvider; PropertyName = propertyName; PropertyType = propertyType; ReadOnly = readOnly; LookupPath = lookupPath; this.getter = getter; this.setter = setter; } public ManagedObjectPropertyDescriptor(ManagedObjectProvider objectProvider,string propertyName,Type propertyType,bool readOnly,string lookupPath) :this(objectProvider,propertyName,propertyType,readOnly,lookupPath,null,null) {} public ManagedObjectPropertyDescriptor(ManagedObjectProvider objectProvider, FieldInfo fieldInfo, string lookupPath) : this(objectProvider, fieldInfo.Name, fieldInfo.FieldType, fieldInfo.IsInitOnly, lookupPath, fieldInfo.GetValue, fieldInfo.SetValue) {} public ManagedObjectPropertyDescriptor(ManagedObjectProvider objectProvider, FieldInfo fieldInfo) : this(objectProvider, fieldInfo, null) {} public ManagedObjectPropertyDescriptor(ManagedObjectProvider objectProvider, PropertyInfo propertyInfo, string lookupPath) : this(objectProvider, propertyInfo.Name, propertyInfo.PropertyType, !propertyInfo.CanWrite, lookupPath, propertyInfo.GetValue, propertyInfo.SetValue) {} public ManagedObjectPropertyDescriptor(ManagedObjectProvider objectProvider, PropertyInfo propertyInfo) : this(objectProvider, propertyInfo, null) {} public void SetValue(object managedObject, object value) => setter(managedObject, value); public object GetValue(object managedObject) => getter(managedObject); 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; } } }