Exemplo n.º 1
0
 static public void InitializeDefault(StringEndec endec)
 {
     if (defaultEndec != null)
     {
         throw new Exception("Default StringEndec instance is already set and cannot be re-set.");
     }
     defaultEndec = endec;
 }
Exemplo n.º 2
0
 public void Initialize(StringEndec endec)
 {
     if (endec != null)
     {
         throw new Exception("StringEndec instance is already set and cannot be re-set.");
     }
     _endec = endec;
 }
Exemplo n.º 3
0
        protected SettingsFieldInfo(MemberInfo settingsTypeMemberInfo, Type settingsType, Func <object> getObject, Action <Settings> setObject)
        {
            this.getObject = getObject;
            this.setObject = setObject;
#endif
            Type     = settingsType;
            Name     = settingsTypeMemberInfo.Name;
            FullName = settingsTypeMemberInfo.DeclaringType.FullName + "." + Name;

            /*//version with static __StorageDir
             * string storageDir;
             * for (; ; )
             * {
             *  PropertyInfo fi = settingType.GetProperty(nameof(Settings.__StorageDir), BindingFlags.Static | BindingFlags.Public);
             *  if (fi != null)
             *  {
             *      storageDir = (string)fi.GetValue(null);
             *      break;
             *  }
             *  settingType = settingType.BaseType;
             *  if (settingType == null)
             *      throw new Exception("Settings type " + Type.ToString() + " or some of its ancestors must define the public static getter " + nameof(Settings.__StorageDir));
             * }
             * File = storageDir + System.IO.Path.DirectorySeparatorChar + FullName + "." + Config.FILE_EXTENSION;
             */
            Settings s = (Settings)Activator.CreateInstance(Type); //!!!slightly slowler than calling a static by reflection. Doesn't run even more slower for a bigger class though.
            File     = s.__StorageDir + System.IO.Path.DirectorySeparatorChar + FullName + "." + Config.FILE_EXTENSION;
            InitFile = Log.AppDir + System.IO.Path.DirectorySeparatorChar + FullName + "." + Config.FILE_EXTENSION;

            SettingsAttributes.EncryptedAttribute encryptedAttribute = settingsTypeMemberInfo.GetCustomAttributes <SettingsAttributes.EncryptedAttribute>().FirstOrDefault();
            if (encryptedAttribute == null)
            {
                encryptedAttribute = settingsType.GetCustomAttributes <SettingsAttributes.EncryptedAttribute>(true).FirstOrDefault();
            }
            if (encryptedAttribute != null)
            {
                Endec = encryptedAttribute.Endec;
            }

            SettingsAttributes.ConfigAttribute configAttribute = settingsTypeMemberInfo.GetCustomAttributes <SettingsAttributes.ConfigAttribute>().FirstOrDefault();
            if (configAttribute == null)
            {
                configAttribute = settingsType.GetCustomAttributes <SettingsAttributes.ConfigAttribute>(true).FirstOrDefault();
            }
            if (configAttribute != null)
            {
                Optional       = configAttribute.Optional;
                Indented       = configAttribute.Indented;
                NullSerialized = configAttribute.NullSerialized;
            }

            SettingsAttributes.TypeVersionAttribute typeVersion = settingsType.GetCustomAttributes <SettingsAttributes.TypeVersionAttribute>(true).FirstOrDefault();
            if (typeVersion != null)
            {
                TypeVersion = typeVersion.Value;
            }
        }
 /// <summary>
 /// Settings field attribute that is used for encrypting.
 /// </summary>
 /// <param name="stringEndecGetterHostingType">Class that exposes the StringEndec getter.</param>
 /// <param name="stringEndecGetterName">Name of the StringEndec getter. The getter must be static.</param>
 public EncryptedAttribute(Type stringEndecGetterHostingType, string stringEndecGetterName)
 {
     try
     {
         if (stringEndecGetterHostingType == null)
         {
             throw new Exception("stringEndecGetterHostingType cannot be NULL.");
         }
         if (string.IsNullOrWhiteSpace(stringEndecGetterName))
         {
             throw new Exception("stringEndecGetterName cannot be empty.");
         }
         System.Reflection.PropertyInfo pi = stringEndecGetterHostingType.GetProperty(stringEndecGetterName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
         if (pi != null)
         {
             //if (!pi.PropertyType.IsSubclassOf(typeof(StringEndec)))//!!!does not work
             if (!typeof(StringEndec).IsAssignableFrom(pi.PropertyType))
             {
                 throw new Exception("Type of the property " + stringEndecGetterHostingType.FullName + "." + stringEndecGetterName + " is not " + typeof(StringEndec).FullName);
             }
             Endec = pi.GetValue(null) as StringEndec;
         }
         else
         {
             System.Reflection.FieldInfo fi = stringEndecGetterHostingType.GetField(stringEndecGetterName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
             if (fi == null)
             {
                 throw new Exception(stringEndecGetterHostingType.FullName + " class does not expose the property/field '" + stringEndecGetterName + "'");
             }
             //if (!fi.FieldType.IsSubclassOf(typeof(StringEndec)))//!!!does not work
             if (!typeof(StringEndec).IsAssignableFrom(fi.FieldType))
             {
                 throw new Exception("Type of the field " + stringEndecGetterHostingType.FullName + "." + stringEndecGetterName + " is not " + typeof(StringEndec).FullName);
             }
             Endec = fi.GetValue(null) as StringEndec;
         }
         if (Endec == null)
         {
             throw new Exception("Property " + stringEndecGetterHostingType.FullName + "." + stringEndecGetterName + " is NULL.");
         }
     }
     catch (Exception e)
     {
         throw new Exception("Error in the attribute " + GetType().FullName, e);
     }
 }