コード例 #1
0
        public static BinTreeObject Serialize <T>(MetaEnvironment environment, uint pathHash, T metaClass)
            where T : IMetaClass
        {
            Type metaClassType = metaClass.GetType();
            MetaClassAttribute metaClassAttribute = metaClassType.GetCustomAttribute(typeof(MetaClassAttribute)) as MetaClassAttribute;

            if (metaClassAttribute is null)
            {
                throw new InvalidOperationException("The provided MetaClass does not have a MetaClass Attribute");
            }

            // Create Tree Properties for meta properties
            List <BinTreeProperty> properties = new();

            foreach (PropertyInfo propertyInfo in metaClassType.GetProperties())
            {
                BinTreeProperty treeProperty = ConvertPropertyToTreeProperty(environment, metaClass, propertyInfo);

                if (treeProperty is not null)
                {
                    properties.Add(treeProperty);
                }
            }

            BinTreeObject treeObject = new BinTreeObject(metaClassAttribute.NameHash, pathHash, properties);

            return(treeObject);
        }
コード例 #2
0
        public static T Deserialize <T>(MetaEnvironment environment, BinTreeObject treeObject)
            where T : IMetaClass
        {
            Type metaClassType = typeof(T);
            MetaClassAttribute metaClassAttribute = metaClassType.GetCustomAttribute(typeof(MetaClassAttribute)) as MetaClassAttribute;

            // Verify attribute
            if (metaClassAttribute is null)
            {
                throw new InvalidOperationException("The provided MetaClass does not have a MetaClass Attribute");
            }
            if (metaClassAttribute.NameHash != treeObject.MetaClassHash)
            {
                throw new InvalidOperationException("Meta Class name does not match class name of treeObject");
            }

            // Create an instance of T and get its runtime type
            T    metaClassObject     = Activator.CreateInstance <T>();
            Type metaClassObjectType = metaClassObject.GetType();

            // Assign values to the object properties
            AssignMetaClassProperties(environment, metaClassObject, metaClassObjectType, treeObject.Properties);

            // Registered the object in the environment for link resolving
            environment.RegisterObject(treeObject.PathHash, metaClassObject);

            return(metaClassObject);
        }