PostSharp Class-Aspect 向 class 的所有成员添加方面
PostSharp Class-Aspect to add aspects to all members of the class
有没有办法用一个属性来标记 Class,从而将属性添加到所有方法?
例如:
[TestAspect]
public class Test
{
public void foo() { ... };
[AttributeA]
public void bar() { ... };
}
现在 TestAspect 应该做到这一点,将方面添加到 bar()。
我已经写了一个 AspectProvider
class,下面应该将 AspectA 和 AspectB 应用于 class 的所有方法,它们具有 AttributeA。
[Serializable, AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class TestAspect : TypeLevelAspect, IAspectProvider
{
private static readonly CustomAttributeIntroductionAspect aspectA =
new CustomAttributeIntroductionAspect(
new ObjectConstruction(typeof(AspectB).GetConstructor(Type.EmptyTypes)));
private static readonly CustomAttributeIntroductionAspect aspectB =
new CustomAttributeIntroductionAspect(
new ObjectConstruction(typeof(AspectA).GetConstructor(Type.EmptyTypes)));
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
Type targetClassType = (Type)targetElement;
foreach(MethodInfo methodInfo in targetClassType.GetMethods())
{
if(!methodInfo.IsDefined(typeof(AttributeA), false))
{
yield break;
}
yield return new AspectInstance(targetElement, aspectA);
yield return new AspectInstance(targetElement, aspectB);
}
}
但是不幸的是属性没有应用到方法上?没有抛出异常或错误。
有人有什么建议吗?
使用下面的代码,我至少可以在我的 Class 中用 AttributeA
修饰的方法中添加 1 个自定义属性,这些方法用 TestClass
标记。
[Serializable, AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class TestClassAttribute : TypeLevelAspect, IAspectProvider
{
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
Type type = (Type)targetElement;
return type.GetMethods().Where(method => method.GetCustomAttributes(typeof(AttributeA), false)
.Any()).Select(m => new AspectInstance(m, new CustomAttributeIntroductionAspect(
new ObjectConstruction(typeof(AttributeB).GetConstructor(Type.EmptyTypes)))));
}
}
有没有办法用一个属性来标记 Class,从而将属性添加到所有方法?
例如:
[TestAspect]
public class Test
{
public void foo() { ... };
[AttributeA]
public void bar() { ... };
}
现在 TestAspect 应该做到这一点,将方面添加到 bar()。
我已经写了一个 AspectProvider
class,下面应该将 AspectA 和 AspectB 应用于 class 的所有方法,它们具有 AttributeA。
[Serializable, AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class TestAspect : TypeLevelAspect, IAspectProvider
{
private static readonly CustomAttributeIntroductionAspect aspectA =
new CustomAttributeIntroductionAspect(
new ObjectConstruction(typeof(AspectB).GetConstructor(Type.EmptyTypes)));
private static readonly CustomAttributeIntroductionAspect aspectB =
new CustomAttributeIntroductionAspect(
new ObjectConstruction(typeof(AspectA).GetConstructor(Type.EmptyTypes)));
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
Type targetClassType = (Type)targetElement;
foreach(MethodInfo methodInfo in targetClassType.GetMethods())
{
if(!methodInfo.IsDefined(typeof(AttributeA), false))
{
yield break;
}
yield return new AspectInstance(targetElement, aspectA);
yield return new AspectInstance(targetElement, aspectB);
}
}
但是不幸的是属性没有应用到方法上?没有抛出异常或错误。
有人有什么建议吗?
使用下面的代码,我至少可以在我的 Class 中用 AttributeA
修饰的方法中添加 1 个自定义属性,这些方法用 TestClass
标记。
[Serializable, AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class TestClassAttribute : TypeLevelAspect, IAspectProvider
{
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
Type type = (Type)targetElement;
return type.GetMethods().Where(method => method.GetCustomAttributes(typeof(AttributeA), false)
.Any()).Select(m => new AspectInstance(m, new CustomAttributeIntroductionAspect(
new ObjectConstruction(typeof(AttributeB).GetConstructor(Type.EmptyTypes)))));
}
}