Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is static constructor threadsafe?
Message
From
04/06/2009 09:00:10
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
 
To
04/06/2009 04:39:01
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01391935
Message ID:
01403643
Views:
51
Is this one of those times when using the Singleton Pattern makes sense?
private static Evaluator instance = null;
public static Evaluator Instance
{
   get
   {
      if(instance == null)
         instance = new Evaluator();
      return instance;
   }
}

// private constructor
private Evaluator()
{
}

// Access this from your classes with
Evaluator.Instance.MyNonStaticMethod();
But are all your methods in this class static?
Tim

>Hi,
>Below is my code.
>
>Original code using static constructor
>
>    public class Evaluator
>    {
>        private static object _evaluator = null;
>        private static Type _evaluatorType = null;
>
>        private static object _lockObject = new object();
>        private static readonly string _jscriptSource = @"
>                                                        package Evaluator
>                                                        {
>                                                            class Evaluator
>                                                            {
>                                                                public function Eval(expr : String) : String
>                                                                {
>                                                                    return eval(expr,""unsafe"");
>                                                                }
>                                                            }
>                                                        }
>                                                        ";
>
>        static Evaluator()
>        {
>            CodeDomProvider provider = CodeDomProvider.CreateProvider("JScript");
>
>            CompilerParameters parameters = new CompilerParameters();
>            parameters.GenerateInMemory = true;
>
>            CompilerResults results = provider.CompileAssemblyFromSource(parameters, _jscriptSource);
>
>            Assembly assembly = results.CompiledAssembly;
>           _evaluatorType = assembly.GetType("Evaluator.Evaluator");
>
>            _evaluator = Activator.CreateInstance(_evaluatorType);
>        }
>
>        public static object EvalToObject(string expression)
>        {
>            object result;
>
>            lock (_lockObject)
>            {
>                try
>                {
>                    result = _evaluatorType.InvokeMember(
>                        "Eval",
>                        BindingFlags.InvokeMethod,
>                        null,
>                        _evaluator,
>                        new object[] { expression }
>                        );
>
>                    if (result.ToString().ToLower() == "undefined")
>                    {
>                        throw new Exception(string.Format("Evaluator: Invalid expression {0}", expression));
>                    }
>                }
>                catch (Exception ex)
>                {
>                    throw new Exception(string.Format("Evaluator: Failed to evaluate expression {0} - {1}", expression, ex.Message));
>                }
>
>                return result;
>            }
>        }
>
>        public static string Eval(string expression)
>        {
>            object o = EvalToObject(expression);
>            return o.ToString();
>        }
>    }
>
>
>My workaround to prevent error whenever more than one request access the same code.
>
>    public class Evaluator
>    {
>        private static object _evaluator = null;
>        private static Type _evaluatorType = null;
>
>        private static object _lockObject = new object();
>        private static readonly string _jscriptSource = @"
>                                                        package Evaluator
>                                                        {
>                                                            class Evaluator
>                                                            {
>                                                                public function Eval(expr : String) : String
>                                                                {
>                                                                    return eval(expr,""unsafe"");
>                                                                }
>                                                            }
>                                                        }
>                                                        ";
>
>        private static void CreateEvaluator()
>        {
>            if (_evaluator != null) return;
>
>            CodeDomProvider provider = CodeDomProvider.CreateProvider("JScript");
>
>            CompilerParameters parameters = new CompilerParameters();
>            parameters.GenerateInMemory = true;
>
>            CompilerResults results = provider.CompileAssemblyFromSource(parameters, _jscriptSource);
>
>            Assembly assembly = results.CompiledAssembly;
>           _evaluatorType = assembly.GetType("Evaluator.Evaluator");
>
>            _evaluator = Activator.CreateInstance(_evaluatorType);
>        }
>
>        public static object EvalToObject(string expression)
>        {
>            object result;
>
>            lock (_lockObject)
>            {
>                CreateEvaluator();
>
>                try
>                {
>                    result = _evaluatorType.InvokeMember(
>                        "Eval",
>                        BindingFlags.InvokeMethod,
>                        null,
>                        _evaluator,
>                        new object[] { expression }
>                        );
>
>                    if (result.ToString().ToLower() == "undefined")
>                    {
>                        throw new Exception(string.Format("Evaluator: Invalid expression {0}", expression));
>                    }
>                }
>                catch (Exception ex)
>                {
>                    throw new Exception(string.Format("Evaluator: Failed to evaluate expression {0} - {1}", expression, ex.Message));
>                }
>
>                return result;
>            }
>        }
>
>        public static string Eval(string expression)
>        {
>            object o = EvalToObject(expression);
>            return o.ToString();
>        }
>    }
>
>
>Please advice. Thank you
Timothy Bryan
Previous
Reply
Map
View

Click here to load this message in the networking platform