private void SetDictValue(string key, IRedType value)
        {
            if (value == null)
            {
                _properties.Remove(key);
            }
            else
            {
                if (value.GetType().IsGenericType)
                {
                    if (value.GetType().GetGenericTypeDefinition() == typeof(CArrayFixedSize <>))
                    {
                        var propertyInfo = RedReflection.GetPropertyByRedName(GetType(), key);
                        var flags        = propertyInfo.Flags;
                        var size         = flags.MoveNext() ? flags.Current : 0;

                        if (((IRedArray)value).Count > size)
                        {
                            throw new ArgumentException();
                        }
                    }

                    if (value.GetType().GetGenericTypeDefinition() == typeof(CStatic <>))
                    {
                        var propertyInfo = RedReflection.GetPropertyByRedName(GetType(), key);
                        var flags        = propertyInfo.Flags;
                        var maxSize      = flags.MoveNext() ? flags.Current : 0;

                        ((IRedArray)value).MaxSize = maxSize;
                    }
                }

                _properties[key] = value;
            }
        }
        internal void InternalInitClass()
        {
            var info = RedReflection.GetTypeInfo(GetType());

            foreach (var propertyInfo in info.PropertyInfos)
            {
                if (string.IsNullOrEmpty(propertyInfo.RedName))
                {
                    continue;
                }

                var propTypeInfo = RedReflection.GetTypeInfo(propertyInfo.Type);
                if (propertyInfo.Type.IsValueType || propTypeInfo.IsValueType)
                {
                    if (propertyInfo.Flags.Equals(Flags.Empty))
                    {
                        SetDictValue(propertyInfo.RedName, (IRedType)System.Activator.CreateInstance(propertyInfo.Type));
                    }
                    else
                    {
                        var flags = propertyInfo.Flags;
                        SetDictValue(propertyInfo.RedName, (IRedType)System.Activator.CreateInstance(propertyInfo.Type, flags.MoveNext() ? flags.Current : 0));
                    }
                }
            }
        }
        public static IRedType CreateRedType(string redTypeName)
        {
            var(type, _) = RedReflection.GetCSTypeFromRedType(redTypeName);
            if (type == null)
            {
                throw new TypeNotFoundException(redTypeName);
            }

            return(CreateRedType(type));
        }
Beispiel #4
0
        public CArrayBase(int size)
        {
            _internalList = new List <T>(new T[size]);

            var propTypeInfo = RedReflection.GetTypeInfo(typeof(T));

            if (propTypeInfo.IsValueType)
            {
                for (var i = 0; i < Count; i++)
                {
                    this[i] = System.Activator.CreateInstance <T>();
                }
            }
        }
        protected T GetPropertyValue <T>([CallerMemberName] string callerName = "") where T : IRedType
        {
            var propertyInfo = RedReflection.GetPropertyByName(this.GetType(), callerName);

            return((T)InternalGetPropertyValue(typeof(T), propertyInfo.RedName, propertyInfo.Flags));
        }
/*
 *      public event ObjectChangedEventHandler ObjectChanged;
 *
 *      private readonly Dictionary<string, ObjectChangedEventHandler> _delegateCache = new();
 *
 *
 *      private void AddEventHandler(string redPropertyName)
 *      {
 *          if (!_delegateCache.ContainsKey(redPropertyName))
 *          {
 *              _delegateCache.Add(redPropertyName, delegate (object sender, ObjectChangedEventArgs args)
 *              {
 *                  if (args._callStack.Contains(this))
 *                  {
 *                      return;
 *                  }
 *                  args._callStack.Add(this);
 *
 *                  if (sender != null)
 *                  {
 *                      ObjectChanged?.Invoke(sender, args);
 *                  }
 *                  else
 *                  {
 *                      args.RedName = redPropertyName;
 *                      ObjectChanged?.Invoke(this, args);
 *                  }
 *              });
 *          }
 *
 *          if (_properties[redPropertyName] is IRedNotifyObjectChanged notify)
 *          {
 *              notify.ObjectChanged += _delegateCache[redPropertyName];
 *          }
 *      }
 *
 *      private void RemoveEventHandler(string redPropertyName)
 *      {
 *          if (!_delegateCache.ContainsKey(redPropertyName))
 *          {
 *              return;
 *          }
 *
 *          if (_properties[redPropertyName] is IRedNotifyObjectChanged notify)
 *          {
 *              notify.ObjectChanged -= _delegateCache[redPropertyName];
 *          }
 *      }
 *
 *      private void OnObjectChanged(string redPropertyName, object oldValue, object newValue)
 *      {
 *          if (ObjectChanged != null)
 *          {
 *              var args = new ObjectChangedEventArgs(ObjectChangedType.Modified, redPropertyName, oldValue, newValue);
 *              args._callStack.Add(this);
 *
 *              ObjectChanged.Invoke(this, args);
 *          }
 *      }
 */
        #endregion

        private string GetRedName(string propertyName)
        {
            var property = RedReflection.GetPropertyByName(this.GetType(), propertyName);

            return(property?.RedName ?? propertyName);
        }