Using C# dll functions in matlab code - c#

I have a C# project, and I want to use the function of my project in matlab.
I've added
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
befor every classes in my project and make the out put type class library.
but when I use of dll in matlab,
temp = NET.addAssembly('../../foo')
and then foo.Classes, there is no class!
what should i do?! plz help me :)

Sample regarding above comment
To use a class from a .NET assembly using NET.addAssembly(...), there is no need to make the class COM Visible but the class, as well as the methods you want to access to, have to be public.
.NET code
namespace foo
{
public class SampleClass
{
// Constructor
public SampleClass() { }
// Static example
public static string StaticMethod() { return "Hello from static method."; }
// Instance example
public string InstanceMethod() { return "Hello from instance method."; }
}
}
Usage from Matlab
% Loading the .NET assembly
NET.addAssembly('..\..\Foo.dll');
% Call of a static method
foo.SampleClass.StaticMethod()
% Call of an instance method
instance = foo.SampleClass();
instance.InstanceMethod();

Related

Python C# interop

In an assembly Test.Dll I have a C# class:
public class MyClass : IDisposable
{
int _TheInt;
public MyClass (int i) {_TheInt = i;}
public int GetInt() { return _TheInt;}
}
and a function:
public int MyFunc(MyClass myObject) { return myObject.GetInt(); }
How, in an ironPython script, create an object of type MyClass and call MyFunc with this object?
I can do the first step but as I am not an expert in neither C# and Python I failed for the second step.
Any help would be highly appreciated.
First, you need to import dll:
import clr
clr.AddReferenceToFileAndPath(r"/path/to/Test.dll")
then after reference was added, you can import your C# namespace or class, and create the object:
import MyNameSpace.MyClass # namespace name class from C# DLL
obj = MyClass(1)
# Now you have an object, let's call the methods:
myIntValue = obj.GetInt()
Then, if you want to call MyFunc, instead of direct call of GetInt(), you need to make it static (since it accepts object as argument
//Put that inside MyClass declaration, cause global functions are not allowed by C#
public static int MyFunc(MyClass myObject) { return myObject.GetInt(); }
And call it on python's side:
result = MyClass.MyFunc(obj)

Function 'class' in Unity/C#

I'm new in Unity. My question isa, is it possible to create function files, without constructor and other stuff? In flash actionscript 3 it's look like this:
package util
{
public function getRandomNumber(minQ:Number = 0, maxQ:Number = Number.MAX_VALUE):Number
{
return minQ + Math.random() * (maxQ - minQ);
}
}
Is it possible to do somthing similar like this?
No, it is impossible in C#. I suggest you learn about Extension Methods and Partial classes.
You can use static classes and singletons as well, but try to avoid the temptation to access it from every part of your project - it will be difficult to modify and refactor it in the future.
You cannot create a global function, but you can create a static method in a static class:
namespace MyNamespace
{
public static class Util
{
public static double GetRandomNumber(..) { ... }
}
}
and use it like
var myNumber = Util.GetRandomNumber(...);
The important part here is that the method is static, which means that you don't need an instance of the class to call it. The static class means that it is impossible to create an instance of that class.

Python for .NET - Create an instance of a singelton

I'm trying to use python to create an instance of a singleton class. The class looks like this:
public sealed class Test
{
private Test()
{
}
public static Test GetInstance()
{
.......
}
}
I've tried everything from here:
Python for .NET: How to explicitly create instances of C# classes using different versions of the same DLL?
How can I create an instance of this class in python?
I have found a solution.
#set reference
ref = clr.AddReference(r'Assemblypath')
#get type
testType= ref.GetType('Namespace.Test')
#GetUninitializedObject
testUninitialize=System.Runtime.Serialization.FormatterServices.GetUninitializedObject(testType);
#Now it's possible to call the method
test = testUninitialize.GetInstance()

How to force sub class to implement a static method

I understand this is only possible with a workaround. But why?
I want to add plugin support to my app. So I designed an abstract class that all future plugins will need to implement. Every plugin must implement a GetVersion() method like in this example code:
public abstract class Plugin
{
public abstract int GetVersion();
}
public class MyPlugin : Plugin
{
public override int GetVersion()
{
return 1;
}
}
This of course works perfectly as long as I instantiate the plugin before calling the GetVersion() method.
But if I want to get the version number of the plugin before creating an instance of it? To check compatibility for example?
public class Program
{
public Program()
{
if (MyPlugin.GetVersion() > 1)
{
PluginLoader.Load(new MyPlugin());
}
}
}
Although it might not answer directly your question "WHY" I think below solution might be usefull in your scenario:
Use assembly version attribute:
Assembly thisAssem = typeof(MyPlugin).Assembly;
AssemblyName thisAssemName = thisAssem.GetName();
Version ver = thisAssemName.Version;
It never can be done by C# because a static method cannot be implemented in derived classes.
Like the workaround, you can create a static factory to create the instance.
public abstract class Plugin
{
public abstract int GetVersion();
}
public class FactoryPlugin<T> where T : Plugin, new()
{
public static int GetVersion()
{
return new T().GetVersion();
}
}
public class Program
{
public Program()
{
if (FactoryPlugin<MyPlugin>.GetVersion() > 1)
{
}
}
}
Consider using the Factory pattern in a way similar to what a COM class factory does. You create two classes, your useful class, and a class factory class. Your class factory class implements IPluginFactory. You package it with your Plugin. The plugin factory has vary simple methods, but one of them allows your Plugin to be created. It's close to what #ThierryV showed, but without static methods. So the process is:
Use whatever you are planning to use to store and instantiate your plugins, but instead of instantiating a plugin, you instantiate the appropriate Plugin Factory
You can have the Plugin factory do what ever you want -- get detailed information about the plugin, allow instantiation of the latest version or a particular version of the plugin - go to town
But, eventually, you use an instance of the factory to instantiate your Plugin.
This is a good place to start: What exactly is a Class Factory?, but Don Box's Essential COM book is where I learned all this stuff, a long time ago in a place far away.

Object from class library not contains methods

Trying to build and use class library in C#.
Creating class library:
File->New Project->Windows->Classic Desktop->Class Library
Code:
namespace ClassLibrary2
{
public class Class1
{
public static long Add(long i, long j)
{
return (i + j);
}
}
}
Trying to consume it from console application:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ClassLibrary2.Class1 c = new Class1();
c. //no Add function
}
}
}
But c object not contains Add function. Why? How how to fix it?
Add is a static method. You can't call static methods "via" instances in C#. That has nothing to do with it being in a different library.
You can call the method as:
long result = ClassLibrary2.Class1.Add(10, 20);
or if you actually have a using directive for ClassLibrary2 (it's unclear from the question):
long result = Class1.Add(10L, 20L);
Alternatively, change the method to be an instance method, if that's what you wanted - at which point you'd be able to call c.Add(10L, 20L).
You declared Class1 as static, then, you don't need an instance to use it.
ClassLibrary2.Add(1, 1);
Add in static method. You must call it like static method:
Class1.Add(1,2);
If You intent to make it Instance specific remove the static
namespace ClassLibrary2
{
public class Class1
{
public long Add(long i, long j)
{
return (i + j);
}
}
}

Categories