我有一个自定义属性,我想限制为返回类型为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属性完成,我可以为自定义属性执行相同的操作吗?
我正在使用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文件都是正确生成的.
我正在努力理解为什么需要像PostSharp这样的后期编译器?
我的理解是它只是在原始代码中插入代码,所以为什么开发人员不会自己编写代码呢?
我希望有人会说它更容易编写,因为你可以使用方法上的属性,然后不会混淆它们的样板代码,但这可以使用DI或反射和没有后编译器的预先考虑.我知道,因为我已经说过反射,性能大象现在会进入 - 但我不关心这里的相对性能,当大多数场景的绝对性能是微不足道的(亚毫秒到毫秒).
我希望能够将属性应用于接口,以便实现该接口的任何类中的每个方法都将应用该属性.
我以为它看起来像这样:
[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)
我错过了什么/做错了什么?
我正在开发一个项目,我们在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#属性来"注释"方法.例如:
[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执行此操作(或最佳实践)?谢谢!
现在,我搞砸了.可能是我混合了所有这些.我想构建健壮的代码和稳定的方法,我应该使用什么?
考虑以下:
[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:]语法背后的目的是什么?
是否有可能获得postharp以在构建期间删除对postsharp程序集的引用?
我有一个exe我需要占用很小的空间.我想使用postharp的一些编译时编织,但不想使用exe部署PostSharp.dll.
我正在使用PostSharp 2(具体为2.0.4.1074)
我正在运行.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实例化.
postsharp ×10
c# ×7
attributes ×3
aop ×2
.net ×1
.net-4.0 ×1
asp.net-mvc ×1
autofac ×1
interface ×1
msbuild ×1
ninject ×1
pdb ×1
reference ×1
reflection ×1