Accessing Internal Members in the UNO Framework #17083
lindexi
started this conversation in
Show and tell
Replies: 1 comment 3 replies
-
Thanks for the details. It's still important for us to know which internal methods you're trying to get to. We may be able to create uno-specific methods through extension methods or helpers that could be part of the stable API. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This article introduces a hack that allows access to internal, non-public members of the UNO framework. This includes calling non-public API methods and properties, and accessing non-public types within UNO.
The core principle is based on the
InternalsVisibleToAttribute
assembly attribute in the UNO framework, which is made visible to assemblies such asSamplesApp
. Therefore, all you need to do is create a new assembly and set theAssemblyName
toSamplesApp
.Below is a new project I created called
UnoHacker
. You can find all the code for this project at the end of this article.The
UnoHacker
project I created uses thenet8.0
framework. Since the method provided in this article strongly depends on the implementation of the UNO framework, this article was written in June 2024. If you read this article a long time after it was written, it may contain knowledge that is not applicable to the UNO framework you are currently using.This article is aimed at version
5.2.161
of the UNO framework. It does not represent that subsequent UNO versions will also be applicable. It is recommended that you test it yourself according to the method provided in this article.Edit the
csproj
project file of theUnoHacker
project, first configure theAssemblyName
property to specify the assembly name, as shown in the following code:Next, add a reference to
Uno.WinUI
. Adding this reference is just to get the actual UNO reference assembly, not really needing to reference this package. That's why the following code needs to addExcludeAssets="all"
code that does not use any content. The following code also adds theGeneratePathProperty
attribute configuration, which can be used to get the path of the corresponding package in the cache folder through this attribute configuration, which is used to reference the content in the package.Since central package management is used by default, the above code reference does not need to add a version number. After adding the above code, you can ensure that the NuGet package exists locally, and through the
GeneratePathProperty
attribute configuration, you can get the local path of theUno.WinUI
package through the$(PKGUno_WinUI)
attribute.The content of the
$(PKGUno_WinUI)
attribute I got on my device is as follows:Through this, you can splice the path and get the files in the NuGet package. The following code uses the actual UNO under the Skia platform:
What needs to be explained here is that the assembly that UNO allows you to write code references, and the DLL assembly that is finally output after publishing are not the same file. The assembly that allows you to write code is under the Lib folder in the NuGet package, and the actual published output is the DLL is under
uno-runtime
. By using different DLLs, UNO can better support multiple different platforms, and different DLL outputs can be used for different platforms.The code of the
csproj
project file after completing the above code is roughly as follows:Try to write code to test access to UNO's non-public members:
You can see that the code is very convenient to write, and it avoids using reflection, which has higher performance.
Through this method, you can use some non-public members in UNO to achieve some specific requirements. But it must be stated that UNO does not make a stability commitment to non-public APIs. You need to conduct sufficient tests when using them.
The code of this article is placed on github and gitee. You can use the following command line to pull the code:
First create an empty folder, then use the cd command to enter this empty folder in the command line, enter the following code in the command line, and you can get the code of this article:
The above uses the gitee source. If gitee cannot be accessed, please replace it with the github source. Please continue to enter the following code in the command line to switch the gitee source to the github source to pull the code:
After obtaining the code, enter the
UnoDemo/UnoSkiaWeelelqairjiwarfekemGahinabai
folder to get the source code.Beta Was this translation helpful? Give feedback.
All reactions