TypeInitializationException未处理#2

时间:2015-04-30 01:04:50

标签: c#

我为我创建的应用程序创建了一个DLL。 但是当我将DLL作为参考添加到控制台应用程序但是不知道这意味着这是错误时,我收到了错误:

未处理的类型' System.TypeInitializationException'发生在ConsoleApplication1.exe

这是我的dll课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Steap
{
    public class SteapAPI
    {
        public static String URL
        {
            get;
            set;
        }

        public static XmlReader r = XmlReader.Create(URL + "?xml=1&l=english");

        public int getSteamID64()
        {
            int ID = 0;
            r.ReadToFollowing("steamID64");
            ID = r.ReadContentAsInt();
            return ID;
        }

        public string getSteamID()
        {
            string ID = String.Empty;
            r.ReadToFollowing("steamID");
            ID = r.ReadContentAsString();
            return ID;
        }

        public int getVac()
        {
            int Vac = 0;
            r.ReadToFollowing("vacBanned");
            Vac = r.ReadContentAsInt();
            return Vac;
        }

        public bool hasVac()
        {
            if (getVac() == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        // =================== [ Aliases

        public string getName()
        {
            return getSteamID();
        }
    }
}

控制台应用程序代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Steap;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SteapAPI sapi = new SteapAPI(); // TypeInitializationException was unhandled error here
            SteapAPI.URL = "http://steamcommunity.com/id/bluesephire";
            Console.ReadKey();
        }
    }
}

有什么问题或缺少什么

2 个答案:

答案 0 :(得分:3)

在类的静态字段初始化期间,您会遇到异常,导致无法加载类,从而导致TypeInitializationException异常。

特殊线:

public static XmlReader r = XmlReader.Create(URL + "?xml=1&l=english");

URL在调用的时间方法中未初始化(即使它具有静态值,如URL=@"c:\file.txt",也无法保证首先初始化一个字段。

从那时起,请注意,SteapAPI类的任何访问都会抛出TypeInitializationException,即使它没有触及直接涉及原始异常的字段。

答案 1 :(得分:1)

在这种情况下,您不应该使用静态字段。如果您创建了两个SteapAPI对象,静态字段将导致巨大的问题,因为当您设置一个URL时,它将覆盖另一个,并且您永远无法重新初始化XmlReader

以下是如何将API类重写为完整的实例类:

namespace Steap
{
    public class SteapAPI
    {
        public String URL
        {
            get;
            set;
        }

        public XmlReader r;

        public SteapAPI(string url)
        {
            URL = url;
            //NOTE: This is wrong! You can't create an XmlReader with a URL
            //and expect it to fetch a web resource.
            r = XmlReader.Create(URL + "?xml=1&l=english");
        }

        public int getSteamID64()
        {
            int ID = 0;
            r.ReadToFollowing("steamID64");
            ID = r.ReadContentAsInt();
            return ID;
        }

        public string getSteamID()
        {
            string ID = String.Empty;
            r.ReadToFollowing("steamID");
            ID = r.ReadContentAsString();
            return ID;
        }

        public int getVac()
        {
            int Vac = 0;
            r.ReadToFollowing("vacBanned");
            Vac = r.ReadContentAsInt();
            return Vac;
        }

        public bool hasVac()
        {
            if (getVac() == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        // =================== [ Aliases

        public string getName()
        {
            return getSteamID();
        }
    }

然后在你的程序中使用它:

class Program
{
    static void Main(string[] args)
    {
        SteapAPI sapi = new SteapAPI("http://steamcommunity.com/id/bluesephire");
        Console.ReadKey();
    }
}

它是一个微小的变化,但好处是巨大的,你应该学习更多关于使用构造函数和静态字段/属性的缺点,因为它适用于多个实例。请记住,非静态类的静态字段/属性是在类的所有“实例”之间共享,因此设置一个会将该类的所有“实例”设置为新值。这在执行I / O操作和文件/资源​​读/写时尤为重要。

相关问题