System.TypeLoadException Microsoft.VisualBasic ASP.NET Core 2
Microsoft.VisualBasic程序集是否与ASP.NET Core2不兼容?System.TypeLoadException Microsoft.VisualBasic ASP.NET Core 2
我有一个C#类库,提供了一个读取CSV文件的方法,我选择使用Microsoft.VisualBasic.FileIO.TextFieldParser来读取文件。在WPF应用程序中引用该库方法时效果很好。然而,在ASP.NET的Core2 Web服务,但它编译没有错误,它会引发在运行时异常:
异常 System.TypeLoadException未能从程序集加载类型“Microsoft.VisualBasic.FileIO.TextFieldParser” 'Microsoft.VisualBasic,Version = 10.0.3.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'。
我怀疑这是因为与Core的服务不兼容。我曾尝试将编译器标记从NETCOREAPP2更改为NET461,但Web服务仍然抛出异常。
回答:
[这是不完全的答案,但它是在发生上述组件的有效的解决方案。]
我建立这个类来代替由FileIO.TextFieldParser在Microsoft.VisualBasic程序提供的功能和以符合API。以下仅提供我需要的功能,以便根据需要进行扩展。
public class TextFieldParser : StreamReader, IDisposable {
int iToken = 1;
bool quoted = false;
char[] delimiters;
string curLine;
public TextFieldParser(string path) : base(path) { }
public TextFieldParser(Stream stream) : base(stream) { }
public string[] ReadFields()
{
curLine = ReadLine();
return GetFields();
}
public void SetDelimiters(string delim)
{
delimiters = delim.ToCharArray();
}
public string[] GetFields()
{
if (delimiters == null || delimiters.Length == 0)
throw new Exception($"{GetType().Name} requires delimiters be defined to identify fields.");
if (!hasFieldsEnclosedInQuotes)
{
return curLine.Split(delimiters);
}
else
{
var token = (char)iToken;
var sb = new StringBuilder();
// Go through the string and change delimiters to token
// ignoring them if within quotes if indicated
for (int c = 0; c < curLine.Length; c++)
{
var qc = curLine[c];
if (hasFieldsEnclosedInQuotes && qc == '"')
{
quoted = !quoted;
continue;
}
else if (!quoted)
{
// Replace the delimiters with token
for (int d = 0; d < delimiters.Length; d++)
{
if (qc == delimiters[d])
{
qc = token;
break;
}
}
}
sb.Append(qc);
}
return sb.ToString().Split(token);
}
}
private bool hasFieldsEnclosedInQuotes = false;
public bool HasFieldsEnclosedInQuotes
{
get { return hasFieldsEnclosedInQuotes; }
set { hasFieldsEnclosedInQuotes = value; }
}
public bool EndOfData
{
get { return EndOfStream; }
}
以上是 System.TypeLoadException Microsoft.VisualBasic ASP.NET Core 2 的全部内容, 来源链接: utcz.com/qa/266777.html