Esempio n. 1
0
        public static void ClearSessionProperties(Type type)
        {
            for (; type != typeof(object); type = type.BaseType)
            {
                FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly);

                foreach (FieldInfo field in fields)
                {
                    Type fieldType = field.FieldType;

                    if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(SessionProperty <>))
                    {
                        string key = null;

                        if (field.IsDefined(typeof(SessionProperty.KeyAttribute), false))
                        {
                            SessionProperty.KeyAttribute attr = ((SessionProperty.KeyAttribute[])field.GetCustomAttributes(typeof(SessionProperty.KeyAttribute), false))[0];

                            key = attr.Key;
                        }

                        if (key == null)
                        {
                            key = BuildSessionKey(field);
                        }

                        WebAppContext.HttpContext.Session.Remove(key);
                    }
                }
            }
        }
Esempio n. 2
0
        private static void CreateSessionProperties(object obj, Type type)
        {
            for (; type != typeof(object); type = type.BaseType)
            {
                FieldInfo[] fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

                foreach (FieldInfo field in fields)
                {
                    Type fieldType = field.FieldType;

                    if (obj == null && !field.IsStatic)
                    {
                        continue;
                    }

                    if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(SessionProperty <>))
                    {
                        if (field.GetValue(field.IsStatic ? null : obj) == null)
                        {
                            string key          = null;
                            object defaultValue = null;
                            bool   createNew    = false;

                            if (field.IsDefined(typeof(SessionProperty.KeyAttribute), false))
                            {
                                SessionProperty.KeyAttribute attr = ((SessionProperty.KeyAttribute[])field.GetCustomAttributes(typeof(SessionProperty.KeyAttribute), false))[0];

                                key = attr.Key;
                            }

                            if (field.IsDefined(typeof(SessionProperty.DefaultValueAttribute), false))
                            {
                                SessionProperty.DefaultValueAttribute attr = ((SessionProperty.DefaultValueAttribute[])field.GetCustomAttributes(typeof(SessionProperty.DefaultValueAttribute), false))[0];

                                defaultValue = attr.Value;
                            }

                            if (field.IsDefined(typeof(SessionProperty.AutoCreateNewAttribute), false))
                            {
                                createNew = true;
                            }

                            if (key == null)
                            {
                                key = BuildSessionKey(field);
                            }

                            field.SetValue(field.IsStatic ? null : obj, CreateSessionProperty(field, key, defaultValue, createNew));
                        }
                    }
                }
            }
        }