private TextWriter WriteImplicitImplementationForInterfaceProperties(TextWriter output, EntityContractDeclarationModel entityContractDeclaration, Type intrfceType) { foreach (var pi in intrfceType.GetProperties()) { var prop = new PropertyDeclarationModel(pi); output.WriteLine( $@" /// <summary> /// {GetPropertyGetSetXmlCommentPrefix(prop)} the '{prop.Name}' property value. /// </summary>{GetAttributeDeclarations(prop)} public {prop.TypeFriendlyName} {prop.Name} {{ {GetPropertyGetSetDeclaration(prop)} }}"); } return(output); }
private static string GetPropertyGetSetXmlCommentPrefix(PropertyDeclarationModel propertyDeclaration) { if (propertyDeclaration.CanGet && propertyDeclaration.CanSet) { return("Gets or sets"); } else if (propertyDeclaration.CanGet) { return("Gets"); } else if (propertyDeclaration.CanSet) { return("Sets"); } else { throw new NotSupportedException("Properties without get or set are not supported!"); } }
private static string GetPropertyGetSetDeclaration(PropertyDeclarationModel propertyDeclaration) { if (propertyDeclaration.CanGet && propertyDeclaration.CanSet) { return("get; set;"); } else if (propertyDeclaration.CanGet) { return("get;"); } else if (propertyDeclaration.CanSet) { return("set;"); } else { throw new NotSupportedException("Properties without get or set are not supported!"); } }
private static string GetAttributeDeclarations(PropertyDeclarationModel propertyDeclaration) { if (!propertyDeclaration.Attributes.Any()) { return(null); } var sb = new StringBuilder(); foreach (var attr in propertyDeclaration.Attributes) { string attributeName = attr.AttributeType.Name.EndsWith("Attribute") ? attr.AttributeType.Name.Substring(0, attr.AttributeType.Name.Length - 9) : attr.AttributeType.Name; sb.Append($@" [{attributeName}"); // if the attribute has any constructor or named parameters if ((attr.ConstructorArguments?.Count ?? 0) + (attr.NamedArguments?.Count ?? 0) > 0) { sb.Append("("); bool appendSeparator = false; if (attr.ConstructorArguments != null) { foreach (var constrParam in attr.ConstructorArguments) { if (appendSeparator) { sb.Append(", "); } AppendConstructorParam(sb, constrParam); if (!appendSeparator) { appendSeparator = true; } } } if (attr.NamedArguments != null) { foreach (var namedParam in attr.NamedArguments) { if (appendSeparator) { sb.Append(", "); } AppendConstructorNamedParam(sb, namedParam); if (!appendSeparator) { appendSeparator = true; } } } sb.Append(")"); } sb.Append("]"); } return(sb.ToString()); }