C# TypeInitializationException未处理#2

C# TypeInitializationException未处理#2,c#,C#,我正在为我创建的应用程序创建一个DLL。 但是,当我将DLL作为引用添加到控制台应用程序时,出现了一个错误,但不知道这是什么意思这是一个错误: ConsoleApplication1.exe中发生类型为“System.TypeInitializationException”的未处理异常 这是我的dll类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst

我正在为我创建的应用程序创建一个DLL。 但是,当我将DLL作为引用添加到控制台应用程序时,出现了一个错误,但不知道这是什么意思这是一个错误:

ConsoleApplication1.exe中发生类型为“System.TypeInitializationException”的未处理异常

这是我的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();
        }
    }
}

什么错误或缺少什么

在初始化类的静态字段时出现异常,导致加载类失败,因此出现
TypeInitializationException
异常

特定行:

public static XmlReader r = XmlReader.Create(URL + "?xml=1&l=english");
URL
在调用方法时未初始化(即使它具有静态值,如
URL=@“c:\file.txt”
也不能保证首先初始化一个字段


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

在这种情况下,您不应该使用静态字段。如果您创建两个
SteapAPI
对象,静态字段将导致巨大的问题ts,当您设置一个URL时,它将覆盖另一个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操作和文件/资源读/写时,这一点尤其重要。

您能告诉我们您在哪一行得到错误吗?您不能执行
公共静态XmlReader r=XmlReader.Create(URL+“?xml=1&l=english”)
因为
URL
没有被分配。Ron Beyer,在控制台应用程序中通过“SteapAPI sapi=new SteapAPI();“我已经编辑了你的标题。请看,”,其中的共识是“不,他们不应该”。那么我如何正确地解决这个问题?有没有办法把它放在这行:SteapAPI sapi=new SteapAPI(这里)@Joey Devries最简单的方法是,如果它对你有用的话,就不要让它们成为静态的……如果不让整个类成为单件(或者仅仅是你感兴趣的领域)使用
Lazy
类可能是一种选择。通过非静态方式,我无法访问URL,或者我从未听说过Lazy,谷歌也会提供我的MSDN,但无法获取it@JoeydeVries是的,您可以通过sapi.URL访问它,而且它应该是一个实例属性,而不是静态的,因为如果您使用两个API副本,其中一个将用dif覆盖另一个不同的URL。@Joey Devries您必须使XMLReader也成为一个实例字段(不是静态的),而不仅仅是URL属性。当我像调用任何方法一样调用时,它会给出:System.Xml中出现“System.InvalidOperationException”类型的未处理异常。dll@JoeydeVries因为XmlReader.Create()不带网址,您需要在创建读卡器之前下载该文件。它不应命名为URL,应命名为URI,并且它是您必须传递给它的本地资源文件。好的,谢谢您的帮助,我现在将尝试!但它是一个dll,我无法下载文件,因此我需要在本地下载该文件,但在哪里?您需要形成entire请求(使用?xml=…)并发出WebRequest或HttpRequest以获取查询返回的数据,然后将该xml保存到文件中,并将其传递到API中。请参阅和