Skip to content
Open
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
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>ExtendedOperations</RootNamespace>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>..\bin\Release\Extensions\</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\bin\Debug\Extensions\</OutputPath>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\MefCalculator\MefCalculator.csproj">
<Private>false</Private>
</ProjectReference>
</ItemGroup>

<PropertyGroup>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

</Project>
12 changes: 12 additions & 0 deletions mef/simple-calculator/cs/ExtendedOperations/Modulo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel.Composition;
using MefCalculator;

[Export(typeof(IOperation))]
[ExportMetadata("Symbol", '%')]
public class Modulo : IOperation
{
public int Operate(int left, int right)
{
return left % right;
}
}
16 changes: 16 additions & 0 deletions mef/simple-calculator/cs/MefCalculator/MefCalculator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>MefCalculator</RootNamespace>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.ComponentModel.Composition" Version="8.0.0" />
</ItemGroup>

<PropertyGroup>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

</Project>
39 changes: 39 additions & 0 deletions mef/simple-calculator/cs/MefCalculator/MefCalculatorInterfaces.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.ComponentModel.Composition;

namespace MefCalculator
{
public interface ICalculator
{
string Calculate(string input);
}

public interface IOperation
{
int Operate(int left, int right);
}

public interface IOperationData
{
char Symbol { get; }
}

[Export(typeof(IOperation))]
[ExportMetadata("Symbol", '+')]
public class Add : IOperation
{
public int Operate(int left, int right)
{
return left + right;
}
}

[Export(typeof(IOperation))]
[ExportMetadata("Symbol", '-')]
public class Subtract : IOperation
{
public int Operate(int left, int right)
{
return left - right;
}
}
}
111 changes: 111 additions & 0 deletions mef/simple-calculator/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Linq;
using System.Reflection;
using MefCalculator;

namespace SimpleCalculator2
{
class Program
{
private static readonly string MyApplicationPath = Assembly.GetEntryAssembly().Location;
private static readonly string MyApplicationDirectory = Path.GetDirectoryName(MyApplicationPath);

[Export(typeof(ICalculator))]
public class MySimpleCalculator : ICalculator
{
[ImportMany]
public IEnumerable<Lazy<IOperation, IOperationData>> Operations { get; set; }

public string Calculate(string input)
{
int left, right;
char operation;
int fn = FindFirstNonDigit(input);
if (fn < 0)
{
return "Could not parse command.";
}
operation = input[fn];
try
{
left = int.Parse(input.Substring(0, fn));
right = int.Parse(input.Substring(fn + 1));
}
catch (Exception)
{
return "Could not parse command.";
}
foreach (var i in Operations)
{
if (i.Metadata.Symbol == operation)
{
return i.Value.Operate(left, right).ToString();
}
}
return "Operation not found!";
}

private int FindFirstNonDigit(string s)
{
for (int i = 0; i < (s?.Length ?? 0); i++)
{
if (!char.IsDigit(s[i])) return i;
}
return -1;
}
}

private class MySimpleCalculatorComposition
{
private readonly CompositionContainer _container;
public bool HasExtensions { get; }

[Import(typeof(ICalculator))]
public ICalculator Calculator { get; set; }

public MySimpleCalculatorComposition()
{
var catalog = new AggregateCatalog();

catalog.Catalogs.Add(new AssemblyCatalog(typeof(MefCalculator.ICalculator).Assembly));

catalog.Catalogs.Add(new AssemblyCatalog(typeof(MySimpleCalculatorComposition).Assembly));

var extensionsDir = Path.Combine(MyApplicationDirectory, "Extensions");
if (Directory.Exists(extensionsDir))
{
catalog.Catalogs.Add(new DirectoryCatalog(extensionsDir));
HasExtensions = true;
}

_container = new CompositionContainer(catalog);

try
{
_container.ComposeParts(this);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}

static void Main(string[] args)
{
var o = new MySimpleCalculatorComposition();
if (o.HasExtensions) Console.WriteLine("Extensions loaded.");
Console.WriteLine("Enter Command:");
string s;
while ((s = Console.ReadLine()) != null)
{
Console.WriteLine(o.Calculator.Calculate(s));
}
Console.WriteLine("Exited");
}
}
}
46 changes: 46 additions & 0 deletions mef/simple-calculator/cs/SimpleCalculator2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<RootNamespace>SimpleCalculator2</RootNamespace>
<TargetFramework>net8.0</TargetFramework>
<StartupObject>SimpleCalculator2.Program</StartupObject>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>bin\Release\</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>

<ItemGroup>
<Compile Remove="ExtendedOperations\**" />
<Compile Remove="MefCalculator\**" />
<EmbeddedResource Remove="ExtendedOperations\**" />
<EmbeddedResource Remove="MefCalculator\**" />
<None Remove="ExtendedOperations\**" />
<None Remove="MefCalculator\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.ComponentModel.Composition" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="ExtendedOperations\ExtendedOperations.csproj">
<Private>false</Private>
<CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="MefCalculator\MefCalculator.csproj">
<Private>true</Private>
</ProjectReference>
</ItemGroup>

<PropertyGroup>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

</Project>
40 changes: 40 additions & 0 deletions mef/simple-calculator/cs/SimpleCalculator2.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29306.81
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleCalculator2", "SimpleCalculator2.csproj", "{3FF19E38-FDCE-411F-9656-65B69A181A2C}"
ProjectSection(ProjectDependencies) = postProject
{6CE9704B-685C-4792-996B-EF08F704F14E} = {6CE9704B-685C-4792-996B-EF08F704F14E}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExtendedOperations", "ExtendedOperations\ExtendedOperations.csproj", "{6CE9704B-685C-4792-996B-EF08F704F14E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MefCalculator", "MefCalculator\MefCalculator.csproj", "{9DF1F88B-D8F8-4D89-839F-45CB494C1A61}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3FF19E38-FDCE-411F-9656-65B69A181A2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3FF19E38-FDCE-411F-9656-65B69A181A2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3FF19E38-FDCE-411F-9656-65B69A181A2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3FF19E38-FDCE-411F-9656-65B69A181A2C}.Release|Any CPU.Build.0 = Release|Any CPU
{6CE9704B-685C-4792-996B-EF08F704F14E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6CE9704B-685C-4792-996B-EF08F704F14E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6CE9704B-685C-4792-996B-EF08F704F14E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6CE9704B-685C-4792-996B-EF08F704F14E}.Release|Any CPU.Build.0 = Release|Any CPU
{9DF1F88B-D8F8-4D89-839F-45CB494C1A61}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9DF1F88B-D8F8-4D89-839F-45CB494C1A61}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9DF1F88B-D8F8-4D89-839F-45CB494C1A61}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9DF1F88B-D8F8-4D89-839F-45CB494C1A61}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0574C180-4EA2-4ACA-8616-AC2E4C393A99}
EndGlobalSection
EndGlobal