Closed epsitec closed 1 month ago
Hello @epsitec, thank you for submitting this issue. We will try to get back to you as soon as possible. Note to the PostSharp team, this ticket is being tracked in our dashboard under ID TP-33174.
Here is my current attempt:
public abstract class Entity<T>
where T : class
{ }
public interface IPerson
{
string Name { get; }
int Age { get; }
}
[EntityImplementation]
public class Person : Entity<IPerson>
{ }
public sealed class EntityImplementationAttribute : TypeAspect
{
public override void BuildAspect(IAspectBuilder<INamedType> builder)
{
_ = builder ?? throw new ArgumentNullException (nameof (builder));
var typeName = builder.Target.Name;
var baseType = builder.Target.BaseType
?? throw new InvalidCastException ("No base type");
if (baseType.IsGeneric == false)
{
throw new InvalidOperationException ($"Base type of {typeName} should be generic");
}
if (baseType.TypeParameters.Count != 1)
{
throw new InvalidOperationException ($"Base type of {typeName} should have one generic parameter");
}
var interfaceType = (INamedType) baseType.TypeArguments[0];
if (interfaceType.TypeKind != TypeKind.Interface)
{
throw new InvalidOperationException ($"The generic type parameter <{interfaceType.ToType ().Name}> of the base type of {typeName} should be an interface, not {interfaceType.TypeKind}");
}
var interfaceProperties = interfaceType.AllProperties;
foreach (var interfaceProperty in interfaceProperties)
{
builder.Advice.IntroduceAutomaticProperty (
builder.Target,
interfaceProperty.Name,
interfaceProperty.Type.ToType (),
OverrideStrategy.Ignore,
b => b.Accessibility = Accessibility.Public);
}
/*
builder.Advice.ImplementInterface (
builder.Target,
interfaceType,
OverrideStrategy.Ignore);
*/
}
}
To upvote a feature request please add a "me too" comment because the number of emoticon reactions is not aggregated by GitHUb.
me too
me too
me too
me too
me too
mee too
me too
me too
This feature is being worked on in 2024.2 and the first bits have been released in preview. See IAdviceFactory.IntroduceClass
.
Thank you. I could not wait for the feature to be implemented in Metalama, so I switched altogether to an incremental Roslyn Source Generator to produce the code I neded.
@epsitec This is an available feature in the latest stable version of Metalama.
I'd like to be write an aspect to generate classes implementing data access interfaces. Currently (2023.1) my code won't work:
Example interface
Example of the code I'd like my aspect to generate
What's working, what's not working
IPerson
toPerson
inBuildAspect
by callingbuilder.Advice.ImplementInterface()
.Name
andAge
to the class by callingbuilder.Advice.IntroduceAutomaticProperty()
.[InterfaceMember]
should be applied dynamically).The last point is a show-stopper for what I am intending to do. I'd love to see this feature be part of a coming release of Metalama.