From 106385a4663f7cd25fa6ce6b80001e8d28b8a226 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Mon, 9 Jan 2023 21:06:30 +0100 Subject: [PATCH] Add support for implicit and explicit operators (#51) * . * xxxxxxxx * rev * .... * . * ok * 2 * . * , --- .../PartialInterfacesGenerator.cs | 2 +- .../ProxyClassesGenerator.Mapster.cs | 1 + .../FileGenerators/ProxyClassesGenerator.cs | 42 +++++++++++++ .../Utils/MemberHelper.cs | 21 ++++++- ...Akka.Actor.LocalActorRefProviderProxy.g.cs | 2 + ....SharePoint.Client.ClientContextProxy.g.cs | 2 + ...t.SharePoint.Client.ClientObjectProxy.g.cs | 2 + ...oint.Client.ClientRuntimeContextProxy.g.cs | 2 + ...harePoint.Client.SecurableObjectProxy.g.cs | 2 + .../Microsoft.SharePoint.Client.WebProxy.g.cs | 2 + .../Destination/NoNamespaceProxy.g.cs | 2 + ...neratorTests.Source.Generic_T__1Proxy.g.cs | 2 + ...ourceGeneratorTests.Source.HumanProxy.g.cs | 2 + ...ceGeneratorTests.Source.IOperatorTest.g.cs | 30 +++++++++ ...atorTests.Source.MixedVisibilityProxy.g.cs | 2 + ...neratorTests.Source.OperatorTestProxy.g.cs | 61 +++++++++++++++++++ ...eratorTests.Source.PersonExtendsProxy.g.cs | 2 + ...urceGeneratorTests.Source.PersonProxy.g.cs | 2 + .../ProxyInterfaceSourceGeneratorTest.cs | 57 +++++++++++++++++ .../Source/IOperatorTest.cs | 6 ++ .../Source/OperatorTest.cs | 46 ++++++++++++++ 21 files changed, 288 insertions(+), 2 deletions(-) create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IOperatorTest.g.cs create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.OperatorTestProxy.g.cs create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Source/IOperatorTest.cs create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Source/OperatorTest.cs diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs index 5027b12..e183c8b 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs @@ -154,7 +154,7 @@ using System; { str.AppendLine($" {attribute}"); } - + str.AppendLine($" event {type} {@event.Key.GetSanitizedName()};"); str.AppendLine(); } diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.Mapster.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.Mapster.cs index 03aa2fd..684a39a 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.Mapster.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.Mapster.cs @@ -19,6 +19,7 @@ internal partial class ProxyClassesGenerator str.AppendLine($" Mapster.TypeAdapterConfig<{replacedType.Key}, {replacedType.Value}>.NewConfig().ConstructUsing({instance} => new {classNameProxy}({instance}));"); str.AppendLine($" Mapster.TypeAdapterConfig<{replacedType.Value}, {replacedType.Key}>.NewConfig().MapWith({proxy} => (({classNameProxy}) {proxy})._Instance);"); + str.AppendLine(); } diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs index 3649883..7c2c5e7 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs @@ -78,6 +78,7 @@ internal partial class ProxyClassesGenerator : BaseGenerator, IFilesGenerator var properties = GeneratePublicProperties(targetClassSymbol, pd.ProxyBaseClasses); var methods = GeneratePublicMethods(targetClassSymbol, pd.ProxyBaseClasses); var events = GenerateEvents(targetClassSymbol, pd.ProxyBaseClasses); + var operators = GenerateOperators(targetClassSymbol, pd.ProxyBaseClasses); var configurationForMapster = string.Empty; if (Context.ReplacedTypes.Any()) @@ -111,6 +112,8 @@ using System; {events} +{operators} + public {constructorName}({targetClassSymbol} instance){@base} {{ _Instance = instance; @@ -328,4 +331,43 @@ using System; return str.ToString(); } + + private string GenerateOperators(ClassSymbol targetClassSymbol, bool proxyBaseClasses) + { + var str = new StringBuilder(); + foreach (var @operator in MemberHelper.GetPublicStaticOperators(targetClassSymbol, proxyBaseClasses)) + { + foreach (var attribute in @operator.GetAttributesAsList()) + { + str.AppendLine($" {attribute}"); + } + + var parameter = @operator.Parameters.First(); + var proxyClassName = targetClassSymbol.Symbol.ResolveProxyClassName(); + + var operatorType = @operator.Name.ToLowerInvariant().Replace("op_", string.Empty); + if (operatorType == "explicit") + { + var returnTypeAsString = GetReplacedType(@operator.ReturnType, out _); + + str.AppendLine($" public static explicit operator {returnTypeAsString}({proxyClassName} {parameter.Name})"); + str.AppendLine(@" {"); + str.AppendLine($" return ({returnTypeAsString}) {parameter.Name}._Instance;"); + str.AppendLine(@" }"); + } + else + { + var returnTypeAsString = GetReplacedType(parameter.Type, out _); + + str.AppendLine($" public static implicit operator {proxyClassName}({returnTypeAsString} {parameter.Name})"); + str.AppendLine(@" {"); + str.AppendLine($" return new {proxyClassName}(({targetClassSymbol.Symbol.Name}) {parameter.Name});"); + str.AppendLine(@" }"); + } + + str.AppendLine(); + } + + return str.ToString(); + } } \ No newline at end of file diff --git a/src/ProxyInterfaceSourceGenerator/Utils/MemberHelper.cs b/src/ProxyInterfaceSourceGenerator/Utils/MemberHelper.cs index 2484bb0..2c2bb98 100644 --- a/src/ProxyInterfaceSourceGenerator/Utils/MemberHelper.cs +++ b/src/ProxyInterfaceSourceGenerator/Utils/MemberHelper.cs @@ -38,6 +38,24 @@ internal static class MemberHelper .ToArray(); } + public static IReadOnlyList GetPublicStaticOperators( + ClassSymbol classSymbol, + bool proxyBaseClasses, + Func? filter = null) + { + filter ??= _ => true; + + return + GetPublicMembers( + classSymbol, + proxyBaseClasses, + m => m.Kind == SymbolKind.Method, + m => m.MethodKind == MethodKind.Conversion, + m => !ExcludedMethods.Contains(m.Name), + filter) + .ToArray(); + } + public static IReadOnlyList> GetPublicEvents( ClassSymbol classSymbol, bool proxyBaseClasses, @@ -47,7 +65,8 @@ internal static class MemberHelper #pragma warning disable CS8619 // Nullability of reference types in value doesn't match target type. #pragma warning disable RS1024 // Compare symbols correctly - return GetPublicMembers(classSymbol, + return GetPublicMembers( + classSymbol, proxyBaseClasses, m => m.MethodKind is MethodKind.EventAdd or MethodKind.EventRemove/* || m.MethodKind == MethodKind.EventRaise*/, filter) diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/AkkaGenerated/Akka.Actor.LocalActorRefProviderProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/AkkaGenerated/Akka.Actor.LocalActorRefProviderProxy.g.cs index 3c806b4..04b414c 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/AkkaGenerated/Akka.Actor.LocalActorRefProviderProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/AkkaGenerated/Akka.Actor.LocalActorRefProviderProxy.g.cs @@ -132,6 +132,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source.AkkaActor + + public LocalActorRefProviderProxy(Akka.Actor.LocalActorRefProvider instance) { _Instance = instance; diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientContextProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientContextProxy.g.cs index 0b3c4f2..9d9618b 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientContextProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientContextProxy.g.cs @@ -48,6 +48,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source.PnP + + public ClientContextProxy(Microsoft.SharePoint.Client.ClientContext instance) : base(instance) { _Instance = instance; diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientObjectProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientObjectProxy.g.cs index 422d60d..48d4c4a 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientObjectProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientObjectProxy.g.cs @@ -85,6 +85,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source.PnP + + public ClientObjectProxy(Microsoft.SharePoint.Client.ClientObject instance) { _Instance = instance; diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientRuntimeContextProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientRuntimeContextProxy.g.cs index d36a0c0..534ec26 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientRuntimeContextProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientRuntimeContextProxy.g.cs @@ -143,6 +143,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source.PnP + + public ClientRuntimeContextProxy(Microsoft.SharePoint.Client.ClientRuntimeContext instance) { _Instance = instance; diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.SecurableObjectProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.SecurableObjectProxy.g.cs index 68cc9a7..1524a3b 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.SecurableObjectProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.SecurableObjectProxy.g.cs @@ -46,6 +46,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source.PnP + + public SecurableObjectProxy(Microsoft.SharePoint.Client.SecurableObject instance) : base(instance) { _Instance = instance; diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.WebProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.WebProxy.g.cs index ca752ba..eeb1699 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.WebProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.WebProxy.g.cs @@ -1131,6 +1131,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source.PnP + + public WebProxy(Microsoft.SharePoint.Client.Web instance) : base(instance) { _Instance = instance; diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/NoNamespaceProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/NoNamespaceProxy.g.cs index fcbf9a9..13bc302 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/NoNamespaceProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/NoNamespaceProxy.g.cs @@ -24,6 +24,8 @@ using System; + + public NoNamespaceProxy(ProxyInterfaceSourceGeneratorTests.Source.NoNamespace instance) { _Instance = instance; diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.Generic_T__1Proxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.Generic_T__1Proxy.g.cs index 2c1df34..98a0059 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.Generic_T__1Proxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.Generic_T__1Proxy.g.cs @@ -30,6 +30,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source + + public GenericProxy(ProxyInterfaceSourceGeneratorTests.Source.Generic instance) { _Instance = instance; diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.HumanProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.HumanProxy.g.cs index ed7182e..3c9bb69 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.HumanProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.HumanProxy.g.cs @@ -27,6 +27,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source + + public HumanProxy(ProxyInterfaceSourceGeneratorTests.Source.Human instance) { _Instance = instance; diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IOperatorTest.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IOperatorTest.g.cs new file mode 100644 index 0000000..30bf9a9 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IOperatorTest.g.cs @@ -0,0 +1,30 @@ +//---------------------------------------------------------------------------------------- +// +// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//---------------------------------------------------------------------------------------- + +#nullable enable +using System; + +namespace ProxyInterfaceSourceGeneratorTests.Source +{ + public partial interface IOperatorTest + { + ProxyInterfaceSourceGeneratorTests.Source.OperatorTest _Instance { get; } + + string Name { get; set; } + + int? Id { get; set; } + + + + + + + } +} +#nullable disable \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.MixedVisibilityProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.MixedVisibilityProxy.g.cs index 35e50b1..169a5bf 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.MixedVisibilityProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.MixedVisibilityProxy.g.cs @@ -25,6 +25,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source + + public MixedVisibilityProxy(ProxyInterfaceSourceGeneratorTests.Source.MixedVisibility instance) { _Instance = instance; diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.OperatorTestProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.OperatorTestProxy.g.cs new file mode 100644 index 0000000..3106294 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.OperatorTestProxy.g.cs @@ -0,0 +1,61 @@ +//---------------------------------------------------------------------------------------- +// +// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//---------------------------------------------------------------------------------------- + +#nullable enable +using System; + +namespace ProxyInterfaceSourceGeneratorTests.Source +{ + public partial class OperatorTestProxy : IOperatorTest + { + public ProxyInterfaceSourceGeneratorTests.Source.OperatorTest _Instance { get; } + + + public string Name { get => _Instance.Name; set => _Instance.Name = value; } + + public int? Id { get => _Instance.Id; set => _Instance.Id = value; } + + + + + + + + public static implicit operator OperatorTestProxy(string name) + { + return new OperatorTestProxy((OperatorTest) name); + } + + public static implicit operator OperatorTestProxy(int? id) + { + return new OperatorTestProxy((OperatorTest) id); + } + + public static explicit operator string(OperatorTestProxy test) + { + return (string) test._Instance; + } + + public static explicit operator int?(OperatorTestProxy test) + { + return (int?) test._Instance; + } + + + + public OperatorTestProxy(ProxyInterfaceSourceGeneratorTests.Source.OperatorTest instance) + { + _Instance = instance; + + + + } + } +} +#nullable disable \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs index 6be1893..052bbd6 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs @@ -114,6 +114,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source + + public PersonExtendsProxy(ProxyInterfaceSourceGeneratorTests.Source.PersonExtends instance) { _Instance = instance; diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs index 595df12..3f8357e 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs @@ -168,6 +168,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source + + public PersonProxy(ProxyInterfaceSourceGeneratorTests.Source.Person instance) : base(instance) { _Instance = instance; diff --git a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs index 3fef764..54e9bc1 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs @@ -88,6 +88,63 @@ namespace ProxyInterfaceSourceGeneratorTests } } + [Fact] + public void GenerateFiles_ForClassWithOperator_Should_GenerateCorrectFiles() + { + // Arrange + var fileNames = new[] + { + "ProxyInterfaceSourceGeneratorTests.Source.IOperatorTest.g.cs", + "ProxyInterfaceSourceGeneratorTests.Source.OperatorTestProxy.g.cs" + }; + + var path = "./Source/IOperatorTest.cs"; + var sourceFile = new SourceFile + { + Path = path, + Text = File.ReadAllText(path), + AttributeToAddToInterface = new ExtraAttribute + { + Name = "ProxyInterfaceGenerator.Proxy", + ArgumentList = "typeof(ProxyInterfaceSourceGeneratorTests.Source.OperatorTest)" + } + }; + + // Act + var result = _sut.Execute(new[] + { + sourceFile + }); + + // Assert + result.Valid.Should().BeTrue(); + result.Files.Should().HaveCount(fileNames.Length + 1); + + foreach (var fileName in fileNames.Select((fileName, index) => new { fileName, index })) + { + var builder = result.Files[fileName.index + 1]; // +1 means skip the attribute + builder.Path.Should().EndWith(fileName.fileName); + + if (Write) File.WriteAllText($"../../../Destination/{fileName.fileName}", builder.Text); + builder.Text.Should().Be(File.ReadAllText($"../../../Destination/{fileName.fileName}")); + } + + var name = "stef"; + var operatorTest = new OperatorTest + { + Name = name + }; + string name1 = (string) operatorTest; + name1.Should().Be(name); + + var p = new OperatorTestProxy(operatorTest); + string name2 = (string)p; + name2.Should().Be(name); + + var p2 = (OperatorTestProxy)name; + p2.Should().BeEquivalentTo(new OperatorTestProxy(operatorTest)); + } + [Fact] public void GenerateFiles_When_NoNamespace_Should_GenerateCorrectFiles() { diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/IOperatorTest.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/IOperatorTest.cs new file mode 100644 index 0000000..e7c9802 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/IOperatorTest.cs @@ -0,0 +1,6 @@ +// file-scoped namespace ! +namespace ProxyInterfaceSourceGeneratorTests.Source; + +public partial interface IOperatorTest +{ +} \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/OperatorTest.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/OperatorTest.cs new file mode 100644 index 0000000..0cbd338 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/OperatorTest.cs @@ -0,0 +1,46 @@ +namespace ProxyInterfaceSourceGeneratorTests.Source +{ + public class OperatorTest + { + public string Name { get; set; } = null!; + + public int? Id { get; set; } + + // Operator : implicit + public static implicit operator OperatorTest(string name) + { + return new() + { + Name = name + }; + } + + public static implicit operator OperatorTest(int? id) + { + return new() + { + Id = id + }; + } + + // Operator : explicit + public static explicit operator string(OperatorTest test) + { + return test.Name; + } + + public static explicit operator int?(OperatorTest test) + { + return test.Id; + } + } + + public class X + { + public X() + { + OperatorTest operatorTest = "stef"; + var s = (string)operatorTest; + } + } +} \ No newline at end of file