Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<Compile Include="ADummyUserControl.xaml.cs">
<DependentUpon>ADummyUserControl.xaml</DependentUpon>
</Compile>
<Compile Include="ModuleInitializers\MakeInitialized.cs" />
<Compile Include="ModuleInitializers\LibraryModuleInitializer.cs" />
<Compile Include="ModuleInitializers\ModuleInitializerAttribute.cs" />
<Compile Include="WpfWindowStarter.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Copyright (c) 2025 David Rettenbacher
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

using System;
using System.Runtime.CompilerServices;

namespace AnotherClassLibrary.ModuleInitializers
{
public static class LibraryModuleInitializer
{
[ModuleInitializer]
public static void Initialize()
{
// some "complex" code to prove the ModuleInitializersRepackStep can handle it
var shouldInitialize = DateTime.Now > new DateTime(2000, 1, 1);
if (shouldInitialize)
{
MakeInitialized.Initialize();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Copyright (c) 2025 David Rettenbacher
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

namespace AnotherClassLibrary.ModuleInitializers
{
public class MakeInitialized
{
public static bool IsInitialized { get; private set; }

public static int Counter { get;private set; }

public static void Initialize()
{
IsInitialized = true;

Counter = ClassLibrary.ModuleInitializers.MakeInitialized.Counter + 1;

// force an actual assembly dependency to ClassLibrary
ClassLibrary.DummyConverter dummy = new ClassLibrary.DummyConverter();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright (c) 2025 David Rettenbacher
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace System.Runtime.CompilerServices
#pragma warning restore IDE0130 // Namespace does not match folder structure
{
/// <summary>
/// Polyfill for compiler support
/// </summary>
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
internal sealed class ModuleInitializerAttribute : Attribute
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<DependentUpon>DummyUserControl.xaml</DependentUpon>
</Compile>
<Compile Include="GenericBasedResourceKey.cs" />
<Compile Include="ModuleInitializers\LibraryModuleInitializer.cs" />
<Compile Include="ModuleInitializers\MakeInitialized.cs" />
<Compile Include="ModuleInitializers\ModuleInitializerAttribute.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Copyright (c) 2025 David Rettenbacher
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

using System.Runtime.CompilerServices;

namespace ClassLibrary.ModuleInitializers
{
public static class LibraryModuleInitializer
{
[ModuleInitializer]
public static void Initialize()
{
MakeInitialized.Initialize();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Copyright (c) 2025 David Rettenbacher
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

namespace ClassLibrary.ModuleInitializers
{
public class MakeInitialized
{
public static bool IsInitialized { get; private set; }

public static int Counter { get; private set; }

public static void Initialize()
{
Counter++;

IsInitialized = true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright (c) 2025 David Rettenbacher
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace System.Runtime.CompilerServices
#pragma warning restore IDE0130 // Namespace does not match folder structure
{
/// <summary>
/// Polyfill for compiler support
/// </summary>
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
internal sealed class ModuleInitializerAttribute : Attribute
{
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System;
using System.Windows;
using AnotherClassLibrary;

namespace NestedLibraryUsageInXAML
Expand All @@ -13,6 +14,18 @@ public MainWindow()
private async void MainWindowLoaded(object sender, RoutedEventArgs e)
{
await new BclAsyncUsage().GetNumber();

if (!ClassLibrary.ModuleInitializers.MakeInitialized.IsInitialized &&
ClassLibrary.ModuleInitializers.MakeInitialized.Counter == 1)
throw new InvalidOperationException($"ModuleInitializer of '{nameof(ClassLibrary.ModuleInitializers.LibraryModuleInitializer)}' was not executed");

if (!AnotherClassLibrary.ModuleInitializers.MakeInitialized.IsInitialized &&
AnotherClassLibrary.ModuleInitializers.MakeInitialized.Counter == 2)
throw new InvalidOperationException($"ModuleInitializer of '{nameof(AnotherClassLibrary.ModuleInitializers.LibraryModuleInitializer)}' was not executed or not in right order");

if (Program.Counter != 3)
throw new InvalidOperationException($"ModuleInitializers of '{nameof(Program)}' were not executed or not in right order");

Close();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright (c) 2025 David Rettenbacher
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace System.Runtime.CompilerServices
#pragma warning restore IDE0130 // Namespace does not match folder structure
{
/// <summary>
/// Polyfill for compiler support
/// </summary>
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
internal sealed class ModuleInitializerAttribute : Attribute
{
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using System.Windows;

namespace NestedLibraryUsageInXAML
Expand All @@ -19,5 +20,20 @@ public static int Main()
return 1;
}
}

internal static int Counter { get; private set; }

[ModuleInitializer]
internal static void TheInitializer()
{
Counter++;
Counter *= AnotherClassLibrary.ModuleInitializers.MakeInitialized.Counter;
}

[ModuleInitializer]
internal static void TheInitializer2()
{
Counter++;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"NestedLibraryUsageInXAML": {
"commandName": "Project"
},
"Merged": {
"commandName": "Executable",
"executablePath": "$(TargetDir)merged\\NestedLibraryUsageInXAML.exe"
}
}
}
1 change: 1 addition & 0 deletions ILRepack/ILRepack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ private void RepackCore(string tempOutputDirectory)
{
signingStep,
new ReferencesRepackStep(Logger, this, Options),
new ModuleInitializersRepackStep(Logger, this, Options),
new TypesRepackStep(Logger, this, _repackImporter, Options),
new ILLinkFileMergeStep(Logger, this, Options),
new ResourcesRepackStep(Logger, this, Options),
Expand Down
Loading