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
21 changes: 21 additions & 0 deletions TestStatements/AppWithPlugin/AppWithPlugin.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project>
<ImportGroup Label="SolutionProps">
<Import Project="..\Solution_net.props" />
</ImportGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.props" />
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PluginBase\PluginBase.csproj" />
</ItemGroup>

</Project>
149 changes: 149 additions & 0 deletions TestStatements/AppWithPlugin/Model/AppWithPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PluginBase.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AppWithPlugin.Model;

public class AppWithPlugin : IEnvironment, IUserInterface
{
static Assembly LoadPlugin(string relativePath)
{
// Navigate up to the solution root
string root = Path.GetFullPath(Path.Combine(
Path.GetDirectoryName(
Path.GetDirectoryName(
Path.GetDirectoryName(
Path.GetDirectoryName(typeof(Program).Assembly.Location))))));

string pluginLocation = Path.GetFullPath(Path.Combine(root, relativePath.Replace('\\', Path.DirectorySeparatorChar),"Debug", "net6.0"));
Console.WriteLine($"Loading commands from: {pluginLocation}");
PluginLoadContext loadContext = new PluginLoadContext(pluginLocation);
return loadContext.LoadFromAssemblyPath(Path.Combine(pluginLocation,relativePath)+".dll");
}

static IEnumerable<ICommand> CreateCommands(Assembly assembly,IEnvironment env)
{
int count = 0;

foreach (Type type in assembly.GetTypes())
{
if (typeof(ICommand).IsAssignableFrom(type))
{
ICommand result = Activator.CreateInstance(type) as ICommand;
if (result != null)
{
result.Initialize(env);
count++;
yield return result;
}
}
}

if (count == 0)
{
string availableTypes = string.Join(",", assembly.GetTypes().Select(t => t.FullName));
throw new ApplicationException(
$"Can't find any type which implements ICommand in {assembly} from {assembly.Location}.\n" +
$"Available types: {availableTypes}");
}
}

IEnumerable<ICommand>? commands;
private IMessenger? _messanger;
private IServiceProvider? _sp;

public IData data { get => throw new NotImplementedException(); }
public IUserInterface ui { get => this; }
public IMessenger messaging { get => _messanger ?? new WeakReferenceMessenger(); }
public string Title { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

public void Initialize(string[] args)
{
_sp = new ServiceCollection()
.AddTransient<IRandom, Model.Random>()
.AddTransient<ISysTime, Model.SysTime>()
.AddSingleton<ILogger, Logging>()
.BuildServiceProvider();


string[] pluginPaths =
[
"HelloPlugin"
];

commands = pluginPaths.SelectMany(pluginPath =>
{
Assembly pluginAssembly = LoadPlugin(pluginPath);
return CreateCommands(pluginAssembly,this);
}).ToList();
Console.WriteLine("AppWithPlugin is initialized.");
}

public void Main(string[] args)
{
try
{
if (args.Length == 1 && args[0] == "/d")
{
Console.WriteLine("Waiting for any key...");
Console.ReadLine();
}


if (args.Length == 0)
{
Console.WriteLine("Commands: ");
foreach (ICommand command in commands)
{
Console.WriteLine($"{command.Name}\t - {command.Description}");
}
}
else
{
foreach (string commandName in args)
{
Console.WriteLine($"-- {commandName} --");

ICommand command = commands.FirstOrDefault(c => c.Name.ToUpper() == commandName.ToUpper());
if (command == null)
{
Console.WriteLine("No such command is known.");
return;
}

command.Execute();

Console.WriteLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}

public T? GetService<T>()
{
return _sp.GetService<T>();
}

public bool AddService<T>(T service)
{
throw new NotImplementedException();
}

public bool ShowMessage(string message)
{
Console.WriteLine(message);
return true;
}
}
24 changes: 24 additions & 0 deletions TestStatements/AppWithPlugin/Model/Logging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;

namespace AppWithPlugin.Model
{
public class Logging : ILogger
{
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
throw new NotImplementedException();
}

public bool IsEnabled(LogLevel logLevel)
{
throw new NotImplementedException();
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
Debug.WriteLine($"[{logLevel}] {eventId.Id} {formatter(state, exception)}");
}
}
}
41 changes: 41 additions & 0 deletions TestStatements/AppWithPlugin/Model/PluginLoadContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Reflection;
using System.Runtime.Loader;

namespace AppWithPlugin.Model;

class PluginLoadContext : AssemblyLoadContext
{
private AssemblyDependencyResolver _resolver;

public PluginLoadContext(string pluginPath)
{
_resolver = new AssemblyDependencyResolver(pluginPath);
}

protected override Assembly? Load(AssemblyName assemblyName)
{
string? assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath != null)
{
return LoadFromAssemblyPath(assemblyPath);
}

return null;
}

protected override nint LoadUnmanagedDll(string unmanagedDllName)
{
string? libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
if (libraryPath != null)
{
return LoadUnmanagedDllFromPath(libraryPath);
}

#if NET7_0_OR_GREATER
return nint.Zero;
#else
return IntPtr.Zero;
#endif
}
}
47 changes: 47 additions & 0 deletions TestStatements/AppWithPlugin/Model/Random.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using PluginBase.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppWithPlugin.Model;

public class Random : IRandom
{
private System.Random _random;

public Random()
{
_random = new System.Random();
}
public int Next()
{
return _random.Next();
}

public int Next(int maxValue)
{
return _random.Next(maxValue);
}

public int Next(int minValue, int maxValue)
{
return _random.Next(minValue, maxValue);
}

public void NextBytes(byte[] buffer)
{
_random.NextBytes(buffer);
}

public double NextDouble()
{
return _random.NextDouble();
}

public void Seed(int seed)
{
_random = new System.Random(seed);
}
}
14 changes: 14 additions & 0 deletions TestStatements/AppWithPlugin/Model/SysTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using PluginBase.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppWithPlugin.Model
{
public class SysTime : ISysTime
{
public DateTime Now => DateTime.Now;
}
}
18 changes: 18 additions & 0 deletions TestStatements/AppWithPlugin/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using PluginBase;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace AppWithPlugin;

public class Program
{
public static void Main(string[] args)
{
var app = new Model.AppWithPlugin();
app.Initialize(args);
app.Main(args);
}
}
23 changes: 23 additions & 0 deletions TestStatements/AppWithPlugin/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"profiles": {
"AppWithPlugin": {
"commandName": "Project"
},
"WSL": {
"commandName": "WSL2",
"distributionName": ""
},
"AppWithPlugin /d": {
"commandName": "Project",
"commandLineArgs": "/d"
},
"AppWithPlugin Hello": {
"commandName": "Project",
"commandLineArgs": "Hello"
},
"Profil \"1\"": {
"commandName": "Project",
"commandLineArgs": "Test"
}
}
}
8 changes: 1 addition & 7 deletions TestStatements/AsyncExampleWPF/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows;

namespace AsyncExampleWPF
{
Expand Down
2 changes: 1 addition & 1 deletion TestStatements/AsyncExampleWPF/AsyncExampleWPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</ImportGroup>
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net462-windows;net472-windows;net48-windows;net481-windows</TargetFrameworks>
<TargetFrameworks>net462-windows;net472-windows;net481-windows</TargetFrameworks>
<UseWPF>true</UseWPF>
</PropertyGroup>

Expand Down
1 change: 0 additions & 1 deletion TestStatements/AsyncExampleWPF/AsyncExampleWPF_net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<OutputType>WinExe</OutputType>
<TargetFrameworks>net6.0-windows;net7.0-windows;net8.0-windows</TargetFrameworks>
<UseWPF>true</UseWPF>
<LangVersion>12.0</LangVersion>
</PropertyGroup>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.props" />
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />
Expand Down
3 changes: 0 additions & 3 deletions TestStatements/AsyncExampleWPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
using System.Net;
using System.IO;
using System.Linq;
using System.Threading;
using System.Diagnostics;
using System.Security.Policy;
using System.Collections;

namespace AsyncExampleWPF
{
Expand Down
1 change: 0 additions & 1 deletion TestStatements/CallAllExamples/CallAllExamples_net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>12.0</LangVersion>
</PropertyGroup>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.props" />
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />
Expand Down
Loading
Loading