Main Content

Call .NET Generic Methods

A generic method in .NET declares one or more parameterized types (type parameters). This allows the method to operate on various data types while maintaining type safety.

Use the NET.invokeGenericMethod function to call generic methods from MATLAB®. The usage depends on whether the generic method is:

  • A static method

  • A member of a generic class

  • A member of a nongeneric class

Note

The methods and methodsview functions do not list generic methods. Use the Display .NET Generic Methods Using Reflection example.

These examples call methods defined in the sample NetDocGeneric.cs file. To see the C# code, see C# NetDocGeneric.cs File.

Invoke a Generic Instance Method

The GenMethod method is a generic instance method that returns the input argument. Call the method with the number 5, specifying the input argument as a System.Int32 type.

mlClass = NetDocGeneric.SampleClass();
ret = NET.invokeGenericMethod(mlClass,"GenMethod",{"System.Int32"},5)
ret =

  int32

   5

Invoke a Method with Mixed Arguments

GenMethodWithMixedArgs takes two parameterized arguments and a flag tf to determine which to return. If tf is true, the method returns the first argument. If tf is false, the method returns the second argument. Call GenMethodWithMixedArgs with arguments of type System.Double.

Return the first argument:

ret = NET.invokeGenericMethod(mlClass,"GenMethodWithMixedArgs",{"System.Double"},5,6,true)
ret =

     5

Return the second argument:

ret = NET.invokeGenericMethod(mlClass,"GenMethodWithMixedArgs",{"System.Double"},5,6,false)
ret =

     6

Invoke a Static Generic Method

To call a static method, specify the fully qualified class name. For example, call GenStaticMethod with class name NetDocGeneric.SampleClass.

ret = NET.invokeGenericMethod("NetDocGeneric.SampleClass","GenStaticMethod",{"System.Int32"},5)
ret =

  int32

   5

Invoke a Static Generic Method of a Generic Class

First, create a generic class definition using NET.GenericClass:

genClsDef = NET.GenericClass("NetDocGeneric.SampleGenericClass","System.Double");
ret = NET.invokeGenericMethod(genClsDef,"GenStaticMethod",{"System.Int32"},5)
ret =

  int32

   5

Invoke a Generic Method of a Generic Class

  • If the method's type parameter matches the class's, call the method directly on the class object.

  • If the method's type parameter is different, use NET.invokeGenericMethod.

Create .NET Arrays of Generic Type

To create arrays of a generic type, call NET.createArray with a NET.GenericClass argument.

For example, create an array of five System.Int32 elements.

genType = NET.GenericClass("System.Collections.Generic.List","System.Int32"); 
arr = NET.createArray(genType,5)
arr = 

  List<System*Int32>[] with properties:

            Length: 5 
        LongLength: 5 
              Rank: 1 
          SyncRoot: [1x1 System.Collections.Generic.List<System*Int32>[]] 
        IsReadOnly: 0 
       IsFixedSize: 1 
    IsSynchronized: 0 

C# NetDocGeneric.cs File

This code provides the of the NetDocGeneric.cs file. The example defines simple generic methods to illustrate the NET.invokeGenericMethod syntax. To build and load the NetDocGeneric assembly, see Build and Load .NET Assembly for MATLAB.

using System;
using System.Collections.Generic;
using System.Text;

namespace NetDocGeneric
{
    public class SampleClass
    {
        public K GenMethod<K>(K arg) 
        { 
            // set type of arg to K
            return (arg); 
        }        
        public K GenMethodWithMixedArgs<K>(K arg1, K arg2, bool tf)
        { 
            // if true, return arg1
            // if false, return arg2
            return (tf ? arg1 : arg2); 
        }
        public static K GenStaticMethod<K>(K arg) 
        { 
            return (arg); 
        }
        public static K GenStaticMethodWithMixedArgs<K>
            (K arg1, K arg2, bool tf) 
        { 
            return (tf ? arg1 : arg2); 
        }
    }

    public class SampleGenericClass<T>
    {
        public string ParameterizedGenMethod<K>(T clsParam, K arg)
        { 
            return (clsParam.GetType().Name + ", " + 
                arg.GetType().Name); 
        }
        public T GenMethod<T>(T arg) 
        { 
            return (arg); 
        }
        public static K GenStaticMethod<K>(K arg) 
        { 
            return (arg); 
        }
        public static K GenStaticMethodWithMixedArgs<K>
            (K arg1, K arg2, bool tf) 
        { 
            return (tf ? arg1 : arg2); 
        }
        public static string ParameterizedStaticGenMethod<K>
            (T clsParam, K arg) 
        { 
            return (clsParam.GetType().Name + ", " + 
                arg.GetType().Name); 
        }
    }
}

See Also

Functions

Classes

Topics