Esempio n. 1
0
        public static void ApplyBindingsRecursive(IBindable obj)
        {
            var type = obj.GetType();

            // if it has children (f.e. BindableList) we forward all their events and child events to
            if (obj is IBindableContainer)
            {
                foreach (var child in ((IBindableContainer)obj).GetBindableChildren())
                {
                    ApplyBindingsRecursive(child);
                }
            }

            // go through all field an look for the [CreateBindable] attribute
            var fields = type.GetFields();

            foreach (var field in fields)
            {
                var fieldValue = field.GetValue(obj);
                if (field.GetCustomAttributes(typeof(BindableAttribute), true).Any())
                {
                    var castedfieldValue = (IBindable)fieldValue;

                    obj.ForwardOnChange(castedfieldValue);
//                    UnityEngine.Debug.Log("forwarding from field '" + field.Name + "' " + castedfieldValue.GetType().Name + " to " + obj.GetType().Name);

                    ApplyBindingsRecursive(castedfieldValue);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// <see cref="CreateObjectRecursive{T}"/>
        /// </summary>
        /// <param name="type"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        protected static IBindable CreateObjectRecursive(Type type, IBindable parent)
        {
            // some debug output
//            UnityEngine.Debug.Log("Trying to create type: " + type.FullName + " isGeneric:" + type.IsGenericType);
//            if (type.IsGenericType)
//            {
//                UnityEngine.Debug.Log("It is a generic type: " + type.GetGenericArguments()[0].ToString());
//            }

            // create the objects instance
            var constructor = type.GetConstructor(Type.EmptyTypes);
            var obj         = constructor.Invoke(null);

            // the object to be created must implement IBindable
            if (!(obj is IBindable))
            {
                throw new BindableException("All objects created by BindableFactory must implement the interface IBindable but " + type.Name + " does not!");
            }

            var objCasted = (IBindable)obj;

            // if the object has a parent then forward the events to the parent
            if (parent != null)
            {
                parent.ForwardOnChange(objCasted);
            }

            // go through all field an look for the [CreateBindable] attribute
            var fields = type.GetFields();

            foreach (var field in fields)
            {
                if (field.GetCustomAttributes(typeof(BindableAttribute), true).Any())
                {
                    var childObj = CreateObjectRecursive(field.FieldType, objCasted);
                    field.SetValue(objCasted, childObj);
                }
            }

            return(objCasted);
        }