Add support for implicit and explicit operators (#51)
* . * xxxxxxxx * rev * .... * . * ok * 2 * . * ,
This commit is contained in:
@@ -154,7 +154,7 @@ using System;
|
||||
{
|
||||
str.AppendLine($" {attribute}");
|
||||
}
|
||||
|
||||
|
||||
str.AppendLine($" event {type} {@event.Key.GetSanitizedName()};");
|
||||
str.AppendLine();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,24 @@ internal static class MemberHelper
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public static IReadOnlyList<IMethodSymbol> GetPublicStaticOperators(
|
||||
ClassSymbol classSymbol,
|
||||
bool proxyBaseClasses,
|
||||
Func<IMethodSymbol, bool>? 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<IGrouping<ISymbol, IMethodSymbol>> 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)
|
||||
|
||||
Reference in New Issue
Block a user