mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-05-06 03:32:57 +00:00
Fix attribute multiple arguments
This commit is contained in:
parent
0207b037f3
commit
89b589896e
@ -78,7 +78,7 @@
|
|||||||
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
|
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
|
||||||
|
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" PrivateAssets="all" />
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
|
||||||
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -79,56 +79,40 @@ public class ResourceGenerator : ISourceGenerator
|
|||||||
return fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1);
|
return fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string FormatAttribute(AttributeData attribute)
|
public static string FormatAttribute(AttributeData attribute)
|
||||||
{
|
{
|
||||||
if (attribute.AttributeClass is object)
|
if (!(attribute.AttributeClass is object))
|
||||||
{
|
throw new Exception("AttributeClass not found");
|
||||||
string className = attribute.AttributeClass.ToDisplayString();
|
|
||||||
|
|
||||||
if (!attribute.ConstructorArguments.Any() & !attribute.ConstructorArguments.Any())
|
var className = attribute.AttributeClass.ToDisplayString();
|
||||||
{
|
|
||||||
return className;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
if (!attribute.ConstructorArguments.Any() & !attribute.ConstructorArguments.Any())
|
||||||
|
return className;
|
||||||
|
|
||||||
stringBuilder.Append(className);
|
var strBuilder = new StringBuilder();
|
||||||
stringBuilder.Append('(');
|
|
||||||
|
|
||||||
bool first = true;
|
strBuilder.Append("[");
|
||||||
|
strBuilder.Append(className);
|
||||||
|
strBuilder.Append("(");
|
||||||
|
|
||||||
foreach (var constructorArgument in attribute.ConstructorArguments)
|
strBuilder.Append(String.Join(", ", attribute.ConstructorArguments.Select(ca => FormatConstant(ca))));
|
||||||
{
|
|
||||||
if (!first)
|
|
||||||
{
|
|
||||||
stringBuilder.Append(", ");
|
|
||||||
}
|
|
||||||
|
|
||||||
stringBuilder.Append(constructorArgument.ToCSharpString());
|
strBuilder.Append(String.Join(", ", attribute.NamedArguments.Select(na => $"{na.Key} = {FormatConstant(na.Value)}")));
|
||||||
first = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var namedArgument in attribute.NamedArguments)
|
strBuilder.Append(")]");
|
||||||
{
|
|
||||||
if (!first)
|
|
||||||
{
|
|
||||||
stringBuilder.Append(", ");
|
|
||||||
}
|
|
||||||
|
|
||||||
stringBuilder.Append(namedArgument.Key);
|
return strBuilder.ToString();
|
||||||
stringBuilder.Append(" = ");
|
|
||||||
stringBuilder.Append(namedArgument.Value.ToCSharpString());
|
|
||||||
first = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
stringBuilder.Append(')');
|
|
||||||
|
|
||||||
return stringBuilder.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception("Attribute not found");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string FormatConstant(TypedConstant constant)
|
||||||
|
{
|
||||||
|
if (constant.Kind == TypedConstantKind.Array)
|
||||||
|
return $"new {constant.Type.ToDisplayString()} {{{string.Join(", ", constant.Values.Select(v => FormatConstant(v)))}}}";
|
||||||
|
else
|
||||||
|
return constant.ToCSharpString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void Execute(GeneratorExecutionContext context)
|
public void Execute(GeneratorExecutionContext context)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -231,7 +215,7 @@ namespace {ci.ClassSymbol.ContainingNamespace.ToDisplayString()} {{
|
|||||||
// copy attributes
|
// copy attributes
|
||||||
//Debugger.Launch();
|
//Debugger.Launch();
|
||||||
|
|
||||||
var attrs = string.Join("\r\n\t", f.GetAttributes().Select(x => $"[{x.AttributeClass.Name}({x.ConstructorArguments.Select(x => x.Values.ToString())})]"));// x.ToString()}]"));
|
var attrs = string.Join("\r\n\t", f.GetAttributes().Select(x => FormatAttribute(x)));
|
||||||
|
|
||||||
//Debugger.Launch();
|
//Debugger.Launch();
|
||||||
if (f.Type.Name.StartsWith("ResourceEventHandler") || f.Type.Name.StartsWith("CustomResourceEventHandler"))
|
if (f.Type.Name.StartsWith("ResourceEventHandler") || f.Type.Name.StartsWith("CustomResourceEventHandler"))
|
||||||
@ -251,13 +235,14 @@ namespace {ci.ClassSymbol.ContainingNamespace.ToDisplayString()} {{
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
context.AddSource(ci.Name + ".Error.g.cs", $"/*\r\n {ex}\r\n*/");
|
context.AddSource(ci.Name + ".Error.g.cs", $"/*\r\n{ex}\r\n*/");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
context.AddSource("Error.g.cs", $"/*\r\n {ex}\r\n*/");
|
|
||||||
|
context.AddSource("Error.g.cs", $"/*\r\n{ex}\r\n*/");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user