如何连接 IAspectProvider 以便 PostSharp 使用它?
How do I wire up an IAspectProvider so that PostSharp will use it?
我用 C# 编写了一个 class 来实现 PostSharp 的 IAspectProvider
接口。我不明白的是,既然我拥有了它,我应该如何处理它。我似乎无法在 PostSharp 的网站上找到任何文档来告诉我如何处理这个 class 一旦它被写入。
PostSharp 是否只是自动找到这个 class 因为它派生自 IAspectProvider
并使用它?或者是否有一个 link 可用于我迄今为止无法找到的页面?
FWIW,下面提供了 class。 (MethodTracingAspect
是我们用于训练的自定义方面。)
namespace LoggingSample
{
using System;
using System.Collections.Generic;
using System.Linq;
using PostSharp.Aspects;
internal class ProviderAspect : IAspectProvider
{
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
var type = (Type) targetElement;
return type.GetMethods()
.Where(m => type.IsAssignableFrom(typeof(IReportBuilder))
&& m.IsPublic
&& m.Name == "Execute")
.Select(m => new AspectInstance(targetElement,
new MethodTracingAspect()));
}
}
}
对于 PostSharp,class 实现 IAspectProvider
只是另一方面,您需要以与任何其他方面相同的方式将其应用于目标元素。因此,通常,您的方面提供者 class 也应该派生自属性 class 之一。然后你可以将它作为一个属性应用到目标元素,这个元素将在构建时传递到 ProvideAspects
方法。
在您的具体示例中,您可以从 TypeLevelAspect
派生,因为您希望 targetElement
是一个类型。
此文档页面上提供了方面提供程序的示例:http://doc.postsharp.net/example-dataattributes
方面用法示例:
// Aspect class
[Serializable]
public class ProviderAspect : TypeLevelAspect, IAspectProvider
{
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
// ...
}
}
// Apply to a single type
[ProviderAspect]
public class TargetClass : IReportBuilder
{
public void Execute()
{
// ...
}
}
// Apply to many types
[assembly: ProviderAspect (AttributeTargetTypes="OurCompany.OurApplication.Reports.*")]
您可以在此文档页面上找到有关将方面应用于代码的更多信息:http://doc.postsharp.net/applying-aspects
P.S。我们还将查看方面提供程序的文档页面并添加有关使用的信息。
我用 C# 编写了一个 class 来实现 PostSharp 的 IAspectProvider
接口。我不明白的是,既然我拥有了它,我应该如何处理它。我似乎无法在 PostSharp 的网站上找到任何文档来告诉我如何处理这个 class 一旦它被写入。
PostSharp 是否只是自动找到这个 class 因为它派生自 IAspectProvider
并使用它?或者是否有一个 link 可用于我迄今为止无法找到的页面?
FWIW,下面提供了 class。 (MethodTracingAspect
是我们用于训练的自定义方面。)
namespace LoggingSample
{
using System;
using System.Collections.Generic;
using System.Linq;
using PostSharp.Aspects;
internal class ProviderAspect : IAspectProvider
{
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
var type = (Type) targetElement;
return type.GetMethods()
.Where(m => type.IsAssignableFrom(typeof(IReportBuilder))
&& m.IsPublic
&& m.Name == "Execute")
.Select(m => new AspectInstance(targetElement,
new MethodTracingAspect()));
}
}
}
对于 PostSharp,class 实现 IAspectProvider
只是另一方面,您需要以与任何其他方面相同的方式将其应用于目标元素。因此,通常,您的方面提供者 class 也应该派生自属性 class 之一。然后你可以将它作为一个属性应用到目标元素,这个元素将在构建时传递到 ProvideAspects
方法。
在您的具体示例中,您可以从 TypeLevelAspect
派生,因为您希望 targetElement
是一个类型。
此文档页面上提供了方面提供程序的示例:http://doc.postsharp.net/example-dataattributes
方面用法示例:
// Aspect class
[Serializable]
public class ProviderAspect : TypeLevelAspect, IAspectProvider
{
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
// ...
}
}
// Apply to a single type
[ProviderAspect]
public class TargetClass : IReportBuilder
{
public void Execute()
{
// ...
}
}
// Apply to many types
[assembly: ProviderAspect (AttributeTargetTypes="OurCompany.OurApplication.Reports.*")]
您可以在此文档页面上找到有关将方面应用于代码的更多信息:http://doc.postsharp.net/applying-aspects
P.S。我们还将查看方面提供程序的文档页面并添加有关使用的信息。