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:
[BusinessException]
public class UserAlreadyExistsException(int age, string email) : Exception;
[BusinessException]
public class UserAlreadyExistsException(int age, string email) : Exception;
I started with [Introduce], but later I noticed I need BuildAspect to access constructors. I've tried with this, but I don't know how to do complete the code. The goal is to have: string.Format("The user with age {0} and email {1} already exists", age, email); Code: https://try.metalama.net/#N4IgZiBcDaoHZRABQE4HsDmKCGBbAdAMYDOIANCIYgK7ECWcGABALICmALtgDZ7b4AxHLjYB3NCgDW+AILEADm0IdiAbgA6cWg2bsuvXPyF4xE6QGE0AEzYat9Rq048+g4aan4AotzoY6AEZ0vhwAnnaacCYK2IRsrKEAMoE4KOGamvLUAb6ETIS8xMRMAELacGxFXgAecfIcdGhwMhwcKIHUHPGQTAAqoYpyisqawJpME0zQAJJwbdbUcQAUAOoAFmxwNXTEKkwAvEwA8gBubCjtNgDKbdhdGKH4p+eXbACUALrjk1k5dHm7dqOdhFbAYNjfCZjOCTWFMcEcSFw6Fw1FME7YFBMLq4eS8LoHJjqEC9DZMWjnJiiOgcNZMMHxYAABgAvvS4FYmGxDMEmMAAIxsngoNjYKyhLnVHYqYl2NFwkUcagoGGAnSCCSGDhLHF4u5sMhEkDEw3E4lvOVollI60wyZI365JhoM4XOg2dFod2lajBKxDJTa6YB5RlP3nAA80wAciYrP1FAA+JgBX3cGwoN5IlFogLYYhsfBh9Mh7Wp8OZiJ21EYrEiUHg1BoRQoMKE8vp874XqYhFI+X4JsthqVftowR0FC7JbVA7J6r4WMiA6HYkg4gM82WmuYpjyTEmLpT9tpjPd3ucMeo-CWOCAxYcCTEK9widT7VZ6vypiDg8iI9qBkX6wmAEiioQdJLLWe5-pwlIMDB7gAZ+8o5t+Kanl2MhWCc-yFrM8xWIsbBDucYRLCh6EYRWsg4XhTyuq8Sz1hujboMOoSGpRqK2rCtosuQICSIgHAgCyHwskAA
Petr Onderka
Petr Onderka311d ago
You can access constructors from a template, like this:
[Introduce(WhenExists = OverrideStrategy.Override)]
public string Message
{
get
{
var parameters = meta.Target.Type
.Constructors
.First()
.Parameters;

var template = "The user with age {0} and email {1} already exists";
return string.Format(template, parameters[0].Value, parameters[1].Value);
}
}
[Introduce(WhenExists = OverrideStrategy.Override)]
public string Message
{
get
{
var parameters = meta.Target.Type
.Constructors
.First()
.Parameters;

var template = "The user with age {0} and email {1} already exists";
return string.Format(template, parameters[0].Value, parameters[1].Value);
}
}
But keep in mind that Metalama does not support preview features of C#, so this is not guaranteed to work. (And to make sure you're aware of this, we require you to specify <MetalamaAllowPreviewLanguageFeatures>true</MetalamaAllowPreviewLanguageFeatures> in your project file.) In particular, there are some things that don't work well: 1. It doesn't work if the exception class has no body (;), it requires at least an empty body ({}). 2. There is no way to tell if a constructor is primary.
Andreu
Andreu311d ago
Thanks! But can I do anything to use an empty body? For example, generate properties and use string.Format with them, use a preview version of Metalama, anything?
Petr Onderka
Petr Onderka311d ago
Do you mean no body? (Empty body should work.) There is currently nothing you can do, Metalama simply does not expect that a class could have no body and so it won't work if you want to introduce something there.
Andreu
Andreu311d ago
Yes, I mean that. Then, if I can't apply any hack meanwhile it gets proper support, please, can you tell me what's the approximate time Metalama will need to support the new C# version? It's because my library needs yes or yes C# 12 and .NET 8
Petr Onderka
Petr Onderka311d ago
Proper support will come very soon after Microsoft releases C# 12, which will be in November.
Want results from more Discord servers?
Add your server
More Posts
View diff in RiderI don't use Visual Studio, and overall, I don't have Windows. Is there any way to see the source genOverride constructorIs it possible to override a constructor? I want to take my code: ``` public class BusinessExceptiDoes Metalama only support attributes?I navigated in all Metalama documentation, but I want to override a method for all types that inheriCan Metalama suppress compiler errors?Hello! I want to know if I can use Metalama in my library. The users of my library have to declare Eligibility rule based on target method not returning voidTypically eligibility rules I've put together are based on a specifically typed return or paramters.Follow up question from yesterday's meetupNow that I've had time to review what was discussed I think I can now ask the question I'd wanted toPlease cache licensing key rather than check every buildEvery time I attempt to build an aspect today, I'm getting an error that I've got an invalid licenseDocumentation: Bad linkAt https://doc.metalama.net/conceptual/using/fabrics there's a link at the bottom titled "ConfigurinCan one pass state between aspects?One can trivially pass arbitrary data between the aspect class and a template via the 'args' parametOrdering AspectsIn the documentation it states: 'Aspects must be ordered using the AspectOrderAttribute assembly-levThis error has me foxedIt's our old friend LAMA0001 Can't work out where it is getting Net Framework reference from. PrAddAspect RefactoringConsider the following scenario. There are two aspects that can potentially be added to a method (i