Skip to content

Commit

Permalink
Made it possible to overload a property, e.g. to specify a new defaul…
Browse files Browse the repository at this point in the history
…t value, the overloaded should be processed first!

[release]
  • Loading branch information
Lakritzator committed May 18, 2016
1 parent 63f6157 commit 5b93453
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 13 deletions.
5 changes: 3 additions & 2 deletions Dapplo.InterfaceImpl.Tests/Dapplo.InterfaceImpl.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<HintPath>..\packages\Dapplo.LogFacade.0.2.44\lib\net45\Dapplo.LogFacade.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Utils, Version=0.1.30.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Utils.0.1.30\lib\net45\Dapplo.Utils.dll</HintPath>
<Reference Include="Dapplo.Utils, Version=0.1.31.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Utils.0.1.31\lib\net45\Dapplo.Utils.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down Expand Up @@ -81,6 +81,7 @@
<Compile Include="HasChangesTests.cs" />
<Compile Include="DefaultValueTest.cs" />
<Compile Include="DescriptionTest.cs" />
<Compile Include="Interfaces\IDefaultValueOverwriteTest.cs" />
<Compile Include="Interfaces\IIndexerTest.cs" />
<Compile Include="Interfaces\ISimpleTypeTest.cs" />
<Compile Include="Interfaces\IBassicAssignTest.cs" />
Expand Down
7 changes: 7 additions & 0 deletions Dapplo.InterfaceImpl.Tests/DefaultValueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public void TestDefaultValue()
Assert.Equal(3, _defaultValueTest.Ages.Count);
}

[Fact]
public void TestDefaultValueOverwrite()
{
var defaultValueOverwriteTest = InterceptorFactory.New<IDefaultValueOverwriteTest>();
Assert.Equal(42, defaultValueOverwriteTest.Age);
}

[Fact]
public void TestDefaultValueAtrribute()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Dapplo - building blocks for desktop applications
// Copyright (C) 2015-2016 Dapplo
//
// For more information see: http://dapplo.net/
// Dapplo repositories are hosted on GitHub: https://github.com/dapplo
//
// This file is part of Dapplo.InterfaceImpl
//
// Dapplo.InterfaceImpl is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Dapplo.InterfaceImpl is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have a copy of the GNU Lesser General Public License
// along with Dapplo.InterfaceImpl. If not, see <http://www.gnu.org/licenses/lgpl.txt>.

#region using

using System.Collections.Generic;
using System.ComponentModel;
using Dapplo.InterfaceImpl.Extensions;

#endregion

namespace Dapplo.InterfaceImpl.Tests.Interfaces
{
/// <summary>
/// This is the interface under test
/// </summary>
public interface IDefaultValueOverwriteTest : IDefaultValueTest, IDefaultValue<IDefaultValueTest>
{
[DefaultValue(42)]
new int Age { get; set; }
}
}
2 changes: 1 addition & 1 deletion Dapplo.InterfaceImpl.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="coveralls.io" version="1.3.4" targetFramework="net46" />
<package id="Dapplo.LogFacade" version="0.2.44" targetFramework="net46" />
<package id="Dapplo.Utils" version="0.1.30" targetFramework="net46" />
<package id="Dapplo.Utils" version="0.1.31" targetFramework="net46" />
<package id="OpenCover" version="4.6.519" targetFramework="net46" />
<package id="ReportGenerator" version="2.4.5.0" targetFramework="net46" />
<package id="xunit" version="2.1.0" targetFramework="net46" />
Expand Down
4 changes: 2 additions & 2 deletions Dapplo.InterfaceImpl/Dapplo.InterfaceImpl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
<HintPath>..\packages\Dapplo.LogFacade.0.2.44\lib\net45\Dapplo.LogFacade.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Utils, Version=0.1.30.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Utils.0.1.30\lib\net45\Dapplo.Utils.dll</HintPath>
<Reference Include="Dapplo.Utils, Version=0.1.31.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Utils.0.1.31\lib\net45\Dapplo.Utils.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
25 changes: 23 additions & 2 deletions Dapplo.InterfaceImpl/IlGeneration/IlTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
using System.Reflection;
using System.Reflection.Emit;
using Dapplo.LogFacade;
using System.Collections.Generic;
using Dapplo.Utils.Extensions;

#endregion

Expand Down Expand Up @@ -81,21 +83,40 @@ from iface in implementingInterfaces
from propertyInfo in iface.GetProperties()
select propertyInfo;

var processedProperties = new Dictionary<string, Type>();
foreach (var propertyInfo in propertyInfos)
{
if (processedProperties.ContainsKey(propertyInfo.Name))
{
if (Log.IsVerboseEnabled())
{
Log.Verbose().WriteLine("Skipping property {0} from {1}, already generated for {2}.", propertyInfo.Name, propertyInfo.DeclaringType.FriendlyName(), processedProperties[propertyInfo.Name].FriendlyName());
}
continue;
}
if (baseProperties.Contains(propertyInfo.Name))
{
Log.Verbose().WriteLine("Skipping property {0}, as the base class implements this.", propertyInfo.Name);
if (Log.IsVerboseEnabled())
{
Log.Verbose().WriteLine("Skipping property {0} from {1}, as the base class implements this.", propertyInfo.Name, propertyInfo.DeclaringType.FriendlyName());
}
continue;
}
if (!propertyInfo.CanRead && !propertyInfo.CanWrite)
{
Log.Verbose().WriteLine("Skipping property {0} as it cannot be read or written.", propertyInfo.Name);
if (Log.IsVerboseEnabled())
{
Log.Verbose().WriteLine("Skipping property {0} from {1}, as it cannot be read or written.", propertyInfo.Name, propertyInfo.DeclaringType.FriendlyName());
}
continue;
}

Log.Verbose().WriteLine("Generating property {0} for {1}", propertyInfo.Name, propertyInfo.DeclaringType.FriendlyName());

// Create get and/or set
IlGetSetBuilder.BuildGetSet(typeBuilder, propertyInfo);

processedProperties.Add(propertyInfo.Name, propertyInfo.DeclaringType);
}

// Make a collection of already implemented method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ orderby sortedExtension.InitOrder ascending
var thisAssembly = GetType().Assembly;

// as GetInterfaces doesn't return the type itself (makes sense), the following 2 lines makes a list of all
var interfacesToCheck = new List<Type>(typeof (T).GetInterfaces())
{
typeof (T)
};
var interfacesToCheck = new[] { typeof(T) }.Concat(typeof(T).GetInterfaces()).ToList();

var propertyTypes = new Dictionary<string, Type>(AbcComparerInstance);
PropertyTypes = new ReadOnlyDictionary<string, Type>(propertyTypes);
Expand Down
2 changes: 1 addition & 1 deletion Dapplo.InterfaceImpl/InterceptorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static IExtensibleInterceptor New(Type interfaceType)
throw new ArgumentException("Only interfaces are allowed.", nameof(interfaceType));
}
// GetInterfaces doesn't return the type itself, so we need to add it.
var implementingInterfaces = interfaceType.GetInterfaces().Concat(new[] {interfaceType}).ToList();
var implementingInterfaces = new[] { interfaceType }.Concat(interfaceType.GetInterfaces()).ToList();

var implementingAndDefaultInterfaces = new List<Type>();
foreach (var implementingInterface in implementingInterfaces.ToList())
Expand Down
2 changes: 1 addition & 1 deletion Dapplo.InterfaceImpl/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Dapplo.LogFacade" version="0.2.44" targetFramework="net452" />
<package id="Dapplo.Utils" version="0.1.30" targetFramework="net452" />
<package id="Dapplo.Utils" version="0.1.31" targetFramework="net452" />
</packages>

0 comments on commit 5b93453

Please sign in to comment.