Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a generic function in VB.NET / C#

Question: I want to call a generic function, defined as:

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

Now when I call it, I wanted to do it with any of the variants below:

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))()

But it doesn't work. I find it very annoying and unreadable having to type

    x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(Of XMLserialization.cConfiguration)()

Is there a way to call a generic function by getting the type from the instance ?

like image 204
Stefan Steiger Avatar asked Jun 02 '10 12:06

Stefan Steiger


People also ask

What is generic in VB net?

A generic type is a single programming element that adapts to perform the same functionality for a variety of data types. When you define a generic class or procedure, you do not have to define a separate version for each data type for which you might want to perform that functionality.

What is the syntax for generic function?

The syntax for a generic method includes a list of type parameters, inside angle brackets, which appears before the method's return type. For static generic methods, the type parameter section must appear before the method's return type.

What is generic function in C?

Generic functions are functions declared with one or more generic type parameters. They may be methods in a class or struct , or standalone functions. A single generic declaration implicitly declares a family of functions that differ only in the substitution of a different actual type for the generic type parameter.


4 Answers

It sounds to me like you want to create a shorter alias for your XMLserialization.cConfiguration type. Try using the Imports statement to accomplish this:

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

' somewhere in the body of the file
Dim x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(Of C)()
like image 78
Dan Tao Avatar answered Oct 20 '22 02:10

Dan Tao


Generics and reflection make very poor friends. However, you can do this via MakeGenericMethod. However, that is going to be hard and ugly.

Since XmlSerializer is based around Type instance - I would reverse things: have the realy code Type based, and call into that from the shallow generic version; example in C#:

public T DeserializeFromXML<T>(string path) {
   return (T)DeserializeFromXML(typeof(T), path);
}
public object DeserializeFromXML(Type type, string path) {
    //TODO: real code
}
like image 20
Marc Gravell Avatar answered Oct 20 '22 04:10

Marc Gravell


Only using the methods of the System.Reflection namespace, which really isn't worth it for the effort you're trying to save.

like image 43
David M Avatar answered Oct 20 '22 04:10

David M


The type of a generic method is determined at compile time, which is why you can't set it using a variable. This is a key difference between generic programming and type reflection.

like image 1
John M Gant Avatar answered Oct 20 '22 03:10

John M Gant