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 (https://github.com/postsharp/Metalama/issues/199) and one cannot limit eligibility based on instance members (such as this Type - https://github.com/postsharp/Metalama/issues/198), 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!
GitHub
Feature request: Support generic aspect types (TypeAspect) · Issue ...
I have a need to apply generic constraints to a type passed into an aspect. This is because the type is used in downstream methods which themselves have such constraints. This means that it's n...
GitHub
Issues · postsharp/Metalama
Metalama is a Roslyn-based meta-programming framework. Use this repo to report bugs or ask questions. - Issues · postsharp/Metalama
Petr Onderka
Petr Onderka291d ago
I still think that a reporting an error as a diagnostic is currently the best option. You mention in one of the issues that when your aspect is applied incorrectly, it produces lots of errors. I think that can be avoided using something like:
public override void BuildAspect(IAspectBuilder<INamedType> builder)
{
bool parameterSatisfiesConstraint = ...;
if (!parameterSatisfiesConstraint)
{
builder.Diagnostics.Report(parameterDoesNotSatisfyConstrainError.WithArguments(...));
return;
}

// actually implement the aspect here
}
public override void BuildAspect(IAspectBuilder<INamedType> builder)
{
bool parameterSatisfiesConstraint = ...;
if (!parameterSatisfiesConstraint)
{
builder.Diagnostics.Report(parameterDoesNotSatisfyConstrainError.WithArguments(...));
return;
}

// actually implement the aspect here
}
Xaniff
Xaniff291d ago
Yeah, I've got a whole pile of issues showing up because some of the types I'm instantiating have generic constraints on them and I'm not able to show that the Type I pass into the attribute actually has those constraints - it struck me that I could just use the generic attributes from C#11, but when I tried that I learned that they are not yet supported. Since generic attributes are on the distant roadmap, let me ask this another way. Metalama is generating valid-looking C# except that the compiler simply doesn't see that the type as used implements those constraints, so it doesn't like the lines Metalama generates that pass this type via a TypeFactory into a new expression as a generic parameter. Do you have any suggestions of how I might indicate that a given Type actually implements IEquatable<>, IComparable and IComparable<> to downstream places where I pass it in via a TypeFactory such that the compiler would be able to understand it implemented these interfaces even though they're not generically constrained on the attribute?
Petr Onderka
Petr Onderka291d ago
The generated code isn't special, whatever works in regular C# should work in generated code as well. Can you give an example of generated code that doesn't work for you?
Xaniff
Xaniff291d ago
Yeah, I'll see if I can't narrow this down to something that's not too.. long I may have to sleep on this, but I'll try to get a sample that's not too long that demonstrates the issue - it's a trick since this whole thing is one long set of INamedTypes and IFields/Properties from the start to the end
Petr Onderka
Petr Onderka291d ago
Thanks. (Though keep in mind that length is not the most important factor, being self-contained is. If you could easily produce long code that I can use to reproduce the issue, that's still mostly fine. Having short repro is always appreciated, but not strictly speaking necessary.)
Xaniff
Xaniff291d ago
Yeah, that's the trick too - I'm trying to do an awful lot of things at once with this aspect and while it generates code at this point, I started the day with 36 runtime errors and have since narrowed it down to 16. Something happened with the method that was showing all those constraint errors earlier and now it's not populating anything at all, but I've been tackling the other bugs trying to circle back to it for this issue.
Want results from more Discord servers?
Add your server
More Posts
Step-through aspect-debuggingI'm sure I've asked this before, but I cannot find the thread in which it was answered (if I actuallCodeLens HelpMetalama is configured in CodeLens, and I have an aspect that has been applied. I am able to right-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 Advising the addition of code commentsUsing Metalama Is there a way to advise adding code comments? For example, when I advise a method Set eligibility to not run against (built-in) methods of record classes or structsI've figured out how to set up an `If` statement for the record types, but I cannot figure out how tHow 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) WCreate NuGet with MetalamaWith source generators I have to add some MSBuild configuration, but with Metalama it seems I don't Override property with BuildAspectI create exceptions inheriting from Exception and putting the `[BusinessException]` attribute. I waView 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-lev