Example #1
0
 public static T GetConfigValue <T>(string keyName)
 {
     if (ConfigurationManager.AppSettings.AllKeys.Where(key => key.Equals(keyName)).Any())
     {
         var configValue = ConfigurationManager.AppSettings[keyName];
         T   returnValue = TConverter.ChangeType <T>(configValue);
         return(returnValue);
     }
     else
     {
         return(default(T));
     }
 }
Example #2
0
        public static ReturnType Bind <ReturnType, SourceType>
        (
            SourceType srcObj,
            Boolean defaultValueInsteadOfNull = false,
            bool rstCryptography = true
        )
            where ReturnType : class, new()
            where SourceType : class, new()
        {
            ReturnType retObj = new ReturnType();

            var srcProps = srcObj.GetType().GetProperties();
            var retProps = retObj.GetType().GetProperties();

            for (int propInx = 0; propInx < srcProps.Count(); propInx++)
            {
                var retExistProp = retProps.FirstOrDefault(p => p.Name.Equals(srcProps[propInx].Name));

                if (retExistProp != null)
                {
                    if (retExistProp.PropertyType.Equals(srcProps[propInx].PropertyType))
                    {
                        var noCryptography = true;

                        if (rstCryptography)
                        {
                            noCryptography = false;
                            string cryptData = "";
                            //var hasDataProtection = retExistProp.GetCustomAttributes(typeof(RSTCryptographyAttribute), true);
                            var hasDataProtection = srcProps[propInx].GetCustomAttributes(typeof(RSTCryptographyAttribute), true);
                            if (hasDataProtection.Any())
                            {
                                switch (((RSTCryptographyAttribute)hasDataProtection.First()).Mode)
                                {
                                case Mode.Encryption:
                                    cryptData = DecoderUtil.Encrypt(srcProps[propInx].GetValue(srcObj).ToString());
                                    break;

                                case Mode.Decryption:
                                    cryptData = DecoderUtil.Decrypt(srcProps[propInx].GetValue(srcObj).ToString());
                                    break;

                                case Mode.None:
                                    noCryptography = true;
                                    break;
                                }

                                if (!string.IsNullOrEmpty(cryptData) && !noCryptography)
                                {
                                    retExistProp.SetValue
                                    (
                                        retObj,
                                        TConverter.ChangeType(srcProps[propInx].PropertyType, cryptData),
                                        null
                                    );
                                }
                            }
                            else
                            {
                                noCryptography = true;
                            }
                        }

                        if (noCryptography)
                        {
                            retExistProp.SetValue
                            (
                                retObj,
                                TConverter.ChangeType(srcProps[propInx].PropertyType, srcProps[propInx].GetValue(srcObj)),
                                null
                            );
                        }
                    }
                }
            }

            return(retObj);
        }