示例#1
0
        /// <summary>
        /// Call given method and return its returned value
        /// </summary>
        /// <param name="_current">Object to call into</param>
        /// <param name="_methodName">Method to call</param>
        /// <param name="_parameters">Parms to pass into method</param>
        /// <returns>Returned object from called Method</returns>
        public static object GetMethodValue(object _current, string _methodName,
                                            object[] _parameters)
        {
            try
            {
                object obj = null;

                if (_parameters == null)
                {
                    Type       type   = _current.GetType();
                    MethodInfo method = type.GetMethod(_methodName);
                    obj = method.Invoke(_current, _parameters);
                }
                else
                {
                    Type       type   = _current.GetType();
                    Type[]     types  = GetTypes(_parameters);
                    MethodInfo method = type.GetMethod(_methodName, types);
                    obj = method.Invoke(_current, _parameters);
                }
                return(obj);
            }
            catch (Exception ex)
            {
                TraceFileHelper.Error("Exception : MethodName-" + _methodName);
                throw ex;
            }
        }
示例#2
0
        /// <summary>
        /// Return value of given object property, using defaultvalue if not found
        /// </summary>
        /// <param name="_current">Object to search</param>
        /// <param name="_propertyName">Property to search</param>
        /// <param name="_defaultValue">Default value if property not found</param>
        /// <returns>String property value</returns>
        public static string GetPropertyValue(object _current, string _propertyName,
                                              string _defaultValue)
        {
            try
            {
                Type type = _current.GetType();

                PropertyInfo property = type.GetProperty(_propertyName);

                if (property != null)
                {
                    string propertyValue = (string)property.GetValue(_current, BindingFlags.Public, null, null, null);

                    if (propertyValue == null)
                    {
                        return(_defaultValue);
                    }

                    return(propertyValue);
                }

                return(_defaultValue);
            }
            catch (Exception e)
            {
                TraceFileHelper.Error("Exception in getPropertyValue : PropertyName-" + _propertyName);
                throw e;
            }
        }
示例#3
0
        public static T DeserializeObject <T>(string _xml)
        {
            XmlSerializer ser = new XmlSerializer(typeof(T));
            MemoryStream  ms  = null;

            T output = default(T);

            try
            {
                byte[] arr = System.Text.Encoding.ASCII.GetBytes(_xml);

                ms = new MemoryStream(arr, false);

                output = (T)ser.Deserialize(ms);
            }
            catch (Exception ex)
            {
                TraceFileHelper.Exception(ex.ToString());
            }
            finally
            {
                if (ms != null)
                {
                    ms.Close();
                }
            }

            return(output);
        }
示例#4
0
        public static object DeserializeObject(Type _outputType, string _xml)
        {
            XmlSerializer ser = new XmlSerializer(_outputType);
            MemoryStream  ms  = null;

            Object output = null;

            try
            {
                byte[] arr = System.Text.Encoding.ASCII.GetBytes(_xml);

                ms = new MemoryStream(arr, false);

                output = ser.Deserialize(ms);
            }
            catch (Exception ex)
            {
                TraceFileHelper.Exception(ex.ToString());
            }
            finally
            {
                if (ms != null)
                {
                    ms.Close();
                }
            }

            return(output);
        }
示例#5
0
        public static void SendMessage(MailMessage _mail)
        {
            try
            {
                SmtpClient client = new SmtpClient();
                client.Host = Config.GetSettingValue("SMTP_HOST");


                string userName = Config.GetSettingValue("SMTP_USER");
                string password = Config.GetSettingValue("SMTP_PASSWORD");
                string domain   = Config.GetSettingValue("SMTP_DOMAIN");

                NetworkCredential credentials = new NetworkCredential(userName, password, domain);
                client.Credentials = credentials;
                try
                {
                    client.Send(_mail);
                    string logMessage = String.Format("Email sent to {0} with subject {1}", _mail.To[0].User,
                                                      _mail.Subject);
                    TraceFileHelper.Info(logMessage);
                }
                catch (Exception ex)
                {
                    TraceFileHelper.Exception(ex.ToString());

                    string logMessage = String.Format("Unable to send email to {0} with subject {1}", _mail.To[0].User,
                                                      _mail.Subject);
                    TraceFileHelper.Info(logMessage);
                }
            }
            catch (Exception ex)
            {
                TraceFileHelper.Exception(ex);
            }
        }
示例#6
0
        public static void SendMessage(string _from, string _to,
                                       string _subject, string _body)
        {
            try
            {
                MailMessage mm = new MailMessage(_from, _to);
                mm.Subject    = _subject;
                mm.Body       = _body;
                mm.IsBodyHtml = true;

                SendMessage(mm);
            }
            catch (Exception ex)
            {
                TraceFileHelper.Error(ex.ToString());
            }
        }
示例#7
0
        /// <summary>
        /// Call given Method of given Class in given Assembly
        /// </summary>
        /// <param name="_assemblyName">Assembly housing class</param>
        /// <param name="_className">Class to create</param>
        /// <param name="_methodName">Method to invoke</param>
        /// <param name="_parameters">Parameters to pass to Method</param>
        /// <returns>string value from called Method</returns>
        public static string InvokeFunction(string _assemblyName, string _className,
                                            string _methodName, object[] _parameters)
        {
            try
            {
                Assembly assembly = GetAssembly(_assemblyName);
                Type     type     = assembly.GetType(_className);
                object   o        = Activator.CreateInstance(type);

                MethodInfo method = type.GetMethod(_methodName);

                return((string)method.Invoke(o, BindingFlags.Public, null, _parameters, null));
            }
            catch (Exception e)
            {
                TraceFileHelper.Error("Exception in invokeFunction : Assembly-" + _assemblyName + ", ClassName-" +
                                      _className + ", MethodName-" + _methodName + ", ParametersCount-" +
                                      _parameters.GetLength(0));
                throw e;
            }
        }
示例#8
0
        public static object DeserializeObject(Type _outputType, Stream _data)
        {
            XmlSerializer ser = new XmlSerializer(_outputType);

            Object output = null;

            try
            {
                output = ser.Deserialize(_data);
            }
            catch (Exception ex)
            {
                TraceFileHelper.Exception(ex.ToString());
            }
            finally
            {
                // Don't close stream here the calling method is responsible
            }

            return(output);
        }
示例#9
0
        /// <summary>
        /// Return value of given object property
        /// </summary>
        /// <param name="_current">Object to search</param>
        /// <param name="_propertyName">Property to search</param>
        /// <returns>String property value</returns>
        public static string GetPropertyValue(object _current, string _propertyName)
        {
            try
            {
                Type type = _current.GetType();

                PropertyInfo property = type.GetProperty(_propertyName);

                if (property != null)
                {
                    return(Convert.ToString(property.GetValue(_current, BindingFlags.Public, null, null, null)));
                }
                else
                {
                    throw new Exception("Property does not exist - " + _propertyName);
                }
            }
            catch (Exception e)
            {
                TraceFileHelper.Error("Exception in getPropertyValue : PropertyName-" + _propertyName);
                throw e;
            }
        }
示例#10
0
        public static void SetPropertyValue(ref object _current, string _propertyName,
                                            object _propertyValue)
        {
            try
            {
                Type type = _current.GetType();

                PropertyInfo property = type.GetProperty(_propertyName);

                if (property != null)
                {
                    property.SetValue(_current, _propertyValue, BindingFlags.Public, null, null, null);
                }
                else
                {
                    throw new Exception("Property does not exist - " + _propertyName);
                }
            }
            catch (Exception e)
            {
                TraceFileHelper.Error("Exception in getPropertyValue : PropertyName-" + _propertyName);
                throw e;
            }
        }
示例#11
0
        /// <summary>
        /// See if given path is writeable
        /// </summary>
        /// <param name="_path">Path to test</param>
        /// <returns>True if writeable, False if not</returns>
        public static bool TestFolderPermissions(string _path)
        {
            StreamWriter sw   = null;
            string       file = "";
            bool         rc   = true;

            try
            {
                //Create path if it doesn't exist
                BuildFolderIfMissing(_path);

                //Create a file in the folder
                file = _path + "permission.txt";
                if (File.Exists(file))
                {
                    File.Delete(file);
                }

                sw = new StreamWriter(file)
                {
                    AutoFlush = true
                };
                sw.WriteLine("test");

                //Close and delete the file
                sw.Close();
                File.Delete(file);
            }
            catch (Exception ex)
            {
                rc = false;
                TraceFileHelper.Error(string.Format("Folder Permissions error - {0}", ex.ToString()));
            }

            return(rc);
        }
示例#12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="_srcdir"></param>
        /// <param name="_destdir"></param>
        /// <param name="_recursive"></param>
        /// <param name="_ignoreExtension"></param>
        public static void FileCopy(
            string _srcdir,
            string _destdir,
            bool _recursive,
            string[] _ignoreExtension)
        {
            DirectoryInfo dir;

            FileInfo[]      files;
            DirectoryInfo[] dirs;
            string          tmppath;

            //determine if the destination directory exists, if not create it
            if (!Directory.Exists(_destdir))
            {
                Directory.CreateDirectory(_destdir);
            }

            dir = new DirectoryInfo(_srcdir);

            //if the source dir doesn't exist, throw
            if (!dir.Exists)
            {
                throw new ArgumentException("source dir doesn't exist -> " + _srcdir);
            }

            //get all files in the current dir
            files = dir.GetFiles();

            //loop through each file
            foreach (FileInfo file in files)
            {
                bool match = false;
                if (_ignoreExtension != null)
                {
                    foreach (string s in _ignoreExtension)
                    {
                        if (s.Equals(file.Extension, StringComparison.OrdinalIgnoreCase))
                        {
                            match = true;
                            continue;
                        }
                    }
                }

                if (!match)
                {
                    //create the path to where this file should be in destdir
                    tmppath = Path.Combine(_destdir, file.Name);

                    //copy file to dest dir
                    file.CopyTo(tmppath, false);
                }
                else
                {
                    TraceFileHelper.Verbose(String.Format("Skipping ignored file ({0})", file.FullName));
                }
            }

            //cleanup
            files = null;

            //if not recursive, all work is done
            if (!_recursive)
            {
                return;
            }

            //otherwise, get dirs
            dirs = dir.GetDirectories();

            //loop through each sub directory in the current dir
            foreach (DirectoryInfo subdir in dirs)
            {
                bool match = false;
                if (_ignoreExtension != null)
                {
                    foreach (string s in _ignoreExtension)
                    {
                        if (s.Equals(subdir.Extension, StringComparison.OrdinalIgnoreCase))
                        {
                            match = true;
                            continue;
                        }
                    }
                }

                if (!match)
                {
                    //create the path to the directory in destdir
                    tmppath = Path.Combine(_destdir, subdir.Name);

                    //recursively call this function over and over again
                    //with each new dir.
                    FileCopy(subdir.FullName, tmppath, _recursive, _ignoreExtension);
                }
                else
                {
                    TraceFileHelper.Verbose(String.Format("Skipping directory file ({0})", dir.FullName));
                }
            }

            //cleanup
            dirs = null;

            dir = null;
        }