Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{x:Static} does not work for internal members in the same assembly. #124

Open
colejohnson66 opened this issue Sep 10, 2024 · 3 comments
Open
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@colejohnson66
Copy link

See AvaloniaUI/Avalonia#16929. According to @maxkatz6, {x:Static} should work on internal members if those members are in the same assembly. However, XamlX fails because it requires everything to be public:

var member = type?.Fields.FirstOrDefault(f => f.IsPublic && f.IsStatic && f.Name == Member) ??
(IXamlMember?)type?.GetAllProperties().FirstOrDefault(p =>
p.Name == Member && p.Getter is { IsPublic: true, IsStatic: true });

@maxkatz6 maxkatz6 added the help wanted Extra attention is needed label Sep 10, 2024
@maxkatz6
Copy link
Collaborator

It should be a relatively simple fix + one unit test for anybody, who has time to take a look.
Once XamlX changes are merged, we can get it upstream in Avalonia.

@maxkatz6 maxkatz6 added the bug Something isn't working label Sep 10, 2024
@stevemonaco
Copy link

stevemonaco commented Sep 27, 2024

This has been more complicated than expected. I've implemented the feature locally (for static fields so far) and I've come across two problems:

  1. trying to test by adding another TestCase here:

https://github.com/kekekeks/XamlX/blob/master/tests/XamlParserTests/IntrinsicsTests.cs#L89-L108

I've modified IntrinsicsTestsClass to include an internal static field which results in this exception:

System.FieldAccessException : Attempt by method 'TestXaml.88acbed1e31b4312990250a1e2bea46b.Populate(System.IServiceProvider, XamlParserTests.IntrinsicsTestsClass)' to access field 'XamlParserTests.IntrinsicsTestsClass.StaticInternalField' failed.

I suspect the issue is the .NET runtime failing because the type is within the test assembly (eg. CecilTests) and not the generated XAML assembly (TestAsm). Therefore internal access fails. Is there a straightforward way to include a class in the generated assembly?

  1. There's a plumbing problem to ensure we're not fetching internal members from an external assembly. However, the call site at Intrinsics appears to lack knowledge of what assembly the to-be-generated XAML type will be in.

@maxkatz6
Copy link
Collaborator

I suspect the issue is the .NET runtime failing because the type is within the test assembly (eg. CecilTests) and not the generated XAML assembly (TestAsm).

For testing purposes, you can try to inject IgnoresAccessChecksTo attribute in the generated assembly.
We actually do that for Avalonia https://github.com/AvaloniaUI/Avalonia/blob/dccaea61a69280e7a91078ca089b9603f3fe6200/src/Markup/Avalonia.Markup.Xaml.Loader/AvaloniaXamlIlRuntimeCompiler.cs#L125

Ideally, it should only be limited by testing assemblies here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants