gpt4 book ai didi

c# - 在 VB.NET/C# 中调用泛型函数

转载 作者:太空狗 更新时间:2023-10-29 22:34:56 30 4
gpt4 key购买 nike

问题:我想调用一个泛型函数,定义为:

      Public Shared Function DeserializeFromXML(Of T)(Optional ByRef strFileNameAndPath As String = Nothing) As T

现在当我调用它时,我想用下面的任何变体来调用它:

Dim x As New XMLserialization.cConfiguration
x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(Of x)()
x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(GetType(x))()
x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(Of GetType(x))()

但它不起作用。我发现必须输入

非常烦人且不可读
    x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(Of XMLserialization.cConfiguration)()

有没有办法通过从实例中获取类型来调用泛型函数?

最佳答案

我觉得您想为您的 XMLserialization.cConfiguration 类型创建一个较短的别名。尝试使用 Imports 语句来完成此操作:

' at the top of the file
Imports C = XMLserialization.cConfiguration

' somewhere in the body of the file
Dim x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(Of C)()

关于c# - 在 VB.NET/C# 中调用泛型函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2957651/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com