开发者

TypeInitializationException received

I have a static class.

static class AppDirectory
{

    public static string PACSTEMP = Path.Combine(Path.GetTempPath() ,"PacsTemp");//@"C:\MyTemp";

    public static string ImageTempDirectory = Pa开发者_StackOverflowth.Combine(Path.GetTempPath(), "ImageRetrieveTemp");//@"C:\ImageRetrieveTemp\";

    static AppDirectory()
    {


        if (Directory.Exists(PACSTEMP))
            Directory.Delete(PACSTEMP);

        if (Directory.Exists(ImageTempDirectory))
            Directory.Delete(ImageTempDirectory);

        Directory.CreateDirectory(PACSTEMP);
        Directory.CreateDirectory(ImageTempDirectory);
    }
}

It is a static class and have one static constructor. When I tried to use PACSTEMP and ImageTempDirectory variables, It is showing Type Initialization error. I Understand that it is because, AppDirectory is not initialized.

Could you please help me, how to initialize these kind of classes. or have I missed rules of OOPs.

Thank you


the field initializers happen before the ctor, so it should be fine. I suspect this is something like a permissions error. Look at the .InnerException to see exactly what:

try {
    // something that uses AppDirectory, causing the error
} catch (TypeInitializationException ex) {
    Trace.WriteLine(ex.InnerException);
    throw;
}

It could also be that the delete is failing because the directory you are deleting isn't empty.


Apparently your static constructor throws some kind of exception during execution.

To quote from the documentation of TypeInitializationException:

When a class initializer fails to initialize a type, a TypeInitializationException is created and passed a reference to the exception thrown by the type's class initializer. The InnerException property of TypeInitializationException holds the underlying exception.

So, you should have a look at the InnerException of your TypeInitializationException to find out what really went wrong.


Doing IO related code (open/create/delete files/folders) in static constructor is asking for trouble - such operations are expected to fail fairly regularly.

Either catch all IOExceptions and do something about it OR better initialize this object in startup code instead of constructor.

Read on dependency injection for better approach to provide shared services to application.


I would ask why do you need a static constructor, you can replace the constructor with a static method and call the static method instead of static constructor. Reason : Static const will be called each time you 1. Call any method of the static class 2. Initialize any sttic member value 3. Initialize the object etc.

It wont be called auto, it will be called only whenever you refer any member/method of the class. So if you have 10 methods and 10 variables, assuming you call all members only once (10+10), so your static constructor will be called 20 times.

Don't you think instead its much better to just replace the static constr with static method and call that method first in your app, so your directory variables are set.

Hope this helps. If not, do some Google to understand static constructor much better

REG the error: I recommend to put a try--catch block and see what causes the exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜