Exemplo n.º 1
0
        /// <summary>
        /// Save a DynamicTypeDescriptor for the specified values
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="valueDictionary"></param>
        /// <param name="nameSpace"></param>
        /// <returns></returns>
        protected DynamicTypeDescriptor SaveTypeDescriptor(string typeName, Dictionary <object, object> valueDictionary, string nameSpace = null)
        {
            DynamicTypeDescriptor descriptor = EnsureType(typeName, nameSpace);

            foreach (object key in valueDictionary.Keys)
            {
                object value = valueDictionary[key];
                if (value != null)
                {
                    Type   childType     = value.GetType();
                    string childTypeName = $"{typeName}.{key}";
                    DynamicTypePropertyDescriptor propertyDescriptor = new DynamicTypePropertyDescriptor
                    {
                        DynamicTypeDescriptorId = descriptor.Id,
                        ParentTypeName          = descriptor.TypeName,
                        PropertyType            = childType.Name,
                        PropertyName            = key.ToString(),
                    };

                    if (childType == typeof(JObject) || childType == typeof(Dictionary <object, object>))
                    {
                        propertyDescriptor.PropertyType = childTypeName;
                        SetDynamicTypePropertyDescriptor(propertyDescriptor);
                        Dictionary <object, object> data = value as Dictionary <object, object>;
                        if (data is null)
                        {
                            data = ((JObject)value).ToObject <Dictionary <object, object> >();
                        }
                        SaveTypeDescriptor(childTypeName, data);
                    }
                    else if (childType == typeof(JArray) || childType.IsArray)
                    {
                        propertyDescriptor.PropertyType = $"arrayOf({childTypeName})";
                        SetDynamicTypePropertyDescriptor(propertyDescriptor);

                        foreach (object obj in (IEnumerable)value)
                        {
                            Dictionary <object, object> data = new Dictionary <object, object>();
                            if (obj is JObject jobj)
                            {
                                data = jobj.ToObject <Dictionary <object, object> >();
                                SaveTypeDescriptor(childTypeName, data);
                            }
                        }
                    }
                    else
                    {
                        SetDynamicTypePropertyDescriptor(propertyDescriptor);
                    }
                }
            }

            return(DynamicTypeDataRepository.Retrieve <DynamicTypeDescriptor>(descriptor.Id));
        }
Exemplo n.º 2
0
        public void CanGetAssembly()
        {
            DynamicTypeManager mgr = new DynamicTypeManager(new DynamicTypeDataRepository(), DataSettings.Default);

            mgr.DynamicTypeDataRepository.Query <DynamicTypeDescriptor>(d => d.Id > 0).Each(d => mgr.DynamicTypeDataRepository.Delete(d));
            mgr.DynamicTypeDataRepository.Query <DynamicTypePropertyDescriptor>(p => p.Id > 0).Each(p => mgr.DynamicTypeDataRepository.Delete(p));
            string testType = nameof(CanAddPropertyToType);

            mgr.AddType(testType);
            string testProperty  = "SomeProperty";
            string testProperty2 = "BooleanProperty";
            DynamicTypePropertyDescriptor prop  = mgr.AddProperty(testType, testProperty, "String");
            DynamicTypePropertyDescriptor prop2 = mgr.AddProperty(testType, testProperty2, "Boolean");

            Assembly ass = mgr.GetAssembly();

            Expect.IsNotNull(ass);
        }
Exemplo n.º 3
0
        private DynamicTypePropertyDescriptor SetDynamicTypePropertyDescriptor(DynamicTypePropertyDescriptor prop)
        {
            int hashCode = prop.GetHashCode();

            _dynamicTypePropertyLocks.AddMissing(hashCode, new object());
            lock (_dynamicTypePropertyLocks[hashCode])
            {
                DynamicTypePropertyDescriptor retrieved = DynamicTypeDataRepository.DynamicTypePropertyDescriptorsWhere(pd =>
                                                                                                                        pd.DynamicTypeDescriptorId == prop.DynamicTypeDescriptorId &&
                                                                                                                        pd.ParentTypeName == prop.ParentTypeName &&
                                                                                                                        pd.PropertyType == prop.PropertyType &&
                                                                                                                        pd.PropertyName == prop.PropertyName).FirstOrDefault();

                if (retrieved == null)
                {
                    retrieved = DynamicTypeDataRepository.Save(prop);
                }
                return(retrieved);
            }
        }