[Mono-list] how to "register" a c++ class to c#?
Christopher David Howie
me at chrishowie.com
Mon Jun 7 11:17:22 EDT 2010
On 06/07/2010 10:19 AM, 冶晶邓 wrote:
> thanks,Jedd!
> i think i have understood the code you supplied.
> i wonder if we can create a C# class with some C-API instead of directly
> defining it in C#? just like python who enables us to create new type by
> filling a PyType struct and pass it to PyType_Ready call.
> is that exists?
You technically do have this capability, but this would not be very
helpful to people who want to code against this class. Since C# is at
heart a static language, it needs to know how types are defined in
advance for other developers to make use of them.
However, you can define a public *interface* in an assembly, and then
emit a completely new class that implements this interface at runtime.
This is a very common technique when writing bindings for remoting
protocols in .NET; the bindings let the user define an interface that
also defines the RPC interface they want to use, then the bindings
implement that interface on a transparent proxying class
behind-the-scenes. The user only needs to know about the interface type
in advance, not the implementation.
So, for example, if your class has "public: void foo(); void bar();"
then you could define in an assembly:
public ISomeClass
{
void Foo();
void Bar();
}
Then you can emit a new class at runtime that will effectively be:
class SomeClass : ISomeClass
{
private delegate void FooCallback();
private delegate void BarCallback();
private FooCallback fooCallback;
private BarCallback barCallback;
public SomeClass(FooCallback fooCallback, BarCallback barCallback)
{
this.fooCallback = fooCallback;
this.barCallback = barCallback;
}
public void Foo()
{
fooCallback();
}
public void Bar()
{
barCallback();
}
}
This approach has the strong advantage that you are free to reimplement
this interface a different way in the future if your internal binding
needs to change.
Note that I'm not terribly familiar with libmono, so I won't be able to
provide the calls required to emit this class and to obtain and call the
constructor. (Were it 100% C# I would...) Perhaps someone else can
chime in with that info.
--
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers
If you correspond with me on a regular basis, please read this document:
http://www.chrishowie.com/email-preferences/
PGP fingerprint: 2B7A B280 8B12 21CC 260A DF65 6FCE 505A CF83 38F5
------------------------------------------------------------------------
IMPORTANT INFORMATION/DISCLAIMER
This document should be read only by those persons to whom it is
addressed. If you have received this message it was obviously addressed
to you and therefore you can read it.
Additionally, by sending an email to ANY of my addresses you are
agreeing that I am, by definition, "the intended recipient," and that I
may do whatever I wish with the contents of any message you send me,
unless a pre-existing agreement prohibits me from so doing.
This overrides any disclaimer or statement of confidentiality that may
be included on your message.
More information about the Mono-list
mailing list