标签: postsharp

有任何使用PostSharp和ReSharper的经验

有没有人使用PostSharp和ReSharper,如果有的话,我应该期待什么问题?

resharper postsharp

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

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

考虑以下:

[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中的AssemblyLoadException,来自引用的DLL的参数的问题?

我刚刚开始使用postharp/AOP.我想为C#做一些工具来跟踪我为一些软件编写的一些插件的用法.

我正在尝试使用OnMethodBoundaryAspect类来记录调用方法时某些参数的值.这些参数是外部DLL中引用的类型.

当我将我的属性添加到方法时,项目将无法生成,我收到以下错误

错误2未处理的异常(2.0.5.1204,64位,CLR 2.0,发布):PostSharp.CodeModel.AssemblyLoadException:加载程序集"C:\ Program Files\Autodesk\Revit Structure 2011\Program\RevitAPI.dll"时出错:可能不加载文件或程序集'revitapi,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一.不支持操作.(HRESULT异常:0x80131515)

REvitAPI.dll是包含类型的文件.我还测试过只是将属性添加到项目中但未将其应用于任何方法,这也会导致错误.所以看起来它与方法参数类型本身无关,而只是存在这个DLL.

有没有人遇到过这个问题,或者有人能指出我在哪里获得更多信息的正确方向?

c# aop postsharp

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

我没有源代码的程序集上的PostSharp

在他们网站上的示例中,PostSharp有一个拦截主系统程序集中的调用的演示.我已经尝试了几次设置和复制所有拦截调用程序集我没有源代码没有成功.

我的方法是简单地将程序集级别属性放在我想要检测的命名空间和方法上.这从来没有对我有用.

就像是:

[assembly:Trace("MyCategory",AttributeTargetTypes ="My.BusinessLayer.*")]

我在这里错过了什么吗?如果我没有为其提供源,我是否可以不在程序集上对我的检测方面进行运行时注入?我以为我可以做运行时注射......

谢谢.

c# aop postsharp aspects

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

模拟PostSharp属性的最简单方法

我正在使用PostSharp方法属性对我的WCF服务进行授权和审核.它工作正常,但现在我正在尝试使用属性进行单元测试,并且正在努力寻找一种方法来模拟和注入属性上的属性.

我的属性如下.

[Serializable]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class AuthoriseAndAuditAttribute : OnMethodBoundaryAspect
{
    private static ILog logger = AppState.logger;

    private static Ninject.IKernel _kernel = MyKernel.Kernel;

    private UserRoleTypesEnum _requiredRole = UserRoleTypesEnum.None;

    [Inject]
    public IServiceAuthToken _serviceAuthToken { get; set; }

    [Inject]
    public UserSessionDataLayer _userSessionDataLayer { get; set; }

    public AuthoriseAndAuditAttribute(UserRoleTypesEnum role = UserRoleTypesEnum.None)
    {
        _requiredRole = role;
        _kernel.Inject(this);
    }

    public override void OnEntry(MethodExecutionArgs args)
    {
        // Get the user's session from cookie.
        UserSession userSession = GetUserSession();

        // Check that …
Run Code Online (Sandbox Code Playgroud)

wcf unit-testing rhino-mocks ninject postsharp

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

NuGet在构建开始之前恢复PostSharp包

我正在使用PostSharp,我的项目文件中有以下目标描述:

<Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''">
  <Error Condition="!Exists('..\..\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://www.postsharp.net/links/nuget-restore." />
  <Error Condition="Exists('..\..\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore." />
</Target>
Run Code Online (Sandbox Code Playgroud)

据我所知,当通过NuGet引用PostSharp时,这会添加到项目中,并且错误条件会检查以下内容:

  • 当PostSharp不可用时,第一个错误条件会破坏构建(即NuGet没有成功恢复它).
  • 当NuGet在最后一次构建时成功恢复PostSharp时,第二个错误条件会破坏构建,但因此未包含在项目中,因此需要进行重建.

,如果我有以下配置NuGet.Config.csproj file,是第二差错情况甚至是必要的?

NuGet.Config 文件:

<configuration>
  <packageRestore>
    <!-- Allow NuGet to …
Run Code Online (Sandbox Code Playgroud)

msbuild postsharp visual-studio nuget nuget-package

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

当文档发生变化时,在postsharp [Command]上触发"CanExecute"?

我目前正在将一个项目迁移到PostSharp以删除大量样板代码,其中大部分代码都非常顺利,但我对如何强制命令重新检查它感到困惑CanExecute.我预计PostSharp将检查命令,就像检查依赖关系一样,这里是一个极简主义的样本

[NotifyPropertyChanged]
public class MyWindowViewModel
{
    /// Anything bound to this refreshes just fine as expected
    public ObservableCollection<SomeType> Documents = new ObservableCollection<SomeType>();

   [Command]
   public ICommand AddDocumentCommand { get; set; }
   public void ExecuteAddDocument () { Documents.Add(new SomeType()); }

   [Command]
   public ICommand CloseDocumentCommand { get; set; }
   public bool CanExecuteCloseDocument () => Documents.Any(); 
   public void ExecuteCloseDocument () { Documents.Remove(Documents.Last()); }        
}
Run Code Online (Sandbox Code Playgroud)

在开始时,集合是空的,并且关闭命令的按钮按预期变灰,但是通过附加的按钮添加文档AddDocument不会激活关闭文档按钮,什么是完成我需要的适当方法?PostSharp是仅将赋值而非方法调用视为更改还是完全不同于其他内容?

.net c# wpf postsharp

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

如何在另一个类的CacheItemPolicy上实现UpdateCallback?

我有一个使用postsharp实现的简单Cache属性.当我设置缓存策略时,我希望能够设置如下所示的更新回调.

 private static CacheItemPolicy GetCachePolicy(CacheType type, int expiry)
    {
        var policy = new CacheItemPolicy();

        switch (type)
        {
            case (CacheType.Absolute):
                policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(expiry);
                policy.UpdateCallback = new CacheEntryUpdateCallback(UpdateHandler);
                break;
            case (CacheType.Sliding):
                policy.SlidingExpiration = new TimeSpan(0, 0, 0, expiry);
                break;
        }

        return policy;
    }
Run Code Online (Sandbox Code Playgroud)

如果我只想这样做,这很好:

 private static void UpdateHandler(CacheEntryUpdateArguments arguments)
    {
        throw new NotImplementedException();
    }
Run Code Online (Sandbox Code Playgroud)

但是,我希望能够动态传递委托/方法/方法名称和参数并执行它.所以我希望看到类似的东西(显然语法错误):

private static CacheItemPolicy GetCachePolicy(CacheType type, int expiry Func<?,?> method)
    {
        var policy = new CacheItemPolicy();

        switch (type)
        {
            case (CacheType.Absolute):
                policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(expiry);
                policy.UpdateCallback = new CacheEntryUpdateCallback(method); …
Run Code Online (Sandbox Code Playgroud)

c# aop caching postsharp

6
推荐指数
1
解决办法
5170
查看次数

你可以在不使用属性的情况下在PostSharp中应用方面吗?

我知道使用Castle Windsor,您可以使用代码而不是将属性应用于类来注册方面(当使用Windsor中的方法拦截作为AOP时).在Postsharp中是否可以相同?这是一个偏好的东西,但更喜欢将方面与接口/对象匹配在一个地方,而不是全部属性.

更新: 好奇我是否可以为与此类似的接口/对象分配方面:

container.Register(
        Component
        .For<IService>()
        .ImplementedBy<Service>()
        .Interceptors(InterceptorReference.ForType<LoggingAspect>()).Anywhere
   );
Run Code Online (Sandbox Code Playgroud)

如果你可以这样做,你可以选择不必在程序集/类/方法上放置属性来应用方面.然后我可以有一个代码文件/类,其中包含哪些方面应用于哪个类/方法/等.

aop postsharp

6
推荐指数
1
解决办法
1973
查看次数