标签: postsharp

我可以仅将自定义属性限制为void方法吗?

我有一个自定义属性,我想限制为返回类型为void的方法.

我知道我可以限制使用方法,[AttributeUsage(AttributeTargets.Method)]但似乎没有办法限制返回类型或方法签名的任何其他方面.

[System.Diagnostics.Conditional]属性完全具有我想要的限制.将其添加到非void方法会导致编译器错误:

Conditional属性在'(SomeMethod)'上无效,因为它的返回类型不是void

和IntelliSense说:

属性'System.Diagnostics.ConditionalAttribute'仅对具有'void'返回类型的属性类或方法有效.

如果我F12到了,ConditionalAttribute我看到它装饰有以下属性:

[Serializable]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,AllowMultiple = true)]
[ComVisible(true)]

其中没有任何关于返回类型的说明.

如何为Conditional属性完成,我可以为自定义属性执行相同的操作吗?

c# attributes postsharp custom-attributes

9
推荐指数
1
解决办法
580
查看次数

PostSharp之后pdb文件正在消失

我正在使用PostSharp版本2.1.6.4(也尝试了最新版本2.1.7.35),有时pdb文件丢失,并且有一个pssym文件.

<?xml version="1.0" encoding="utf-8"?>
<Symbols xmlns="http://schemas.postsharp.org/2.0/symbols">
  <Class Class="#1=T:[CrosscuttingLogging]CrosscuttingLogging.Attributes.LogMethodCallStatsAttribute" LimitedLicense="true" />
  <Class Class="#2=T:[RequestLimiter]RequestLimiter.RequestCounterAttribute" LimitedLicense="true" />
</Symbols>
Run Code Online (Sandbox Code Playgroud)

我在构建过程中运行procmon,据我所知,postsharp.srv.4.0-x86.exe进程将dll和pdb文件从obj\Debug文件夹移动到obj\Debug\Before-PostSharp文件夹,然后在文件夹中生成一个新的dll obj\Debug,但是没有生成新的pdb文件.

对于我的一些dll(看似随机)会发生这种情况并且似乎不可靠,因为在其他机器上所有pdb文件都是正确生成的.

c# msbuild postsharp pdb visual-studio-2015

9
推荐指数
2
解决办法
2625
查看次数

为什么要使用后期编译器?

我正在努力理解为什么需要像PostSharp这样的后期编译器?

我的理解是它只是在原始代码中插入代码,所以为什么开发人员不会自己编写代码呢?

我希望有人会说它更容易编写,因为你可以使用方法上的属性,然后不会混淆它们的样板代码,但这可以使用DI或反射和没有后编译器的预先考虑.我知道,因为我已经说过反射,性能大象现在会进入 - 但我不关心这里的相对性能,当大多数场景的绝对性能是微不足道的(亚毫秒到毫秒).

postsharp

8
推荐指数
3
解决办法
1506
查看次数

使用PostSharp将属性应用于接口

我希望能够将属性应用于接口,以便实现该接口的任何类中的每个方法都将应用该属性.

我以为它看起来像这样:

[Serializable]
[AttributeUsage(AttributeTargets.All, Inherited = true)]
public sealed class TestAttribute : OnMethodBoundaryAspect
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

然而,当我将它应用于如下界面时,在实现接口的类中调用方法时,永远不会访问属性中的OnEntry/OnExit代码:

[Test]
public interface ISystemService
{
    List<AssemblyInfo> GetAssemblyInfo();
}
Run Code Online (Sandbox Code Playgroud)

如果我在实现类本身中应用该属性,如下所示,它可以正常工作:

[Test]
public class SystemService : ISystemService
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么/做错了什么?

c# attributes interface postsharp

8
推荐指数
1
解决办法
2044
查看次数

汇编宽组播属性.他们是邪恶的吗?

我正在开发一个项目,我们在AssemblyInfo.cs中有几个属性,这些属性被多播到特定类的方法.

[assembly: Repeatable(
AspectPriority = 2,
AttributeTargetAssemblies = "MyNamespace",
AttributeTargetTypes = "MyNamespace.MyClass", 
AttributeTargetMemberAttributes = MulticastAttributes.Public,
AttributeTargetMembers = "*Impl", Prefix = "Cls")]
Run Code Online (Sandbox Code Playgroud)

我不喜欢这个,是它把一个逻辑放入AssemblyInfo(信息,请注意!),对于初学者来说根本不应该包含任何逻辑.最糟糕的部分是,实际的MyClass.cs在文件中没有任何属性,并且完全不清楚这个类的方法可能有它们.从我的角度来看,它极大地损害了代码的可读性(更不用说过度使用PostSharp会使调试成为一场噩梦).特别是当你有多个组播属性时.

这里的最佳做法是什么?有没有人使用像这样的PostSharp属性?

c# postsharp

8
推荐指数
1
解决办法
1082
查看次数

一种方法的多个方面

在我的应用程序中,我以前使用常规C#属性来"注释"方法.例如:


[Foo(SomeKey="A", SomeValue="3")]
[Foo(SomeKey="B", SomeValue="4")]
public void TheMethod()
{
   SpecialAttributeLogicHere();
}


SpecialAttributeLogicHere()所做的是反思地查看注释了这个特定方法的所有Foo属性.然后(它自己),为所有键和值创建自己的字典.

我现在正试图转移到PostSharp,因为在OnEntry中可以将SpecialAttributeLogic放入一个方面(并从方法体中移除,它更干净!).Foo将被扩展OnMethodBoundaryAspect的方面所取代.

我仍然希望以下列方式使用它:


[Foo(SomeKey="A", SomeValue="3")]
[Foo(SomeKey="B", SomeValue="4")]
Run Code Online (Sandbox Code Playgroud)

但是如果Foo有OnEntry,那意味着"SpecialAttributeLogic"将被执行两次.我基本上需要将每个Foo()的所有键和值"收集"到一个字典中,然后我将一些逻辑应用到字典中.

如何使用PostSharp执行此操作(或最佳实践)?谢谢!

c# aop postsharp

8
推荐指数
1
解决办法
1116
查看次数

AOP vs MVC FilterAttributes vs Interceptor

  1. ASP.NET MVC建议使用或扩展内置的Authorization,Action,Result,Exception过滤器.
  2. 第3方.Net IoC容器(Unity,Ninject,Autofac)提出拦截器
  3. 第三方AOP工具(Postsharp)提出了他们的属性.

现在,我搞砸了.可能是我混合了所有这些.我想构建健壮的代码和稳定的方法,我应该使用什么?

asp.net-mvc ninject postsharp unity-container autofac

8
推荐指数
1
解决办法
3066
查看次数

您如何反映应用于返回值的属性?

考虑以下:

[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public class NotNullAttribute : Attribute
{
}

public class Class1
{
    [return: NotNull]
    public static string TestMethod([NotNull] string arg)
    {
        return arg + " + " + arg;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用System.Reflection,您是否会看到NotNullAttribute属性已应用于方法的返回值?如果你不能,[return:]语法背后的目的是什么?

c# reflection attributes postsharp

7
推荐指数
1
解决办法
2395
查看次数

构建后删除PostSharp引用?

是否有可能获得postharp以在构建期间删除对postsharp程序集的引用?

我有一个exe我需要占用很小的空间.我想使用postharp的一些编译时编织,但不想使用exe部署PostSharp.dll.

我正在使用PostSharp 2(具体为2.0.4.1074)

.net reference postsharp

7
推荐指数
1
解决办法
1317
查看次数

PostSharp OnMethodBoundaryAspect OnEntry Not Executing

我正在运行.NET 4.0 Web应用程序(不是网站)和PostSharp 1.5.我无法使用OnMethodBoundaryAspect基类执行OnEntry重写方法.这是一些相关的代码:

public sealed class MonitorAttribute : OnMethodBoundaryAspect {

    public string[] SomeValue { get; protected set; }         

    public MonitorAttribute (params string[] someValue){
        SomeValue = someValue;
    }

    public override void OnEntry(MethodExecutionEventArgs eventArgs){
        // do Something here
        base.OnEntry(eventArgs);
    }

}

public sealed class MyUsageClass : IMyUsageClass {

    [Monitor(new string[]{ 'Test' })
    public void SomeMethod {
        // Do something else in here
    }        

}
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?它永远不会击中OnEntry方法.我还尝试用新的2.0版本替换我的PostSharp.dll和PostSharp.Laos.dll依赖项.如果它有任何区别MyUsageClass由StructureMap实例化.

c# aop postsharp .net-4.0

7
推荐指数
1
解决办法
3002
查看次数