How to Export C# Methods

How to export C# methods?

Contrary to popular belief, this is possible.

See here.

How can I export the names of all the methods in an assembly?

I've got to admit I'm not sure how you could do it in Visual Studio, but programatically you can use reflection:

System.IO.File.WriteAllLines(myFileName,
System.Reflection.Assembly.LoadFile(myDllPath)
.GetType(className)
.GetMethods()
.Select(m => m.Name)
.ToArray());

ETA:

I'm not 100% if you want the methods in the screenshot or all the methods in the DLL so I've updated with the second variant:

 System.IO.File.WriteAllLines(myFileName,
System.Reflection.Assembly.LoadFile(myDllPath)
.GetTypes()
.SelectMany(t => t.GetMethods())
.Select(m => m.Name)
.ToArray());

How to export C# dll method/functions to use it in C++

Don't. Exporting methods from C# is not supported. It is just barely possible by manually editting the emitted assembly, but really - don't.

There's a few proper solutions you could use:

  • Use a C++/CLI project to expose the managed methods to unmanaged code
  • Use COM (.NET is basically COM 2.0, so this is very easy; it's also quite easy to consume on the C++ side)
  • Pass a delegate to the unmanaged code on initialization, and call that when needed
  • Use some other method of inter-process communication (e.g. named pipes, TCP...)

C# Export function dll

Solution: http://www.codeproject.com/Articles/37675/Simple-Method-of-DLL-Export-without-C-CLI

Put the "DllExporter.exe" in your project folder.

German article: http://www.sotzny.de/2011/11/10/dllexport-net-ohne-ccli/

Is is possible to export functions from a C# DLL like in VS C++?

Unmanaged Exports =>
https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports

DLLExport => https://github.com/3F/DllExport

How does it work?


Create a new classlibrary or proceed with an existing one.
Then add the UnmanagedExports Nuget package.

This is pretty much all setup that is required.

Now you can write any kind of static method, decorate it with [DllExport] and use it from native code.

It works just like DllImport, so you can customize the marshalling of parameters/result with MarshalAsAttribute.

During compilation, my task will modify the IL to add the required exports...

Export a list of methods & params in library to a text file

Here is an example:

StringBuilder sb = new StringBuilder();
var type = typeof (MyMath);
sb.AppendFormat("Class Name: {0}", type.Name);
sb.AppendLine();
sb.Append("Methods: ");
sb.AppendLine();
foreach (var method in type.GetMethods())
{
sb.AppendFormat("{0} {1}.{2}( ", method.ReturnType.Name, type.Name, method.Name);
foreach (var param in method.GetParameters())
{
sb.AppendFormat("{0} {1},", param.GetType().Name, param.Name);
}
sb.Remove(sb.ToString().Length - 1, 1);
sb.Append(")");
sb.AppendLine();
}

var content = sb.ToString();
File.WriteAllText("deneme.txt",content);

How to export & import functions and execute them with MEF?

You can import the functions as a Func<> or Action<> delegate, depending on the function signature. For the first function you could import it into IEnumerable<Lazy<Action>>. The second one would be IEnumerable<Lazy<Func<string, string>>>.

You may want to include a contract name to differentiate between different functions with the same signature. A sample export:

[Export("FunctionType")]
public string Function(string value)
{
return value;
}

And a corresponding import:

[ImportMany("FunctionType")]
public IEnumerable<Lazy<Func<string, string>>> ImportedFunctions { get; set; }

How to export dll functions?

Any public classes and thier public members will be visible by adding an assembly reference to your C# Assembly (DLL).

So if in your assembly you have:

public class Helper
{
public static Foo() { } //Visible.
private static Bar() { } //Not Visible.
internal static FooBar() { } //Not Visible.
protected static Wibble() { } //Visible when created a class derived from Helper.
}

private class HiddenHelper //Not Visible
{
public static Foo() { } //Not Visible.
private static Bar() { } //Not Visible.
//etc. Not Visible
}

Anything less than public will be hidden.


Unless this is to be called by something outside .Net. Then you will have to use COM.



Related Topics



Leave a reply



Submit