Question

I have a class library that contains static class Utils with few properties.

I call one of the properties from the Utils class into my Console application and I get this error. Here is a small sample of the Utils class.

public static class Utils
{
    public static int CurrentEmpId = -1;
    public static int CurrentUserId
    {
        get
        {
            if (HttpContext.Current != null)
            {
                 if(HttpContext.Current.Session["CurrentUserId"] == null)
                 {
                     HttpContext.Current.Session["CurrentUserId"] = GetCurrentUser();
                     return Int32.Parse(HttpContext.Current.Session["CurrentUserId"]);
                 }
                 else
                 {
                     return Int32.Parse(HttpContext.Current.Session["CurrentUserId"]);
                 }
            }

            return -1;
        }
    }
    //this is making call to a static Method in a static Class called _
    public static string RowHeader = _.T("Some Header");
}

When i try to take the CurrentUserId property in my console app i get the exception. I commented out the public static string RowHeader = _.T("Some Header"); code and the exception was gone. In either of the cases i dont have compiler or build errors.

The "_" named class is this one with a few modifications: Translation Class

My question is why is the CurrentUserId property is throws exception because of the RowHeader one?

Was it helpful?

Solution

To answer your question, all Static members can be evaluated when the first class reference happens. (this is an understatement and over simplification) It depends on .NET version, presence of Static constructors, Lazy variables etc. but in this it looks like you may be hitting the issue, where the first reference to a member also initializes other members and one of that causes the exception.

for the error, inspect the InnerException property of a type initializer exception to get the details of the exact error. It is the best indication of the error.

OTHER TIPS

Try setting _.ResourceCulture first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top