转换功能< T>从C#到VB.NET [英] Converting function <T> from C# to VB.NET

查看:249
本文介绍了转换功能< T>从C#到VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这行C#代码,我想换到VB.NET:

 公共静态字符串functionx< RT> ;(RT REQ){} 

是以下正确的翻译

 公共功能functionx(中RT)(REQ作为RT)作为字符串


解决方案

要声明一个泛型方法,您需要:

 公共共享功能functionx(中RT)(BYVAL REQ作为RT)作为字符串

我不知道为什么,但你回答一个问题你自己的问题...这是关于的typeof 相当于VB.NET:



在VB.NET TypeOf运算是不完全在C#一样的的typeof 。请参阅MSDN描述:




TypeOf运算运营商决定是否的运行时类型
objectexpression与类型名称兼容。兼容性
依赖于类型名的类型类别。下表显示了
兼容性是如何确定的。




它的语法是 TypeOf运算objectexpression是类型名称,这就是为什么错误说的预期。



您应该使用的GetType

 暗淡serialzer =新的XmlSerializer(的GetType(T))序列化(作家,REQ,NS)


I have this line of C# code that I want to change to VB.NET:

public static string functionx<RT>(RT req){}

Is the following the correct translation?

Public Function functionx(Of RT)(req As RT) As String

解决方案

To declare a generic method, you need:

Public Shared Function functionx(Of RT)(ByVal req As RT) As String

I don't know why, but you answered your own question with another question... Which is about the typeof equivalent in VB.NET:

In VB.NET TypeOf is not exactly the same as typeof in C#. See MSDN description:

The TypeOf operator determines whether the run-time type of objectexpression is compatible with typename. The compatibility depends on the type category of typename. The following table shows how compatibility is determined.

Its syntax is TypeOf objectexpression Is typename, and that's why error says that Is is expected.

You should use GetType:

    Dim serialzer = New XmlSerializer(GetType(T)).Serialize(writer, req, ns)

这篇关于转换功能&LT; T&GT;从C#到VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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