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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user