Skip to content

Commit

Permalink
Added support for inheritance (some properties defined on base class …
Browse files Browse the repository at this point in the history
…level) (#7)
  • Loading branch information
Antao Almada authored and mikhailshilkov committed Mar 8, 2017
1 parent 34bab13 commit 98dd3bf
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 10 deletions.
8 changes: 8 additions & 0 deletions AssemblyToProcess/AssemblyToProcess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Inheritance.cs" />
<Compile Include="MultipleConstructors2.cs" />
<Compile Include="MultipleConstructorsOnlyOneIsPublic.cs" />
<Compile Include="NoMatchingParameter.cs" />
Expand All @@ -52,6 +53,13 @@
<Compile Include="PropertiesOfSameType.cs" />
<Compile Include="PrimitiveValues.cs" />
<Compile Include="InAssemblyUsage.cs" />
<Compile Include="InheritanceFromAnotherAssembly.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ReferencedAssembly\ReferencedAssembly.csproj">
<Project>{1339aa3a-8712-4f25-b000-9e589d5b1284}</Project>
<Name>ReferencedAssembly</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
30 changes: 30 additions & 0 deletions AssemblyToProcess/Inheritance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace AssemblyToProcess
{
public class BaseInheritance
{
public BaseInheritance(int value1, string value2)
{
this.Value1 = value1;
this.Value2 = value2;
}

public int Value1 { get; }

public string Value2 { get; }

public BaseInheritance With<T>(T value) => this;
}

public class Inheritance : BaseInheritance
{
public Inheritance(int value1, string value2, long value3)
: base(value1, value2)
{
this.Value3 = value3;
}

public long Value3 { get; }

public new Inheritance With<T>(T value) => this;
}
}
17 changes: 17 additions & 0 deletions AssemblyToProcess/InheritanceFromAnotherAssembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace AssemblyToProcess
{
using ReferencedAssembly;

public class InheritanceFromAnotherAssembly : ReferencedBaseInheritance
{
public InheritanceFromAnotherAssembly(int value1, string value2, long value3)
: base(value1, value2)
{
this.Value3 = value3;
}

public long Value3 { get; }

public InheritanceFromAnotherAssembly With<T>(T value) => this;
}
}
36 changes: 36 additions & 0 deletions ReferencedAssembly/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ReferencedAssembly")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ReferencedAssembly")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("1339aa3a-8712-4f25-b000-9e589d5b1284")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
55 changes: 55 additions & 0 deletions ReferencedAssembly/ReferencedAssembly.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1339AA3A-8712-4F25-B000-9E589D5B1284}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ReferencedAssembly</RootNamespace>
<AssemblyName>ReferencedAssembly</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ReferencedBaseInheritance.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
15 changes: 15 additions & 0 deletions ReferencedAssembly/ReferencedBaseInheritance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace ReferencedAssembly
{
public class ReferencedBaseInheritance
{
public ReferencedBaseInheritance(int value1, string value2)
{
this.Value1 = value1;
this.Value2 = value2;
}

public int Value1 { get; }

public string Value2 { get; }
}
}
22 changes: 22 additions & 0 deletions Tests/WeaverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ public void MultipleConstructors_WithIsInjected(string typeName)
Assert.AreEqual(31231, result3.Value3);
}

[Test]
public void Inheritance_WithIsInjected()
{
var type = assembly.GetType("AssemblyToProcess.Inheritance");
var instance = (dynamic)Activator.CreateInstance(type, new object[] { 1, "Hello", 234234L });

var result1 = instance.With(123);
Assert.AreEqual(123, result1.Value1);
Assert.AreEqual(instance.Value2, result1.Value2);
Assert.AreEqual(instance.Value3, result1.Value3);

var result2 = instance.With("World");
Assert.AreEqual(instance.Value1, result2.Value1);
Assert.AreEqual("World", result2.Value2);
Assert.AreEqual(instance.Value3, result1.Value3);

var result3 = instance.With(31231L);
Assert.AreEqual(instance.Value1, result3.Value1);
Assert.AreEqual(instance.Value2, result3.Value2);
Assert.AreEqual(31231L, result3.Value3);
}

[Test]
public void MultipleConstructorsOnlyOneIsPublic_WithIsInjected()
{
Expand Down
10 changes: 10 additions & 0 deletions With.Fody.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nuget", "NuGet\Nuget.csproj
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssemblyToProcess", "AssemblyToProcess\AssemblyToProcess.csproj", "{E97E649F-B703-47E3-B18A-0871D3498742}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReferencedAssembly", "ReferencedAssembly\ReferencedAssembly.csproj", "{1339AA3A-8712-4F25-B000-9E589D5B1284}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -44,6 +46,14 @@ Global
{E97E649F-B703-47E3-B18A-0871D3498742}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E97E649F-B703-47E3-B18A-0871D3498742}.Release|Any CPU.Build.0 = Release|Any CPU
{E97E649F-B703-47E3-B18A-0871D3498742}.Release|x86.ActiveCfg = Release|Any CPU
{1339AA3A-8712-4F25-B000-9E589D5B1284}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1339AA3A-8712-4F25-B000-9E589D5B1284}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1339AA3A-8712-4F25-B000-9E589D5B1284}.Debug|x86.ActiveCfg = Debug|Any CPU
{1339AA3A-8712-4F25-B000-9E589D5B1284}.Debug|x86.Build.0 = Debug|Any CPU
{1339AA3A-8712-4F25-B000-9E589D5B1284}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1339AA3A-8712-4F25-B000-9E589D5B1284}.Release|Any CPU.Build.0 = Release|Any CPU
{1339AA3A-8712-4F25-B000-9E589D5B1284}.Release|x86.ActiveCfg = Release|Any CPU
{1339AA3A-8712-4F25-B000-9E589D5B1284}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
44 changes: 34 additions & 10 deletions With.Fody/ModuleWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ public void Execute()
foreach (var type in ModuleDefinition.Types
.Where(type => type.GetMethods().Any(m => m.IsPublic && m.Name.StartsWith("With"))))
{
var ctor = GetValidConstructor(type);
if (ctor != null)
try
{
AddWith(type, ctor);
RemoveGenericWith(type);
LogInfo($"Added method 'With' to type '{type.Name}'.");
var ctor = GetValidConstructor(type);
if (ctor != null)
{
AddWith(type, ctor);
RemoveGenericWith(type);
LogInfo($"Added method 'With' to type '{type.Name}'.");
}
}
catch (AssemblyResolutionException ex) {
LogInfo($"Type '{type.Name}' references another assembly '{ex.AssemblyReference.FullName}'.");
}
}
}
Expand All @@ -44,7 +50,7 @@ private static bool IsPair(PropertyDefinition pro, ParameterDefinition par)
private static MethodDefinition GetValidConstructor(TypeDefinition type)
{
return type.GetConstructors()
.Where(ctor => ctor.Parameters.Count >= 2 && ctor.Parameters.All(par => type.Properties.Any(pro => IsPair(pro, par))))
.Where(ctor => ctor.Parameters.Count >= 2 && ctor.Parameters.All(par => GetAllProperties(type).Any(pro => IsPair(pro, par))))
.Aggregate((MethodDefinition)null, (max, next) => next.Parameters.Count > (max?.Parameters.Count ?? -1) ? next : max);

}
Expand All @@ -62,9 +68,9 @@ private void AddWith(TypeDefinition type, MethodDefinition ctor)
foreach (var property in ctor.Parameters)
{
var parameterName = property.Name;
var getter = type.Methods.First(m => m.IsGetter && string.Compare(m.Name, $"get_{property.Name}", StringComparison.InvariantCultureIgnoreCase) == 0);
var getter = GetPropertyGetter(type, parameterName);

string propertyName = ToPropertyName(property.Name);
var propertyName = ToPropertyName(property.Name);
MethodDefinition method;
var explicitName = $"With{propertyName}";
if (type.Methods.Any(m => m.Name == explicitName)
Expand Down Expand Up @@ -94,7 +100,7 @@ private void AddWith(TypeDefinition type, MethodDefinition ctor)
}
else
{
var getterParameter = type.Methods.First(m => m.IsGetter && string.Compare(m.Name, $"get_{parameter.Name}", StringComparison.InvariantCultureIgnoreCase) == 0);
var getterParameter = GetPropertyGetter(type, parameter.Name);
processor.Emit(OpCodes.Ldarg_0);
processor.Emit(OpCodes.Call, getterParameter);
}
Expand Down Expand Up @@ -138,8 +144,26 @@ private void ReplaceCalls(TypeDefinition withType, MethodDefinition newMethod, T
}


private string ToPropertyName(string fieldName)
private static string ToPropertyName(string fieldName)
{
return Char.ToUpperInvariant(fieldName[0]) + fieldName.Substring(1);
}

private static IEnumerable<PropertyDefinition> GetAllProperties(TypeDefinition type)
{
// get recursively through the hierachy all the properties with a public getter
return type.Properties.Where(pro => pro.GetMethod.IsPublic)
.Concat(type.BaseType == null ?
Enumerable.Empty<PropertyDefinition>() :
GetAllProperties(type.BaseType.Resolve()));
}

private static MethodDefinition GetPropertyGetter(TypeDefinition type, string name)
{
// get the getter for the property anywhere in the hierachy with the given name
return GetAllProperties(type)
.First(pro => String.Compare(pro.Name, name, StringComparison.InvariantCultureIgnoreCase) == 0)
.GetMethod;
}

}

0 comments on commit 98dd3bf

Please sign in to comment.