// TODO:sk reflection should be cached if possible
        private static MySyncedClass GetSyncedClass(object obj, Type type = null)
        {
            Debug.Assert(tmpFields.Count == 0);
            MySyncedClass sync = new MySyncedClass();

            type = type ?? obj.GetType();

            Type baseType = type.BaseType;

            if (baseType != null && baseType != typeof(object))
            {
                sync.Add(GetSyncedClass(obj, baseType));
            }

            var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            foreach (var field in fields)
            {
                //var baseFieldType = field.FieldType.BaseType;
                //if (baseFieldType != null && baseFieldType.IsGenericType && baseFieldType.GetGenericTypeDefinition() == typeof(MySyncedBase<>))

                var fieldAttributes = field.GetCustomAttributes(false);

                foreach (var fieldAttribute in fieldAttributes)
                {
                    StateDataAttribute stateData = fieldAttribute as StateDataAttribute;
                    if (stateData != null)
                    {
                        int order = stateData.Order;
                        Debug.Assert(!tmpFields.ContainsKey(order));

                        object value = field.GetValue(obj);
                        Debug.Assert(value != null, "Uninitialized synced variable");
                        Debug.Assert(!tmpFields.ContainsValue(value));

                        tmpFields.Add(order, value);
                    }
                }
            }

            foreach (var value in tmpFields.Values)
            {
                sync.Add((IMySyncedValue)value);
            }
            tmpFields.Clear();

            return(sync);
        }
Exemplo n.º 2
0
        // TODO:SK reflection should be cached if possible
        private static MySyncedClass GetSyncedClass(object obj, Type type = null)
        {
            Debug.Assert(tmpFields.Count == 0);
            MySyncedClass sync = null;
            MySyncedClass baseSync = null;

            type = type ?? obj.GetType();

            Type baseType = type.BaseType;

            if (baseType != null && baseType != typeof(object))
            {
                baseSync = GetSyncedClass(obj, baseType);
            }

            var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            foreach (var field in fields)
            {
                //var baseFieldType = field.FieldType.BaseType;
                //if (baseFieldType != null && baseFieldType.IsGenericType && baseFieldType.GetGenericTypeDefinition() == typeof(MySyncedBase<>))

                var fieldAttributes = field.GetCustomAttributes(false);

                foreach (var fieldAttribute in fieldAttributes)
                {
                    StateDataAttribute stateData = fieldAttribute as StateDataAttribute;
                    if (stateData != null)
                    {
                        int order = stateData.Order;
                        Debug.Assert(!tmpFields.ContainsKey(order));

                        object value = field.GetValue(obj);
                        Debug.Assert(value != null, "Uninitialized synced variable");
                        Debug.Assert(!tmpFields.ContainsValue(value));

                        tmpFields.Add(order, value);
                    }
                }
            }

            if (baseSync != null && tmpFields.Count == 0)
            {
                return baseSync;
            }
            else
            {
                sync = new MySyncedClass();
                if (baseSync != null)
                {
                    sync.Add(baseSync);
                }
                foreach (var value in tmpFields.Values)
                {
                    sync.Add((IMySyncedValue)value);
                }
            }
            tmpFields.Clear();

            return sync;
        }