[GH-ISSUE #39] Use Reflection.Emit for object invocation. #19

Closed
opened 2026-05-05 10:59:33 -06:00 by gitea-mirror · 3 comments
Owner

Originally created by @pauldotknopf on GitHub (Jul 25, 2018).
Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/39

This will give us a huge performance boost.

Using what we've learned in #36, we should not send callbacks to the native world.

Instead, every NetMethodInfo and NetPropertyInfo will have a unit long Id. This id will be used to lookup cached dynamic methods that perform as little as possible.

For example:

public void Invoke(IntPtr t, IntPtr m, IntPtr p, IntPtr r)
{
    using (var method = new NetMethodInfo(m))
    using (var target = new NetReference(t))
    using (var parameters = new NetVariantList(p))
    using (var result = r != IntPtr.Zero ? new NetVariant(r) : null)
    {
        // This will be dynamically compiled!
        ((SomeClass) target.Instance).WithMethodWithStringIntParameters(parameters.Get(0).String, parameters.Get(1).Int);
    }
}

The same thing can be done with property reading/setting.

Originally created by @pauldotknopf on GitHub (Jul 25, 2018). Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/39 This will give us a huge performance boost. Using what we've learned in #36, we should not send callbacks to the native world. Instead, every ```NetMethodInfo``` and ```NetPropertyInfo``` will have a unit ```long Id```. This id will be used to lookup cached dynamic methods that perform as little as possible. For example: ```c# public void Invoke(IntPtr t, IntPtr m, IntPtr p, IntPtr r) { using (var method = new NetMethodInfo(m)) using (var target = new NetReference(t)) using (var parameters = new NetVariantList(p)) using (var result = r != IntPtr.Zero ? new NetVariant(r) : null) { // This will be dynamically compiled! ((SomeClass) target.Instance).WithMethodWithStringIntParameters(parameters.Get(0).String, parameters.Get(1).Int); } } ``` The same thing can be done with property reading/setting.
Author
Owner

@RichiCoder1 commented on GitHub (Aug 10, 2018):

Just in case you're interested:

With Refit, it allows you to define an interface and add attributes to it, and then Refit generates the meat of the code (either using a standalone app or an msbuild task).

Refit does this by scanning the source files using Roslyn, and then generating output files (again using roslyn/template files).
https://github.com/reactiveui/refit/blob/master/InterfaceStubGenerator.Core/InterfaceStubGenerator.cs

<!-- gh-comment-id:411989049 --> @RichiCoder1 commented on GitHub (Aug 10, 2018): Just in case you're interested: With Refit, it allows you to define an interface and add attributes to it, and then Refit generates the meat of the code (either using a standalone app or an msbuild task). Refit does this by scanning the source files using Roslyn, and then generating output files (again using roslyn/template files). https://github.com/reactiveui/refit/blob/master/InterfaceStubGenerator.Core/InterfaceStubGenerator.cs
Author
Owner

@pauldotknopf commented on GitHub (Aug 10, 2018):

Oh, wow! I was wondering how they supported Xamarin where it does AOT compilation. Turns out, they aren't using Reflection.Emit!

I haven't thought about an approach like this. I'm not sure how it would look in Qml.Net.

<!-- gh-comment-id:411989653 --> @pauldotknopf commented on GitHub (Aug 10, 2018): Oh, wow! I was wondering how they supported Xamarin where it does AOT compilation. Turns out, they aren't using Reflection.Emit! I haven't thought about an approach like this. I'm not sure how it would look in Qml.Net.
Author
Owner

@RichiCoder1 commented on GitHub (Aug 10, 2018):

Absolutely. Figured I'd at least mention it just in case it's applicable :). And would work with AOT restrictions on mobile platforms if/when you got there.

<!-- gh-comment-id:411995035 --> @RichiCoder1 commented on GitHub (Aug 10, 2018): Absolutely. Figured I'd at least mention it just in case it's applicable :). And would work with AOT restrictions on mobile platforms if/when you got there.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/qmlnet#19
No description provided.