matlab.engine.typedinterface.generateCSharp
Generate C# code interface for MATLAB namespaces, classes, and functions
Since R2023b
Description
matlab.engine.typedinterface.generateCSharp(
creates a C# interface from one or more MATLAB® namespaces, classes, and functions. Use the interface to build a C#
application. You must specify at least one targetFolder
,Name=Value
)Namespaces
,
Classes
, or Functions
name-value argument.
Examples
Create a MATLAB function in a file on the MATLAB path named CircleInfo.m
with these statements.
function [diameter,area] = CircleInfo(radius) arguments (Input) radius (1,1) double {mustBeReal} end arguments (Output) diameter (1,1) double {mustBeReal} area (1,1) double {mustBeReal} end area = pi.*radius.*radius; diameter = 2.*radius; end
Generate a C# interface from the CircleInfo
function, and save
the C# file in a folder named CircleApp
.
matlab.engine.typedinterface.generateCSharp("CircleApp",Functions="CircleInfo")
0 class(es) and 1 function(s) written to CircleApp
The function creates CircleInfo.cs
. You can include this file in
a .NET application.
/* File: CircleInfo.cs * * MATLAB Strongly Typed Interface Version: R2023b * C# source code generated on: 06-Jun-2023 */ using System; using MathWorks.MATLAB.Types; using MathWorks.MATLAB.Exceptions; public static partial class MATLABFunctions { public static void CircleInfo(MATLABProvider _matlab, dynamic radius){ dynamic _dynMatlab = _matlab; _dynMatlab.CircleInfo(new RunOptions(nargout:0),radius); } public static void CircleInfo(MATLABProvider _matlab, dynamic radius, out dynamic diameter){ dynamic _dynMatlab = _matlab; diameter = (MATLABArray)_dynMatlab.CircleInfo(new RunOptions(nargout:1),radius); } public static void CircleInfo(MATLABProvider _matlab, dynamic radius, out dynamic diameter, out dynamic area){ dynamic _dynMatlab = _matlab; (diameter,area) = ((MATLABArray,MATLABArray))_dynMatlab.CircleInfo(new RunOptions(nargout:2),radius); } }
Input Arguments
Name of the folder for the generated C# interface, specified as a string scalar or character vector. The name can include a relative or absolute path. A best practice is to place C# code files in separate folders from MATLAB files.
Example: "MyApp"
specifies a subfolder of the current
folder.
Example: "C:\work\MyApp"
specifies a folder with an absolute
path.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: matlab.engine.typedinterface.generateCSharp("MyApp",Classes=["Position","Rectangle"],DisplayReport=true,SaveReport="report.log")
generates a C# interface in the MyApp
folder using the
Position
and Rectangle
classes, displays generation
messages, and saves the log as report.log
.
Note
You must specify at least one Namespaces
,
Classes
, or Functions
name-value argument.
Since R2024a
MATLAB namespaces to include in the C# interface, specified as a string array, character vector, or cell array of character vectors. The namespace value becomes a C# namespace and supersedes any specified namespace. This value becomes the default namespace.
Example: Namespaces="shapes"
MATLAB value and handle classes to include in the C# interface, specified as a
string array, character vector, or cell array of character vectors. The function
generates one file with a .cs
extension for each C# class, struct,
and enumeration.
The function supports user-authored value and handle classes, classes inherited from other user-authored classes, built-in MATLAB classes, and classes inherited from MATLAB classes.
The function represents a MATLAB value class as a C# struct.
The function represents a MATLAB enumeration as a member of the namespace specified by the
OuterCSharpNamespace
name-value argument. The default namespace isMATLABEnums
. Properties and methods of a MATLAB enumeration are not generated in C#.
C# class methods return new objects as output variables. Like MATLAB methods, C# class methods do not update the existing object.
Example: Classes=["Position","Rectangle"]
MATLAB functions to include in the C# interface, specified as a string array,
character vector, or cell array of character vectors. The function supports
user-authored functions and built-in MATLAB functions. Because C# requires that every function belong to a class,
matlab.engine.typedinterface.generateCSharp
generates a C#
class named MATLABFunctions
for these functions. To specify the
class name, use the FunctionHolderClass
name-value argument.
Example: Functions=["show","enlarge"]
C# namespace containing the generated interface, specified as a string scalar or character vector. Specify this argument as the outermost namespace.
Example: OuterCSharpNamespace="shapes"
C# class for MATLAB functions that do not belong to a C# class, specified as a string scalar or character vector.
Example: FunctionHolderClass="shapeInformation"
Option to display generation messages, specified as a numeric or logical
0
(false
) or 1
(true
).
Example: DisplayReport=true
Log filename, specified as a string scalar or character vector. The name can
include a relative or absolute path. Specify this name-value argument to create a text
file, and optionally include a file extension. The file contains detailed information
about the matlab.engine.typedinterface.generateCSharp
function
call and the generated code.
Example: SaveReport="log.txt"
Version History
Introduced in R2023bYou can include handle classes in a strongly typed C# interface. The function creates code with handle copy behavior. That is, functions and methods can modify the input data in the calling workspace without making copies.
The function generates C# comparison methods and the isvalid
function
as well as comparison operators for both handle and value classes which implement the
eq
, ne
, lt
,
gt
, le
, and ge
methods in
MATLAB.
The name of the Packages
name-value argument is now Namespaces
. The
behavior of this name-value argument remains the same. There are no plans to remove support
for existing references to the argument.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)