In our product, we have things called "services" which are the basic means of communication between different parts of the product (and especially between languages—an in-house language, C, Python and .NET).
At present, code is like this (Services.Execute
utilising params object[] args
):
myString = (string)Services.Execute("service_name", arg1, arg2, ...);
I'd rather like to be able to write code like this and get the benefits of type checking and less verbose code:
myString = ServiceName(arg1, arg2, ...);
This can be achieved with a simple function,
public static string ServiceName(int arg1, Entity arg2, ...)
{
return (string)Services.Execute("service_name", arg1, arg2, ...);
}
But this is rather verbose, and not quite so easy to manage when doing it for scores of services, as I intend to be doing.
Seeing how extern
and the DllImportAttribute
work, I hope it should be possible to hook this up by some means like this:
[ServiceImport("service_name")]
public static extern string ServiceName(int arg1, Entity arg2, ...);
But I don't know how to achieve this at all and can't seem to find any documentation for it (extern
seems to be a fairly vaguely defined matter). The closest I've found is a somewhat related question, How to provide custom implementation for extern methods in .NET? which didn't really answer my question and is somewhat different, anyway. The C# Language Specification (especially, in version 4.0, section 10.6.7, External methods) doesn't help.
So, I want to provide a custom implementation of external methods; can this be achieved? And if so, how?