Metalama Community


Metalama Community

Join the community to ask questions about Metalama Community and get answers from other members.

Join Server

Trying to override a method and pass an enum

I'm trying to create an OverrideMethodAspect to transform a method. I want to pass an enum value into the aspect attribute, but I get an error complaining about the enum being runtime and the aspect being compile time. What is the best way to accomplish this? I'm trying to decorate my methods with something like [ParserRule(SyntaxNodeType.Expression)].

How to inject factory-based dependencies from method aspect into type?

Let's use an example - in Microsoft.Extensions.Logging, I might register an ILoggerFactory so I can inject it into a type's constructor and then build a field from that. For example, typically this might look like this usually: ```cs public class MyType {...

IntroduceDependency with record types

The IntroduceDependencyAttribute used within the SimpleLogAttribute to inject an ILogger _logger isn't working on record types at compile-time. I'm getting this error: Severity Code Description Project File Line Suppression State Error LAMA0041 'OverrideMethodAspect.BuildAspect' threw 'InvalidCastException' when applied to 'SomeRecord.GetValue()': Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.RecordDeclarationSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.ConstructorDeclarationSyntax'. Exception details are in 'C:\Users\xxxx\AppData\Local\Temp\Metalama\CrashReports\2023.2.5\exception-f187d93b-fe84-4ec8-8ded-e0785ba642aa.txt'. To attach a debugger to the compiler, use the '-p:MetalamaDebugCompiler=True' command-line option. Entities C:\Source\xxxx\SomeRecord.cs 11 Active ...

Giving Aspects Metadata ?

I've only just started messing with MetaLama and going thru the documentation and videos but I was wondering if its possible to give an attribute extra metadata to send based on the place its invoked for example ``` public class HookFunctionAndPrint : OverrideMethodAspect { public override dynamic? OverrideMethod()...

Unable to get aspect testing to work

I've got a very simple class set up to apply a logging aspect to so I can get the hang down of testing aspects. I've created the starting class to test with Metalama.Framework (latest preview) installed, viewed the preview to get the aspect-applied version and copied that into <name>.t.cs and then installed the Metalama.Testing.AspectTesting package (latest preview). When I run all tests, I get the following (per the attached image). Any guesses what I'm doing wrong here?...
No description

How to debug a "Cannot preview the transformed code: unknown error"?

I've got an aspect that's building without error and a target project that's applying the aspect that also builds without error. Despite that, if I attempt to view the Metalama preview of the applied aspect, I get an error "Cannot preview the transformed code: unknown error" in the gold bar in VS. No crash report is generated for this error. Any ideas of how to diagnose what's happening here?...

Warn on ineligibility instead of throwing

I've got two conditions in my BuildEligibility for a method override aspect: If the target type is a record the method must be explcitly declared on the type, and the type's name must end with "Store". Unfortunately, when looking at the build logs, this means I've got several dozen errors as it's throwing an exception for any conditions that fails eligibility. I'd rather it not throw an error, but rather simply notate that it's skipped the application with a warning. Is this possible? Can I still get the benefits of build eligibility without it breaking the build due to all the errors thrown? Thanks!...

Referencing value assigned to variable in statement builder within another statement

Given a statement that does something like: ```cs var expr = new ExpressionBuilder(); expr.AppendVerbatim("var propertyValue = ("); expr.AppendType(field.Type);...

Specifying generic constraints of compile-time introduced type

I have a TypeAspect that has three attribute arguments when applied to a type. One of these is a Type and is sprinkled liberally through my resulting compiled target. Since generic typed aspects aren't yet supported ( and one cannot limit eligibility based on instance members (such as this Type -, I'm at a loss as to how I can specify the generic constraints of this type (must implement IEquatable<>) at compile-time otherwise. Any ideas? Thanks!...

Step-through aspect-debugging

I'm sure I've asked this before, but I cannot find the thread in which it was answered (if I actually did).
Given an aspect, is it possible to step through my code (e.g. see the while Metalama is building the compile-time code? I'm trying to determine if a given type implements an IEnumerable, and if so, what the generic argument is for this IEnumerable, but I can't figure out how to peek inside it while debugging (Debugger.Break never hits) to see what precisely is happening. ```cs...

CodeLens Help

Metalama is configured in CodeLens, and I have an aspect that has been applied. I am able to right-click and see the metalama diff. However, I am unable to see any of the details in CodeLens as described here -

How to apply an attribute to a method introduced by another aspect?

I have an aspect that has a dependency on another aspect. However, I'd like for downstream projects to be able to elect to apply either aspect without actually specifying both on the page. When applying the changes via a TypeAspect, how might I have it decorate the type with the attribute for another aspect? Thank you!...

Advising the addition of code comments

Using Metalama Is there a way to advise adding code comments? For example, when I advise a method and inject a new attribute, I would also like to add additional xml code documentation. Here's the exact scenario. [ApiController]...

Set eligibility to not run against (built-in) methods of record classes or structs

I've figured out how to set up an If statement for the record types, but I cannot figure out how to end it - how can I ensure my OverrideMethodAspect doesn't actually apply to any methods that are attached to a record? Even better though - is there any way to have it not apply to any methods that aren't automatically a part of a record (e.g. don't apply to ToString or Equals, but do apply to MyCustomMethod without specifically calling such custom methods out)? Thanks!...

How to specify an IMethod as the callback for an event?

I'd like to specify an introduced method on a TypeAspect as the callback for an event handler. 1) When I attempt to just pass it in via a template, I get squigglies: dictionary.OnDictionaryChanged += myDictionaryChangedMethod; //Doesn't like this since IMethod isn't a System.EventHandler 2) How can I similarly remove the event in a subsequently introduced Dispose() method? ...

Create NuGet with Metalama

With source generators I have to add some MSBuild configuration, but with Metalama it seems I don't need it. My goal is to create a fabric that detects classes inheriting from BusinessException, and then apply an attribute/aspect to it, generating code. Then I'll publish a NuGet to, and when I create it (dotnet build & pack), I'll pass the license key with an envinroment variable to MSBuild....

Override property with BuildAspect

I create exceptions inheriting from Exception and putting the [BusinessException] attribute. I want to override the Message property using string.Format with the primary constructor parameters. For example, the next class: ```...

View diff in Rider

I don't use Visual Studio, and overall, I don't have Windows. Is there any way to see the source generated by Metalama? With source generators I could see the source in the IDE, but with Metalama I can't....

Override constructor

Is it possible to override a constructor? I want to take my code: ```...

Does Metalama only support attributes?

I navigated in all Metalama documentation, but I want to override a method for all types that inherit from BusinessException. I don't want to use attributes because for my use case it's more natural to extend from a class....