.NET assembly created by Matlab NE Builder cannot be used from Matlab ("The type initializer for ... threw an exception")?

3 ビュー (過去 30 日間)
Yuri Shirman
Yuri Shirman 2013 年 8 月 19 日
回答済み: Dhruvesh Patel 2017 年 2 月 2 日
Initial intention (summary):
1. Call a .NET DLL from Matlab and analyze the results.
2. Add some Matlab algorithm to this DLL (i.e. call .NET assembly created from Matlab script by Builder NE)
3. Repeat step 1.
Resulting failure:
The type initializer for Namespace.<Class> threw an exception
Steps for simplified version of the test:
1. Create with deploytool a project Summator.prj (.NET assembly, class SumClass) wrapping a simple script, say:
function img = imagesum(img1, img2)
img = img1 + img2;
2. Build the project: the assemblies Summator.dll and SummatorNative.dll can be found now in C:\Users\MyName\Documents\MATLAB\Test.NET\Summator\distrib .
3. Try to load the assembly from Matlab:
myroot = 'C:\Users\MyName\Documents\MATLAB\Test.NET'
mypath=fullfile(myroot,'\Summator\distrib');
addpath(mypath);
asm = NET.addAssembly(fullfile(mypath,'Summator.dll'));
import Summator.*;
try
mysummator = Summator.SumClass();
catch ex
disp(ex);
disp(ex.ExceptionObject);
disp(ex.ExceptionObject.TypeName);
disp(ex.ExceptionObject.Message);
disp(ex.ExceptionObject.InnerException);
disp(ex.ExceptionObject.TargetSite);
disp(ex.ExceptionObject.StackTrace);
disp(ex.ExceptionObject.Source);
end
The result:
myroot =
C:\Users\MyName\Documents\MATLAB\Test.NET
NET.NetException
Package: NET
Properties:
ExceptionObject: [1x1 System.TypeInitializationException]
identifier: 'MATLAB:NET:CLRException:CreateObject'
message: 'Message: The type initializer for 'Summator.SumClass' threw an exception.
Source: mscorlib
HelpLink: '
cause: {}
stack: [1x1 struct]
Methods, Superclasses
System.TypeInitializationException handle
Package: System
Properties:
TypeName: [1x1 System.String]
Message: [1x1 System.String]
Data: [1x1 System.Collections.ListDictionaryInternal]
InnerException: [1x1 System.TypeInitializationException]
TargetSite: [1x1 System.Reflection.RuntimeMethodInfo]
StackTrace: [1x1 System.String]
HelpLink: []
Source: [1x1 System.String]
HResult: -2146233036
Methods, Events, Superclasses
Summator.SumClass
The type initializer for 'Summator.SumClass' threw an exception.
System.TypeInitializationException handle
Package: System
Properties:
TypeName: [1x1 System.String]
Message: [1x1 System.String]
Data: [1x1 System.Collections.ListDictionaryInternal]
InnerException: [1x1 System.Exception]
TargetSite: [1x1 System.Reflection.RuntimeConstructorInfo]
StackTrace: [1x1 System.String]
HelpLink: []
Source: [1x1 System.String]
HResult: -2146233036
Methods, Events, Superclasses
System.Reflection.RuntimeMethodInfo handle
Package: System.Reflection
Properties:
Name: [1x1 System.String]
DeclaringType: [1x1 System.RuntimeType]
ReflectedType: [1x1 System.RuntimeType]
MemberType: [1x1 System.Reflection.MemberTypes]
MetadataToken: 100667905
Module: [1x1 System.Reflection.RuntimeModule]
IsSecurityCritical: 1
IsSecuritySafeCritical: 1
IsSecurityTransparent: 0
MethodHandle: [1x1 System.RuntimeMethodHandle]
Attributes: [1x1 System.Reflection.MethodAttributes]
CallingConvention: [1x1 System.Reflection.CallingConventions]
ReturnType: [1x1 System.RuntimeType]
ReturnTypeCustomAttributes: [1x1 System.Reflection.RuntimeParameterInfo]
ReturnParameter: [1x1 System.Reflection.RuntimeParameterInfo]
IsGenericMethod: 0
IsGenericMethodDefinition: 0
ContainsGenericParameters: 0
MethodImplementationFlags: [1x1 System.Reflection.MethodImplAttributes]
IsPublic: 0
IsPrivate: 0
IsFamily: 0
IsAssembly: 1
IsFamilyAndAssembly: 0
IsFamilyOrAssembly: 0
IsStatic: 1
IsFinal: 0
IsVirtual: 0
IsHideBySig: 1
IsAbstract: 0
IsSpecialName: 0
IsConstructor: 0
CustomAttributes: [1x1 System.Collections.ObjectModel.ReadOnlyCollection<System*Reflection*CustomAttributeData>]
Methods, Events, Superclasses
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
at dotnetcli.NETConstructor.invoke(NETConstructor* , COSClient* client, Int32* nlhs, COSValue* plhs, Int32 nrhs, COSValue* prhs)
mscorlib
The most important point: If I use the DLL from .NET application everything works, e.g.:
NETApp.ex -> NETAlgo.dll -> Summator.dll = Success
Matlab.exe -> Summator.dll = _Failure _(as shown above)
Matlab.exe -> NETAlgo.dll -> Summator.dll = Failure (same way)
Details (environment) :
A. MATLAB Version: 8.0.0.783 (R2012b) 32-bit
Operating System: Microsoft Windows 7 Version 6.1 (Build 7601: Service Pack 1) Java Version: Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot™ Client VM mixed mode
B. .NET framework: v4.0.30319
  3 件のコメント
Yevgen Revtsov
Yevgen Revtsov 2016 年 3 月 23 日
I am facing the same problem. Any updates?
James Mithen
James Mithen 2016 年 3 月 30 日
I am also facing the same problem.

サインインしてコメントする。

回答 (1 件)

Dhruvesh Patel
Dhruvesh Patel 2017 年 2 月 2 日
This is a documented limitation. Please refer the following answer.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by