Andreu
Andreu
Explore posts from servers
MCMetalama Community
Created by Andreu on 7/12/2023 in #technical-questions
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 nuget.org, and when I create it (dotnet build & pack), I'll pass the license key with an envinroment variable to MSBuild. And... just that? Will library users have to specify a license key? Do I have to publish the NuGet as an analyzer? Anything? Will they have Metalama code generation out-of-the-box inheriting from BusinessException? Thanks!
10 replies
MCMetalama Community
Created by Andreu on 7/12/2023 in #technical-questions
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
6 replies
MCMetalama Community
Created by Andreu on 7/12/2023 in #technical-questions
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.
4 replies
MCMetalama Community
Created by Andreu on 7/12/2023 in #technical-questions
Override constructor
Is it possible to override a constructor? I want to take my code:
public class BusinessException
{
public BusinessException(int age)
{
}
}
public class BusinessException
{
public BusinessException(int age)
{
}
}
and translate it to:
public class BusinessException
{
private int age;

public BusinessException(int age)
{
this.age = age;
}
}
public class BusinessException
{
private int age;

public BusinessException(int age)
{
this.age = age;
}
}
In the example, I add a private field age, but if it's not possible it's ok, I'll be happy with just overriding the constructor
2 replies
MCMetalama Community
Created by Andreu on 7/11/2023 in #technical-questions
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.
4 replies
MCMetalama Community
Created by Andreu on 7/11/2023 in #technical-questions
Can 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 the next class with this inheritance: public class EmailAlreadyExists(string email) : BusinessException("{0} already exists") So, with Metalama I can replace the parameter email in the message with the placeholder. For example, with: ... : BusinessException(string.Format("{0} already exists", email)) But most of developers will have <TreatWarningsAsErrors>true</TreatWarningsAsErrors>, so they'll get compiler errors because they don't use the parameter email, even if it's used with Metalama. Is this correct? Originally my intention was to use source generators, but I don't want to plague all their code with partial.
2 replies