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
8 changes: 8 additions & 0 deletions Cloneable.Sample/ClassWithInheritance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Cloneable.Sample
{
[Cloneable]
public partial class ClassWithInheritance : InheritedClass
{
public double C { get; set; }
}
}
4 changes: 3 additions & 1 deletion Cloneable.Sample/Cloneable.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Cloneable\Cloneable.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\Cloneable\Cloneable.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions Cloneable.Sample/InheritedClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Cloneable.Sample
{
[Cloneable]
public partial class InheritedClass
{
public double A { get; set; }

public double B { get; set; }
}
}
13 changes: 13 additions & 0 deletions Cloneable.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static void Main(string[] args)
DoSimpleExplicitClone();
DoDeepClone();
DoSafeDeepClone();
DoBaseClassClone();
}

static void DoSimpleClone()
Expand Down Expand Up @@ -79,5 +80,17 @@ static void DoSafeDeepClone()
Console.WriteLine("Is parents child copied: " + (clone.Child != parent.Child));
Console.WriteLine();
}

static void DoBaseClassClone()
{
var classWithInheritance = new ClassWithInheritance();

classWithInheritance.A = 2;

var clone = classWithInheritance.Clone();

Console.WriteLine(clone);
Console.WriteLine($"Clone A of Inherited Class:{clone.A}");
}
}
}
6 changes: 4 additions & 2 deletions Cloneable/Cloneable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<Description>Auto-generator of Clone method using C# Source Generator</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<IsRoslynComponent>true</IsRoslynComponent>
<Nullable>enable</Nullable>
<IncludeBuildOutput>false</IncludeBuildOutput>
<RepositoryUrl>https://github.com/mostmand/Cloneable</RepositoryUrl>
Expand All @@ -15,7 +16,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.11.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
<PackageReference Include="System.CodeDom" Version="5.0.0" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions Cloneable/CloneableGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ private IEnumerable<IPropertySymbol> GetCloneableProperties(ITypeSymbol classSym
var targetSymbolMembers = classSymbol.GetMembers().OfType<IPropertySymbol>()
.Where(x => x.SetMethod is not null &&
x.CanBeReferencedByName);

var baseTargetSymbolMembers = classSymbol.BaseType?.GetMembers().OfType<IPropertySymbol>()
.Where(x => x.SetMethod is not null &&
x.CanBeReferencedByName);

if (baseTargetSymbolMembers != null)
{
targetSymbolMembers = targetSymbolMembers.Concat(baseTargetSymbolMembers);
}

if (isExplicit)
{
return targetSymbolMembers.Where(x => x.HasAttribute(cloneAttribute!));
Expand Down