Only generate setters and getters for public properties (#47)
* Only generate setters and getters for public properties * .
This commit is contained in:
@@ -7,6 +7,11 @@ internal static class MethodSymbolExtensions
|
||||
public static string GetMethodNameWithOptionalTypeParameters(this IMethodSymbol method) =>
|
||||
!method.IsGenericMethod ? method.Name : $"{method.Name}<{string.Join(", ", method.TypeParameters.Select(tp => tp.Name))}>";
|
||||
|
||||
public static bool IsPublic(this IMethodSymbol? methodSymbol)
|
||||
{
|
||||
return methodSymbol is { DeclaredAccessibility: Accessibility.Public };
|
||||
}
|
||||
|
||||
//public static string GetWhereStatement(this IMethodSymbol method) =>
|
||||
// !method.IsGenericMethod ? string.Empty : string.Join("", method.TypeParameters.Select(tp => tp.GetWhereConstraints()));
|
||||
}
|
||||
@@ -7,13 +7,23 @@ internal static class PropertySymbolExtensions
|
||||
{
|
||||
public static TypeEnum GetTypeEnum(this IPropertySymbol p) => p.Type.GetTypeEnum();
|
||||
|
||||
public static (string PropertyType, string? PropertyName, string GetSet) ToPropertyDetails(this IPropertySymbol property, string? overrideType = null)
|
||||
public static (string PropertyType, string? PropertyName, string GetSet)? ToPropertyDetails(this IPropertySymbol property, string? overrideType = null)
|
||||
{
|
||||
var get = property.GetMethod != null ? "get; " : string.Empty;
|
||||
var set = property.SetMethod != null ? "set; " : string.Empty;
|
||||
var getIsPublic = property.GetMethod.IsPublic();
|
||||
var setIsPublic = property.SetMethod.IsPublic();
|
||||
|
||||
if (!getIsPublic && !setIsPublic)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var get = getIsPublic ? "get; " : string.Empty;
|
||||
var set = setIsPublic ? "set; " : string.Empty;
|
||||
|
||||
var type = !string.IsNullOrEmpty(overrideType) ? overrideType : $"{property.Type}";
|
||||
|
||||
return (type!, property.GetSanitizedName(), $"{{ {get}{set}}}");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -96,9 +96,13 @@ using System;
|
||||
{
|
||||
var type = GetPropertyType(property, out var isReplaced);
|
||||
|
||||
(string propertyType, string? propertyName, string getSet) = isReplaced ?
|
||||
property.ToPropertyDetails(type) :
|
||||
property.ToPropertyDetails();
|
||||
var getterSetter = isReplaced ? property.ToPropertyDetails(type) : property.ToPropertyDetails();
|
||||
if (getterSetter is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var propertyName = getterSetter.Value.PropertyName;
|
||||
|
||||
if (property.IsIndexer)
|
||||
{
|
||||
@@ -106,7 +110,7 @@ using System;
|
||||
propertyName = $"this[{string.Join(", ", methodParameters)}]";
|
||||
}
|
||||
|
||||
str.AppendLine($" {propertyType} {propertyName} {getSet}");
|
||||
str.AppendLine($" {getterSetter.Value.PropertyType} {propertyName} {getterSetter.Value.GetSet}");
|
||||
str.AppendLine();
|
||||
}
|
||||
|
||||
|
||||
@@ -155,17 +155,25 @@ using System;
|
||||
overrideOrVirtual = "virtual ";
|
||||
}
|
||||
|
||||
var getIsPublic = property.GetMethod.IsPublic();
|
||||
var setIsPublic = property.SetMethod.IsPublic();
|
||||
|
||||
if (!getIsPublic && !setIsPublic)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string get;
|
||||
string set;
|
||||
if (isReplaced)
|
||||
{
|
||||
get = property.GetMethod != null ? $"get => Mapster.TypeAdapter.Adapt<{type}>({instancePropertyName}); " : string.Empty;
|
||||
set = property.SetMethod != null ? $"set => {instancePropertyName} = Mapster.TypeAdapter.Adapt<{property.Type}>(value); " : string.Empty;
|
||||
get = getIsPublic ? $"get => Mapster.TypeAdapter.Adapt<{type}>({instancePropertyName}); " : string.Empty;
|
||||
set = setIsPublic ? $"set => {instancePropertyName} = Mapster.TypeAdapter.Adapt<{property.Type}>(value); " : string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
get = property.GetMethod != null ? $"get => {instancePropertyName}; " : string.Empty;
|
||||
set = property.SetMethod != null ? $"set => {instancePropertyName} = value; " : string.Empty;
|
||||
get = getIsPublic ? $"get => {instancePropertyName}; " : string.Empty;
|
||||
set = setIsPublic ? $"set => {instancePropertyName} = value; " : string.Empty;
|
||||
}
|
||||
|
||||
str.AppendLine($" public {overrideOrVirtual}{type} {propertyName} {{ {get}{set}}}");
|
||||
|
||||
@@ -57,7 +57,8 @@ internal static class MemberHelper
|
||||
private static IEnumerable<T> GetPublicMembers<T>(
|
||||
ClassSymbol classSymbol,
|
||||
bool proxyBaseClasses,
|
||||
params Func<T, bool>[] filters) where T : ISymbol
|
||||
params Func<T, bool>[] filters
|
||||
) where T : ISymbol
|
||||
{
|
||||
var membersQuery = classSymbol.Symbol.GetMembers().OfType<T>()
|
||||
.Where(m => m.DeclaredAccessibility == Accessibility.Public);
|
||||
|
||||
Reference in New Issue
Block a user