-
Hello, I'm wondering if it's possible from a MethodAspect to make the declaring class implementing an interface. Here my code: Source code public interface IObject {}
public interface IHandleObject<TObject> where TObject : IObject
{
void Handle(TObject instance);
}
internal class Program
{
[ObjectHandler]
public void Handle(Object1 instance)
{
}
public class Object1 : IObject { }
} Aspect code using Metalama.Framework.Aspects;
using Metalama.Framework.Code;
using Metalama.Framework.Code.Collections;
public class ObjectHandler : MethodAspect
{
/// <inheritdoc />
public override void BuildAspect(IAspectBuilder<IMethod> builder)
{
base.BuildAspect(builder);
var interfaceType =
typeof(IHandleObject<>).MakeGenericType(builder.Target.Parameters.OfName("instance")!.Type.ToType());
builder.Advice.ImplementInterface(builder.Target.DeclaringType, interfaceType);
}
} I would expect that the aspect adds Is there something I'm doing wrong or such scenario is not supported by Metalama? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi. That particular exception happens because usage of The fundamental problem though, is that implementing an interface whose members have "unknown" types is currently not supported. The closest you can get right now is: (Try Metalama link) public class ObjectHandler : MethodAspect
{
public override void BuildAspect(IAspectBuilder<IMethod> builder)
{
base.BuildAspect(builder);
var interfaceType =
((INamedType)TypeFactory.GetType(typeof(IHandleObject<>))).WithTypeArguments(builder.Target.Parameters.OfName("instance")!.Type);
builder.Advice.ImplementInterface(builder.Target.DeclaringType, interfaceType);
}
[InterfaceMember(IsExplicit = true)]
internal void Handle(Program.Object1 instance) => meta.This.Handle(instance);
} But that's obviously not what you want, since it requires that the aspect knows the type We plan to support this scenario in a future version of Metalama. |
Beta Was this translation helpful? Give feedback.
Hi.
That particular exception happens because usage of
Type
in aspects is limited. Instead, you have to useTypeFactory
and work withIType
.The fundamental problem though, is that implementing an interface whose members have "unknown" types is currently not supported. The closest you can get right now is: (Try Metalama link)