/// <summary>
 /// Generates a standard file name to be used for the configuration file
 /// </summary>
 /// <returns></returns>
 private string GetConfigurationFileName()
 {
     if (string.IsNullOrEmpty(ConfigurationFileName))
     {
         return(StringHelper.AddBS(StringHelper.JustPath(System.Reflection.Assembly.GetExecutingAssembly().Location)) + "App.sconfig");
     }
     return(ConfigurationFileName);
 }
Exemple #2
0
 /// <summary>Makes sure the secified path ends with a back-slash</summary>
 /// <param name="path">Path</param>
 /// <returns>Path with BS</returns>
 /// <example>
 /// @"c:\folder".AddBS(); // returns @"c:\folder\"
 /// @"c:\folder\".AddBS(); // returns @"c:\folder\"
 /// </example>
 public static string AddBS(this string path)
 {
     return(StringHelper.AddBS(path));
 }
        /// <summary>
        /// Loads a named object from an assembly
        /// </summary>
        /// <param name="className">Fully qualified name of the class</param>
        /// <param name="assemblyName">Assembly name (preferrably the fully or partially qualified name, or the file name)</param>
        /// <returns>Newly instantiated object</returns>
        /// <example>SqlDataService oService = (SqlDataService)ObjectHelper.CreateObject("EPS.Data.SqlClient","SqlDataService")</example>
        public static object CreateObject(string className, string assemblyName)
        {
            Assembly assembly = null;

            if (assemblyName.ToLower(CultureInfo.InvariantCulture).EndsWith(".dll") || assemblyName.ToLower(CultureInfo.InvariantCulture).EndsWith(".exe"))
            {
                try
                {
                    assembly = Assembly.LoadFrom(assemblyName);
                }
                catch
                {
                    if (assemblyName.IndexOf("\\", StringComparison.Ordinal) < 0)
                    {
                        string location;
                        try
                        {
                            location = ((Assembly.GetEntryAssembly() != null) ? Assembly.GetEntryAssembly().Location : Assembly.GetAssembly(typeof(ObjectHelper)).Location);
                        }
                        catch
                        {
                            try
                            {
                                location = Assembly.GetAssembly(typeof(ObjectHelper)).Location;
                            }
                            catch
                            {
                                throw new ObjectInstantiationException("Unable to find assemblie's location(" + assemblyName + ")");
                            }
                        }
                        string startPath = StringHelper.AddBS(StringHelper.JustPath(location));
                        assemblyName = startPath + assemblyName;
                        try
                        {
                            assembly = Assembly.LoadFrom(assemblyName);
                        }
                        catch (Exception ex)
                        {
                            throw new ObjectInstantiationException("Unable to load assembly " + assemblyName + ". Error: " + ex.Message, ex);
                        }
                    }
                }
            }
            else if (assemblyName.IndexOf(",", StringComparison.Ordinal) > -1)
            {
                try
                {
                    assembly = Assembly.Load(assemblyName);
                }
                catch (Exception ex)
                {
                    throw new ObjectInstantiationException("Unable to load assembly " + assemblyName + ". Error: " + ex.Message, ex);
                }
            }
            else
            {
                try
                {
                    assembly = Assembly.Load(assemblyName);
                }
                catch (Exception ex)
                {
                    throw new ObjectInstantiationException("Unable to load assembly " + assemblyName + ". Error: " + ex.Message, ex);
                }
            }
            Type objectType;

            try
            {
                if (!(assembly != null))
                {
                    throw new ObjectInstantiationException("Unable to create instance " + className + ". Error: Unable to load assembly.");
                }
                objectType = assembly.GetType(className, true);
            }
            catch (Exception ex)
            {
                throw new ObjectInstantiationException("Unable to get type " + className + ". Error: " + ex.Message, ex);
            }
            object result;

            try
            {
                result = Activator.CreateInstance(objectType);
            }
            catch (Exception ex)
            {
                throw new ObjectInstantiationException("Unable to create instance " + className + ". Error: " + ex.Message, ex);
            }
            return(result);
        }