Skip to content

Commit

Permalink
Add the GeneratedCode Attribute to the Resource Designer classes.
Browse files Browse the repository at this point in the history
Context dotnet#8381

We generate a Resource Designer assembly and an Intermediate
source file at build time. Both of these contain classes which
should have the `GeneratedCode` Attribute. So lets add it.
The version will be the same as the build assembly used to generate it.
  • Loading branch information
dellis1972 committed Sep 30, 2024
1 parent 230ebb0 commit 82f1fed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ bool Run (DirectoryAssemblyResolver res)
TypeReference e = ImportType ("System.ComponentModel.EditorBrowsableState", module, netstandardDef.MainModule);
var editorBrowserAttr = new CustomAttribute (editorBrowserConstructor);
editorBrowserAttr.ConstructorArguments.Add (new CustomAttributeArgument (e, System.ComponentModel.EditorBrowsableState.Never));

MethodReference generatedCodeConstructor = ImportCustomAttributeConstructor (cache, "System.CodeDom.Compiler.GeneratedCodeAttribute", module, netstandardDef.MainModule, argCount: 2);
var generatedCodeAttr = new CustomAttribute (generatedCodeConstructor);
generatedCodeAttr.ConstructorArguments.Add (new CustomAttributeArgument (module.TypeSystem.String, nameof(GenerateResourceDesignerAssembly)));
var version = typeof(GenerateResourceDesignerAssembly).Assembly.GetName().Version;
generatedCodeAttr.ConstructorArguments.Add (new CustomAttributeArgument (module.TypeSystem.String, version.ToString ()));

var att = TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.Public | TypeAttributes.BeforeFieldInit;

Expand All @@ -139,6 +145,7 @@ bool Run (DirectoryAssemblyResolver res)
);
CreateCtor (cache, resourceDesigner, module);
resourceDesigner.CustomAttributes.Add (editorBrowserAttr);
resourceDesigner.CustomAttributes.Add (generatedCodeAttr);
module.Types.Add (resourceDesigner);
TypeDefinition constDesigner = null;
if (IsApplication) {
Expand All @@ -152,6 +159,7 @@ bool Run (DirectoryAssemblyResolver res)
);
CreateCtor (cache, constDesigner, module);
constDesigner.CustomAttributes.Add (editorBrowserAttr);
constDesigner.CustomAttributes.Add (generatedCodeAttr);
module.Types.Add (constDesigner);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ public class GenerateResourceDesignerIntermediateClass : AndroidTask
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.CodeDom.Compiler;
namespace %NAMESPACE% {
#pragma warning disable IDE0002
/// <summary>
/// Android Resource Designer class.
/// Exposes the Android Resource designer assembly into the project Namespace.
/// </summary>
[GeneratedCode(""%TOOL%"", ""%VERSION%"")]
public partial class Resource : %BASECLASS% {
}
#pragma warning restore IDE0002
Expand All @@ -40,6 +42,7 @@ public partial class Resource : %BASECLASS% {
//------------------------------------------------------------------------------
namespace %NAMESPACE%
[<GeneratedCode(""%TOOL%"", ""%VERSION%"")>]
type Resource = %BASECLASS%
";

Expand All @@ -54,11 +57,19 @@ public override bool RunTask ()
//bool isVB = string.Equals (extension, ".vb", StringComparison.OrdinalIgnoreCase);
bool isFSharp = string.Equals (language, "F#", StringComparison.OrdinalIgnoreCase);
bool isCSharp = string.Equals (language, "C#", StringComparison.OrdinalIgnoreCase);
var version = typeof(GenerateResourceDesignerIntermediateClass).Assembly.GetName().Version;
string template = "";
if (isCSharp)
template = CSharpTemplate.Replace ("%NAMESPACE%", Namespace).Replace ("%BASECLASS%", ns);
else if (isFSharp)
template = FSharpTemplate.Replace ("%NAMESPACE%", Namespace).Replace ("%BASECLASS%", ns);
if (isCSharp) {
template = CSharpTemplate.Replace ("%NAMESPACE%", Namespace)
.Replace ("%BASECLASS%", ns)
.Replace ("%VERSION%", version.ToString ())
.Replace ("%TOOL%", nameof (GenerateResourceDesignerIntermediateClass));
} else if (isFSharp) {
template = FSharpTemplate.Replace ("%NAMESPACE%", Namespace)
.Replace ("%BASECLASS%", ns)
.Replace ("%VERSION%", version.ToString ())
.Replace ("%TOOL%", nameof (GenerateResourceDesignerIntermediateClass));
}

Files.CopyIfStringChanged (template, OutputFile.ItemSpec);
return !Log.HasLoggedErrors;
Expand Down

0 comments on commit 82f1fed

Please # to comment.