错误:无法推断类型参数“T" [英] Error: Type parameter 'T' cannot be inferred

查看:28
本文介绍了错误:无法推断类型参数“T"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从事过一个 C# 项目,现在我基本上在做同样的项目,但在 vb.net 中.

I previously worked on a C# project and now I'm basically doing the same project but in vb.net.

C# 代码:经理类

 /// <summary>
    /// Reads file from binary
    /// </summary>
    /// <param name="the filepath"></param>
    /// <returns></returns>
    public Animal[] ReadFile(string filename)
    {
        BinSerializerUtility BinSerial = new BinSerializerUtility();
        var animals = BinSerial.BinaryFileDeSerialize<Animal>(filename);
        return animals.ToArray();
    }

类 BinaryFileDeSerialize

class BinaryFileDeSerialize

public List<T> BinaryFileDeSerialize<T>(string filePath) where T : class
    {
        var list = new List<T>();

        if (!File.Exists(filePath))
            throw new FileNotFoundException("The file" + " was not found. ", filePath);
        using (var fileStream = new FileStream(filePath, FileMode.Open))
        {
            BinaryFormatter b = new BinaryFormatter();

            while (fileStream.Position < fileStream.Length)
                list.Add((T)b.Deserialize(fileStream));
        }


        return list;
    }

在 C# 中一切正常,但是在 vb.net 中出现问题.

Everything worked fine in C#, however in the vb.net there was a problem.

我尝试在 vb.net 中这样做:

I tried doing it this way in vb.net:

经理类:

 Public Function ReadFile(ByVal filename As String) As Animal()
            Dim BinSerial As BinSerializerUtility = New BinSerializerUtility
            Dim animals = BinSerial.BinaryFileDeSerialize(filename)
            Return animals.ToArray
        End Function

从上面的代码我得到一个错误:

From the code above I'm getting an error:

'Public Function BinaryFileDeSerialize(Of T As)的类型参数'T'Class)(filePath As String) As System.Collections.Generic.List(Of T)'无法推断.

Type parameter 'T' for 'Public Function BinaryFileDeSerialize(Of T As Class)(filePath As String) As System.Collections.Generic.List(Of T)' cannot be inferred.

vb.net 类 BinaryFileDeSerialize:

vb.net class BinaryFileDeSerialize:

  Public Function BinaryFileDeSerialize(Of T As {Class})(ByVal filePath As String) As List(Of T)
            Dim list = New List(Of T)
            If Not File.Exists(filePath) Then
                Throw New FileNotFoundException(("The file" + " was not found. "), filePath)
            End If
            Dim fileStream = New FileStream(filePath, FileMode.Open)
            Dim b As BinaryFormatter = New BinaryFormatter

            While (fileStream.Position < fileStream.Length)
                list.Add(CType(b.Deserialize(fileStream), T))

            End While
            Return list
        End Function

有谁知道问题是什么以及如何解决它?

Does anyone know what the problem is and how to solve it?

推荐答案

您需要致电

Dim animals = BinSerial.BinaryFileDeSerialize(Of <put your type here>)(filename)

因此,如果您的类型是 Animal*,请使用

So if your type is Animal*, use

Dim animals = BinSerial.BinaryFileDeSerialize(Of Animal)(filename)

*在您最近的编辑之后

从一开始,您就可以帮助您的项目避免与类型相关的复杂性.将 Option Explicit 作为第一行你的类文件.它将要求您使用
声明每个变量作为<type>,它的回报是应用程序可以更容易阅读、维护并避免与类型相关的错误.

Already from the beginning, you can help your project to be without type-related complications. Write Option Explicit as the first line of your class file. It will require you to declare every variable with
As <type> and its pay-off is aplication which can be more easily read, maintained and kept free from type-related errors.

所以在添加Option Explicit之后,你必须添加类型声明:

So after adding Option Explicit, you have to add type declaration:

Dim animals As List(Of Animal) = BinSerial.BinaryFileDeSerialize(Of Animal)(filename)

您可以立即更好地了解自己在做什么.

You immediately get better overview of what you are doing.

这篇关于错误:无法推断类型参数“T"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆