C#7引入了本地函数(非常棒!).假设我有以下代码:
using System;
using PostSharp.Aspects;
namespace AspectCS7
{
class Program
{
private static void Main()
{
[MyAspect]
void LocalFunction()
{
Console.WriteLine("Hello Aspect!");
}
LocalFunction();
}
}
[Serializable]
public class MyAspect : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
Console.WriteLine("Entering Aspect");
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码显示编译时错误.是否可以将属性应用于本地函数?
我正在设计一个具有 AOP 架构(postsharp)的程序,它将拦截所有方法调用,但我需要一种将类附加到每个调用的方法。问题是我不想在每个方法调用中都显式地传递类。那么有没有办法将类附加到 C# 中的方法调用?
例如,在 angular 中,我可以使用自定义拦截器将我想要的任何内容附加到每个传出呼叫的标头。这节省了重复代码。C#中有这样的东西吗?
@Injectable()
export class CustomInterceptor implements HttpInterceptor {
constructor() { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({ withCredentials: true });
return next.handle(request);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在 C# 中的界面
public class Wrapper: IMyInterface
{
private IMyInterface_wrapped;
public Wrapper(IMyInterface caller)
{
_wrapped = caller;
}
public FOO GetUserStuff(string userName)
{
return _wrapped.GetUserStuff(req);
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法像这样调用接口
var wrapper = new Wrapper(new MyInterface());
LoginRequest req = new LoginRequest <------ this needs to be attached …Run Code Online (Sandbox Code Playgroud) 最近提到的PostSharp让我想起了这个:
去年我工作的地方,我们正在考虑使用PostSharp将检测注入我们的代码中.这是在Team Foundation Server团队构建/持续集成环境中.
考虑到这一点,我对PostSharp的运行方式产生了一种唠叨的感觉 - 它编辑了编译器生成的IL.这让我感到困扰.
我并不担心PostSharp不能正常工作; 我很担心这是我第一次回忆起这样的工具.我担心其他工具可能不会考虑到这一点.
事实上,随着我们前进,我们确实遇到了一些问题,因为PostSharp对原始IL所处的文件夹感到困惑.这打破了我们的构建.它似乎是由于与MSBUILD目标的冲突解决了项目引用.冲突似乎是由于PostSharp使用临时目录来存储IL的未修改版本.
无论如何,我当时没有StackOverflow来引用!既然我这样做了,我想问你们所有人是否知道编辑IL作为构建过程的一部分的任何其他工具; 或者Microsoft是否在Visual Studio,MSBUILD,Team Build等中考虑了这种工具.
更新:感谢您的回答.
最重要的是,至少在VS 2010中,微软真的应该意识到这种事情会发生.因此,如果在VS2010中存在这方面的问题,那么微软可能会分担责任.
我希望方面基于如下条件退出方法调用:
[AttributeUsage(AttributeTargets.Method)]
public class IgnoreIfInactiveAttribute : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionEventArgs eventArgs)
{
if (condition)
{
**// How can I make the method return here?**
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助非常感谢.
我正在使用PostSharp 2.1.5.1并在今天发出警告:
在将来的版本中,将从简化版禁用Aspect依赖项(在"MyNamespace.MyAspect.MyVerificationAttribute"上定义).请改用AspectPriority属性.
在我看来,以下行引起了警告:
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.After, StandardRoles.Tracing)]
Run Code Online (Sandbox Code Playgroud)
有人能指出我如何使用的正确例子AspectPriority吗?以下示例是最新的吗?
http://www.sharpcrafters.com/blog/post/introducing-postsharp-2-0-3-aspect-dependencies.aspx("旧优先级优先"一节)
http://www.sharpcrafters.com/blog/post/Day-3-Applying-Aspects-with-Multicasting-Part-2.aspx("Aspect Priority"部分)
谢谢.
我有一个包含许多项目的解决方案,我想为调试版本禁用Postsharp以减少本地开发人员的构建时间.有没有办法在不编辑每个项目文件的情况下执行此操作?
我知道这可能听起来不错,但我们只使用Postsharp进行异常日志记录,我们的构建经历了几个环境进行自动/手动测试(这将使用发布版本),因此我们将了解任何潜在的问题.这点.
如果有帮助,Postsharp会通过nuget添加到项目中.
我想知道是否有任何支持AOP(面向方面编程)和MonoTouch(以及MonoDevelop).
到目前为止,我无法找到任何支持MonoDevelop的工具.据我所知,不支持像PostSharp这样的工具.
假设我有一个方面实现IInstanceScopedAspect,我已将此方面应用于类型中的方法.如何在创建对象时初始化方面FormatterServices.GetUninitializedObject?构造函数没有被执行,因此NullReferenceException当我执行应用方面的方法时,我得到了一个.
是否有可用于初始化对象方面的PostSharp API?
我正在研究使用post sharp,我正在尝试整理一些演示.其中一个是记录异常的方面,另一个是检查空参数并在遇到异常时抛出异常.
我遇到的问题是我希望我的异常日志记录方面记录null检查方面抛出的异常.它似乎没有工作.有没有人知道如何使这项工作?
编辑:要清楚,异常记录器正在处理在方面之外抛出的异常,但是没有记录NullCheckAttribute上的异常
空检查方面的代码如下
[Serializable]
public class NullCheckAttribute : OnMethodBoundaryAspect
{
[ExceptionLogger]
public override void OnEntry(MethodExecutionArgs args)
{
foreach (var argument in args.Arguments)
{
if (argument == null)
throw new InvalidOperationException("Null argument in " + args.Method.Name);
}
}
}
Run Code Online (Sandbox Code Playgroud)
以及异常记录器的代码
[Serializable]
public class ExceptionLoggerAttribute : OnMethodBoundaryAspect
{
public override void OnException(MethodExecutionArgs args)
{
Console.WriteLine("Exception thrown in " + args.Method.Name);
Console.WriteLine("***Message: " + args.Exception.Message);
Console.WriteLine("***Stack trace: " + args.Exception.StackTrace);
}
}
Run Code Online (Sandbox Code Playgroud)
我还应该提一下,我只是使用免费许可证.
我正在将对WCF Web服务的所有请求(包括参数)记录到数据库中.这就是我这样做的方式:
这工作正常,但有时参数非常大,例如一个自定义类,带有几个带有照片,指纹的字节数组等.我想从序列化中排除所有这些字节数组数据类型,这将是最好的方法做到了吗?
序列化json的示例:
[
{
"SaveCommand":{
"Id":5,
"PersonalData":{
"GenderId":2,
"NationalityCode":"DEU",
"FirstName":"John",
"LastName":"Doe",
},
"BiometricAttachments":[
{
"BiometricAttachmentTypeId":1,
"Parameters":null,
"Content":"large Base64 encoded string"
}
]
}
}
]
Run Code Online (Sandbox Code Playgroud)
期望的输出:
[
{
"SaveCommand":{
"Id":5,
"PersonalData":{
"GenderId":2,
"NationalityCode":"DEU",
"FirstName":"John",
"LastName":"Doe",
},
"BiometricAttachments":[
{
"BiometricAttachmentTypeId":1,
"Parameters":null,
"Content":"..."
}
]
}
}
]
Run Code Online (Sandbox Code Playgroud)
编辑:我不能更改用作Web服务方法的参数的类 - 这也意味着我不能使用JsonIgnore属性.
postsharp ×10
c# ×7
.net ×4
aop ×4
aspect ×1
attributes ×1
json.net ×1
monodevelop ×1
wcf ×1
xamarin.ios ×1