示例#1
0
        /// <summary>
        /// Adds a <see cref="ContainerPropertyBag{TContainer}"/> to the store.
        /// </summary>
        /// <param name="propertyBag">The <see cref="ContainerPropertyBag{TContainer}"/> to add.</param>
        /// <typeparam name="TContainer">The container type this <see cref="ContainerPropertyBag{TContainer}"/> describes.</typeparam>
        internal static void AddPropertyBag <TContainer>(IPropertyBag <TContainer> propertyBag)
        {
            if (!RuntimeTypeInfoCache <TContainer> .IsContainerType)
            {
                throw new Exception($"PropertyBagStore Type=[{typeof(TContainer)}] is not a valid container type. Type can not be primitive, enum or string.");
            }

            if (RuntimeTypeInfoCache <TContainer> .IsAbstractOrInterface)
            {
                throw new Exception($"PropertyBagStore Type=[{typeof(TContainer)}] is not a valid container type. Type can not be abstract or interface.");
            }

#if !NET_DOTS
            if (null != TypedStore <TContainer> .PropertyBag)
            {
                if (propertyBag.GetType().GetCustomAttributes <System.Runtime.CompilerServices.CompilerGeneratedAttribute>().Any())
                {
                    return;
                }
            }
#endif

            TypedStore <TContainer> .PropertyBag = propertyBag;
            if (!s_PropertyBags.ContainsKey(typeof(TContainer)))
            {
                s_RegisteredTypes.Add(typeof(TContainer));
            }

            s_PropertyBags[typeof(TContainer)] = propertyBag;
            NewTypeRegistered?.Invoke(typeof(TContainer), propertyBag);
        }
        public PropertyBagDebugInfo(Type type, IPropertyBag propertyBag)
        {
            Type          = type;
            Namespace     = type.Namespace;
            Assembly      = type.Assembly.GetName().Name;
            Name          = TypeUtility.GetTypeDisplayName(type);
            FullName      = $"{Namespace}.{Name}";
            m_PropertyBag = propertyBag;

            var bagType = m_PropertyBag.GetType();

            if (null != bagType.GetCustomAttribute <System.Runtime.CompilerServices.CompilerGeneratedAttribute>())
            {
                PropertyBagType = PropertyBagType.CodeGen;
            }
            else if (null != bagType.GetCustomAttribute <ReflectedPropertyBagAttribute>())
            {
                PropertyBagType = PropertyBagType.Reflection;
            }
            else
            {
                PropertyBagType = PropertyBagType.Manual;
            }

            TypeTraits = type.IsValueType
                ? TypeTraits.Struct
                : TypeTraits.Class;
            if (UnsafeUtility.IsUnmanaged(type))
            {
                TypeTraits |= TypeTraits.Unmanaged;
            }
            if (UnsafeUtility.IsBlittable(type))
            {
                TypeTraits |= TypeTraits.Blittable;
            }

            if (type.IsGenericType)
            {
                TypeTraits |= TypeTraits.Generic;
            }

            TypeInfo      = CacheTypeInfo();
            Properties    = CacheProperties();
            PropertyNames = new List <string>();
            PropertyTypes = new List <string>();
            foreach (var property in Properties)
            {
                if (!(property is PropertyTypeDescriptor typed))
                {
                    continue;
                }
                PropertyNames.Add(typed.Descriptor.Name);
                PropertyTypes.Add(TypeUtility.GetTypeDisplayName(typed.Value));
            }
            Serialization    = CacheSerializationInfo();
            UI               = CacheInspectorInfo();
            CanBeConstructed = TypeConstruction.CanBeConstructed(type);

            if (Properties.Count > 0)
            {
                Extensions |= ExtensionType.Properties;
            }

            if (Serialization.Count > 0)
            {
                Extensions |= ExtensionType.Serialization;
            }

            if (UI.Count > 0)
            {
                Extensions |= ExtensionType.UI;
            }
        }